JsRender/JsRender.vala
[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 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.toJsonArray());
81         generator.set_root(node);
82         return generator.to_data(null);
83         }
84         
85     public void save ()
86     {
87          
88         var generator = new Json.Generator ();
89         generator.indent = 4;
90         generator.pretty = true;
91         var node = new Json.Node(Json.NodeType.OBJECT);
92         node.set_object(this.toJsonArray());
93         generator.set_root(node);
94         
95         print("WRITE: " + this.path);// + "\n" + JSON.stringify(write));
96         try {
97             generator.to_file(this.path);
98         } catch(Error e) {
99             print("Save failed");
100         }
101     }
102         
103     public void   saveHTML ()
104     {
105         // NOOP
106     }
107
108     public void loadItems()
109     {
110        print("load items not handled..");
111
112         
113     }
114     
115     /**
116      *
117      * load from a javascript file.. rather than bjs..
118      * 
119      *
120      */
121      /*
122     _loadItems : function(cb)
123     {
124         // already loaded..
125         if (this.items !== false) {
126             return false;
127         }
128           
129         
130         
131         var tr = new  TokenReader(  { 
132             keepDocs :true, 
133             keepWhite : true,  
134             keepComments : true, 
135             sepIdents : false,
136             collapseWhite : false,
137             filename : args[0],
138             ignoreBadGrammer: true
139         });
140         
141         var str = File.read(this.path);
142         var toks = tr.tokenize(new TextStream(str));  
143         var rf = new JsParser(toks);
144         rf.parse();
145         var cfg = rf.cfg;
146         
147         this.modOrder = cfg.modOrder || '001';
148         this.name = cfg.name.replace(/\.bjs/, ''); // BC!
149         this.parent =  cfg.parent;
150         this.permname =  cfg.permname || '';
151         this.title =  cfg.title || cfg.name;;
152         this.items = cfg.items || []; 
153         //???
154         //this.fixItems(_this, false);
155         cb();
156         return true;    
157             
158     },
159     */
160         /**
161          * accepts:
162          * { success : , failure : , scope : }
163          * 
164          * 
165          * 
166          */
167     /*     
168     void getTree ( o ) {
169         print("File.getTree tree called on base object?!?!");
170     }
171 */
172     public Json.Object toJsonArray ()
173     {
174         
175         
176         var ret = new Json.Object();
177         ret.set_string_member("id", this.id);
178         ret.set_string_member("name", this.name);
179         ret.set_string_member("parent", this.parent);
180         ret.set_string_member("title", this.title);
181         ret.set_string_member("path", this.path);
182         //ret.set_string_member("items", this.items);
183         ret.set_string_member("permname", this.permname);
184         ret.set_string_member("modOrder", this.modOrder);
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 string toSource() {
264                  return "??";
265          }
266       
267 }
268     
269
270
271
272
273
274
275
276