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