JsRender/JsRender.vala
[app.Builder.js] / JsRender / JsRender.vala
1 //<Script type="text/javascript">
2
3
4 class JsRender.JsRender  : Object {
5     /**
6      * @cfg {Array} doubleStringProps list of properties that can be double quoted.
7      */
8     Array<string> doubleStringProps;
9     
10     string id;
11     string name;   // is the JS name of the file.
12     string path;  // is the full path to the file.
13     string parent;  // JS parent.
14     
15     string title;  // a title.. ?? nickname.. ??? - 
16     Project.Project project;
17     //Project : false, // link to container project!
18     
19     Node tree; // the tree of nodes.
20     
21     GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
22     
23     
24     void JsRender(Project.Project project, string path) {
25         
26         this.cn = new GLib.List<JsRender>();
27         this.path = path;
28         this.project = project;
29         
30         
31     }
32     .JsRender? factory(string xt, Project.Project project, string path)
33     {
34         .JsRender ret;
35         switch (xt) {
36             case "Gtk":
37                 return new JsRender.Gtk(project, path);
38             case "Roo":
39                 return new JsRender.Roo(project, path);
40         }
41         return null;    
42     }
43     
44     void save ()
45     {
46             
47         var write = this.toJsonArray();
48         var generator = new Json.Generator ();
49         generator.indent = 4;
50         generator.pretty = true;
51         var node = new Json.Node();
52         node.init_object(this.toJsonArray());
53         generator.set_root(node);
54         
55         print("WRITE: " + this.path);// + "\n" + JSON.stringify(write));
56         generator.to_file(this.path);
57     }
58         
59     void   saveHTML ()
60     {
61         // NOOP
62     }
63     
64     /**
65      *
66      * load from a javascript file.. rather than bjs..
67      * 
68      *
69      */
70      /*
71     _loadItems : function(cb)
72     {
73         // already loaded..
74         if (this.items !== false) {
75             return false;
76         }
77           
78         
79         
80         var tr = new  TokenReader(  { 
81             keepDocs :true, 
82             keepWhite : true,  
83             keepComments : true, 
84             sepIdents : false,
85             collapseWhite : false,
86             filename : args[0],
87             ignoreBadGrammer: true
88         });
89         
90         var str = File.read(this.path);
91         var toks = tr.tokenize(new TextStream(str));  
92         var rf = new JsParser(toks);
93         rf.parse();
94         var cfg = rf.cfg;
95         
96         this.modOrder = cfg.modOrder || '001';
97         this.name = cfg.name.replace(/\.bjs/, ''); // BC!
98         this.parent =  cfg.parent;
99         this.permname =  cfg.permname || '';
100         this.title =  cfg.title || cfg.name;;
101         this.items = cfg.items || []; 
102         //???
103         //this.fixItems(_this, false);
104         cb();
105         return true;    
106             
107     },
108     */
109         /**
110          * accepts:
111          * { success : , failure : , scope : }
112          * 
113          * 
114          * 
115          */
116     /*     
117     void getTree ( o ) {
118         print("File.getTree tree called on base object?!?!");
119     }
120 */
121     Json.Object toJsonArray ()
122     {
123         
124         
125         var ret = new Json.Object();
126         ret.set_string_member("id", this.id);
127         ret.set_string_member("name", this.name);
128         ret.set_string_member("parent", this.parent);
129         ret.set_string_member("title", this.title);
130         ret.set_string_member("path", this.path);
131         //ret.set_string_member("items", this.items);
132         ret.set_string_member("permname", this.permname);
133         ret.set_string_member("modOrder", this.modOrder);
134         
135         return ret;
136     }
137     
138     
139
140     string getTitle ()
141     {
142         if (this.title.length > 0) {
143             return this.title;
144         }
145         var a = this.path.split('/');
146         return a[a.length-1];
147     }
148     string getTitleTip()
149     {
150         if (this.title.length > 0) {
151             return "<b>" + this.title + "</b> " + this.path;
152         }
153         return this.path;
154     }
155     /*
156         sortCn: function()
157         {
158             this.cn.sort(function(a,b) {
159                 return a.path > b.path;// ? 1 : -1;
160             });
161         },
162     */
163         // should be in palete provider really..
164         
165     string guessName(JsRender.Node ar) // turns the object into full name.
166     {
167          // eg. xns: Roo, xtype: XXX -> Roo.xxx
168         if (!ar.hasXnsType()) {
169            return '';
170         }
171         
172         return ar.get("|xns") +'.' + ar.get("|xtype");
173                           
174                             
175     }
176          
177         
178     /*
179     copyTo: function(path, cb)
180     {
181         var _this = this;
182         this.loadItems(function() {
183             
184             _this.path = path;
185             cb();
186         });
187         
188     },
189     */
190     
191     /**
192      * 
193      * munge JSON tree into Javascript code.
194      *
195      * NOTE - needs a deep copy of original tree, before starting..
196      *     - so that it does not modify current..
197      * 
198      * FIXME: + or / prefixes to properties hide it from renderer.
199      * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
200      * FIXME: needs to understand what properties might be translatable (eg. double quotes)
201      * 
202      * @arg {object} obj the object or array to munge..
203      * @arg {boolean} isListener - is the array being sent a listener..
204      * @arg {string} pad - the padding to indent with. 
205      */
206     
207     function mungeToString(string pad)
208     {
209         return this.tree.mungeToString(false, pad);
210         
211     }
212     
213       
214 }
215     
216      
217  
218
219
220
221
222
223