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