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