resources/RooUsage.txt
[app.Builder.js] / old-javascript / JsRender / JsRender.vala
1 //<Script type="text/javascript">
2
3 namespace JsRender {
4
5
6         public errordomain Error {
7                 INVALID_FORMAT,
8                 RENAME_FILE_EXISTS
9         }
10                 
11         public abstract class JsRender  : Object {
12                 /**
13                  * @cfg {Array} doubleStringProps list of properties that can be double quoted.
14                  */
15                 public Gee.ArrayList<string> doubleStringProps;
16                 
17                 public string id;
18                 public string name;   // is the JS name of the file.
19                 public string fullname;
20                 public string path;  // is the full path to the file.
21                 public string parent;  // JS parent.
22                 public string region;  // RooJS - insert region.
23         
24                 public string title;  // a title.. ?? nickname.. ??? -
25
26                 public string permname;
27                 public string language;
28                 public string modOrder;
29                 public string xtype;
30                 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
31                     
32                 public Project.Project project;
33                 //Project : false, // link to container project!
34                 
35                 public Node tree; // the tree of nodes.
36                 
37                 public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
38
39                 public bool hasParent; 
40
41                 public signal void changed (Node? node, string source); 
42
43                 /**
44                  * UI componenets
45                  * 
46                  */
47                 //public Xcls_Editor editor;
48                 
49                 
50                 
51                 public JsRender(Project.Project project, string path) {
52                     
53                         this.cn = new GLib.List<JsRender>();
54                         this.path = path;
55                         this.project = project;
56                         this.hasParent = false;
57                         this.parent = "";
58                         this.tree = null;
59                         this.title = "";
60                         this.region = "";
61                         this.permname = "";
62                         this.modOrder = "";
63                         this.language = "";
64
65                         
66                         // should use basename reallly...
67                         
68                         var ar = this.path.split("/");
69                         // name is in theory filename without .bjs (or .js eventually...)
70                         try {
71                                 Regex regex = new Regex ("\\.(bjs|js)$");
72
73                                 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
74                         } catch (Error e) {
75                                 this.name = "???";
76                         }
77                         this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
78
79                         this.doubleStringProps = new Gee.ArrayList<string>();
80
81                 }
82                 
83                 public void renameTo(string name) 
84                 {
85                         
86                         var bjs = GLib.Path.get_dirname(this.path) +"/" +  name + ".bjs";
87                         if (FileUtils.test(bjs, FileTest.EXISTS)) {
88                                 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
89                         }
90                         GLib.FileUtils.remove(this.path);
91                         this.removeFiles();
92                         // remove other files?
93                         
94                         this.name = name;
95                         this.path = bjs;
96                         
97                 }
98                 
99
100                 
101                 // not sure why xt is needed... -> project contains xtype..
102                 
103                 public static JsRender factory(string xt, Project.Project project, string path)
104                 {
105          
106                         switch (xt) {
107                                 case "Gtk":
108                                         return new Gtk(project, path);
109                                 case "Roo":
110                                         return new Roo(project, path);
111                         }
112                         throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
113                         return null;    
114                 }
115
116                 public string toJsonString()
117                 {
118                         var generator = new Json.Generator ();
119                         generator.indent = 4;
120                         generator.pretty = true;
121                         var node = new Json.Node(Json.NodeType.OBJECT);
122                         node.set_object(this.toJsonObject());
123                         generator.set_root(node);
124                         return generator.to_data(null);
125                 }
126         
127                 public string nickType()
128                 {
129                         var ar = this.name.split(".");
130                         string[] ret = {};
131                         for (var i =0; i < ar.length -1; i++) {
132                                 ret += ar[i];
133                         }
134                         return string.joinv(".", ret);
135                         
136                 }
137                 public string nickName()
138                 {
139                         var ar = this.name.split(".");
140                         return ar[ar.length-1];
141                         
142                 }
143
144                 
145                 public string getIconFileName(bool return_default)
146                 {
147                          
148                         var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path); 
149
150                         var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
151                         if (!FileUtils.test(dir, FileTest.IS_DIR)) {
152                                  File.new_for_path(dir).make_directory();
153                         }
154                         var fname = dir + "/" + m5 + ".png";
155                         
156                         if (!return_default) {
157                                 print("getIconFileName return %s\n", fname);
158                                 return fname;
159                         }
160                         
161                         if (FileUtils.test(fname, FileTest.EXISTS)) {
162                                 print("getIconFileName return %s\n", fname);
163                                 return fname;
164                         }
165                         // we need to create this somehow...
166                         print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
167                         return  GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
168
169                 }
170                 
171
172                 public void saveBJS()
173                 {
174                      
175                     var generator = new Json.Generator ();
176                     generator.indent = 4;
177                     generator.pretty = true;
178                     var node = new Json.Node(Json.NodeType.OBJECT);
179                     node.set_object(this.toJsonObject());
180                     generator.set_root(node);
181                     
182                     print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
183                     try {
184                         this.writeFile(this.path, generator.to_data(null));
185                         //generator.to_file(this.path);
186                     } catch(Error e) {
187                         print("Save failed");
188                     }
189                 }
190                  
191                  
192
193                 public abstract void loadItems() throws GLib.Error;
194                 
195                 /**
196                  *
197                  * load from a javascript file.. rather than bjs..
198                  * 
199                  *
200                  */
201                  /*
202                 _loadItems : function(cb)
203                 {
204                     // already loaded..
205                     if (this.items !== false) {
206                         return false;
207                     }
208                       
209                     
210                     
211                     var tr = new  TokenReader(  { 
212                         keepDocs :true, 
213                         keepWhite : true,  
214                         keepComments : true, 
215                         sepIdents : false,
216                         collapseWhite : false,
217                         filename : args[0],
218                         ignoreBadGrammer: true
219                     });
220                     
221                     var str = File.read(this.path);
222                     var toks = tr.tokenize(new TextStream(str));  
223                     var rf = new JsParser(toks);
224                     rf.parse();
225                     var cfg = rf.cfg;
226                     
227                     this.modOrder = cfg.modOrder || '001';
228                     this.name = cfg.name.replace(/\.bjs/, ''); // BC!
229                     this.parent =  cfg.parent;
230                     this.permname =  cfg.permname || '';
231                     this.title =  cfg.title || cfg.name;;
232                     this.items = cfg.items || []; 
233                     //???
234                     //this.fixItems(_this, false);
235                     cb();
236                     return true;    
237                         
238                 },
239                 */
240                     /**
241                      * accepts:
242                      * { success : , failure : , scope : }
243                      * 
244                      * 
245                      * 
246                      */
247                 /*     
248                 void getTree ( o ) {
249                     print("File.getTree tree called on base object?!?!");
250                 }
251         */
252                 public string jsonHasOrEmpty(Json.Object obj, string key) {
253                         return obj.has_member(key) ? 
254                                                 obj.get_string_member(key) : "";
255                 }
256
257                 
258                 public Json.Object toJsonObject ()
259                 {
260                     
261                     
262                         var ret = new Json.Object();
263                         //ret.set_string_member("id", this.id); // not relivant..
264                         ret.set_string_member("name", this.name);
265                         ret.set_string_member("parent", this.parent == null ? "" : this.parent);
266                         ret.set_string_member("title", this.title == null ? "" : this.title);
267                         ret.set_string_member("path", this.path);
268                         //ret.set_string_member("items", this.items);
269                         ret.set_string_member("permname", this.permname  == null ? "" : this.permname);
270                         ret.set_string_member("modOrder", this.modOrder  == null ? "" : this.modOrder);
271                         var ar = new Json.Array();
272                         // empty files do not have a tree.
273                         if (this.tree != null) {
274                                 ar.add_object_element(this.tree.toJsonObject());
275                         }
276                         ret.set_array_member("items", ar);
277                 
278                     return ret;
279                 }
280                 
281                 
282
283                 public string getTitle ()
284                 {
285                     if (this.title.length > 0) {
286                         return this.title;
287                     }
288                     var a = this.path.split("/");
289                     return a[a.length-1];
290                 }
291                 public string getTitleTip()
292                 {
293                     if (this.title.length > 0) {
294                         return "<b>" + this.title + "</b> " + this.path;
295                     }
296                     return this.path;
297                 }
298                 /*
299                     sortCn: function()
300                     {
301                         this.cn.sort(function(a,b) {
302                             return a.path > b.path;// ? 1 : -1;
303                         });
304                     },
305                 */
306                     // should be in palete provider really..
307
308
309                 public Palete.Palete palete()
310                 {
311                         return Palete.factory(this.xtype);
312
313                 }
314                 
315                 public string guessName(Node ar) // turns the object into full name.
316                 {
317                      // eg. xns: Roo, xtype: XXX -> Roo.xxx
318                     if (!ar.hasXnsType()) {
319                        return "";
320                     }
321                     
322                     return ar.get("* xns") + "." + ar.get("* xtype");
323                                       
324                                         
325                 }
326                 /**
327                  *  non-atomic write (replacement for put contents, as it creates temporary files.
328                  */
329                 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
330                 {
331
332                                  
333                         var f = GLib.File.new_for_path(path);
334                         var data_out = new GLib.DataOutputStream(
335                                           f.replace(null, false, GLib.FileCreateFlags.NONE, null)
336                        );
337                         data_out.put_string(contents, null);
338                         data_out.close(null);
339                 }
340                 /*
341                 copyTo: function(path, cb)
342                 {
343                     var _this = this;
344                     this.loadItems(function() {
345                         
346                         _this.path = path;
347                         cb();
348                     });
349                     
350                 },
351                 */
352                 
353                 /**
354                  * 
355                  * munge JSON tree into Javascript code.
356                  *
357                  * NOTE - needs a deep copy of original tree, before starting..
358                  *     - so that it does not modify current..
359                  * 
360                  * FIXME: + or / prefixes to properties hide it from renderer.
361                  * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
362                  * FIXME: needs to understand what properties might be translatable (eg. double quotes)
363                  * 
364                  * @arg {object} obj the object or array to munge..
365                  * @arg {boolean} isListener - is the array being sent a listener..
366                  * @arg {string} pad - the padding to indent with. 
367                  */
368                 
369                 public string mungeToString(string pad)
370                 {
371                         if (this.tree == null) {
372                                 return "";
373                         }
374                         var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
375                         return x.munge();
376                         
377                     
378                 }
379                 public abstract void save();
380                 public abstract void saveHTML(string html);
381                 public abstract string toSource() ;
382                 public abstract string toSourcePreview() ;
383                 public abstract void removeFiles() ;
384                   
385         } 
386
387 }
388