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