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