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