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