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