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