13 public Roo(Project.Project project, string path) {
21 // var jstr = JSON.parse(cfg.json);
22 // this.items = [ jstr ];
23 // //console.log(cfg.items.length);
24 // delete cfg.json; // not needed!
26 this.modOrder = "001"; /// sequence id that this uses.
27 this.region = "center";
28 this.disabled = false;
31 this.id = "file-roo-%d".printf(rid++);
33 // various loader methods..
35 string[] dsp = { "title",
44 for (var i=0;i<dsp.length;i++) {
45 this.doubleStringProps.add(dsp[i]);
52 setNSID : function(id)
55 this.items[0]['|module'] = id;
67 public override void removeFiles() {
68 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
69 if (FileUtils.test(html, FileTest.EXISTS)) {
70 GLib.FileUtils.remove(html);
72 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
73 if (FileUtils.test(js, FileTest.EXISTS)) {
74 GLib.FileUtils.remove(js);
78 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
83 if (this.tree != null) {
86 print("load " + this.path);
88 var pa = new Json.Parser();
89 pa.load_from_file(this.path);
90 var node = pa.get_root();
92 if (node.get_node_type () != Json.NodeType.OBJECT) {
93 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
95 var obj = node.get_object ();
98 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
99 this.name = this.jsonHasOrEmpty(obj, "name");
100 this.parent = this.jsonHasOrEmpty(obj, "parent");
101 this.permname = this.jsonHasOrEmpty(obj, "permname");
102 this.title = this.jsonHasOrEmpty(obj, "title");
103 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
105 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
106 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
109 // load items[0] ??? into tree...
110 if (obj.has_member("items")
112 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
114 obj.get_array_member("items").get_length() > 0
116 this.tree = new Node();
117 var ar = obj.get_array_member("items");
118 var tree_base = ar.get_object_element(0);
119 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
126 * old code had broken xtypes and used arrays differently,
127 * this code should try and clean it up..
131 fixItems : function(node, fixthis)
135 var fn = this.guessName(node);
136 //print("guessname got " + fn);
138 var bits = fn.split('.');
139 node.xtype = bits.pop();
140 node['|xns'] = bits.join('.');
147 if (!node.items || !node.items.length) {
153 node.items.forEach(function(i) {
157 _this.fixItems(i, true);
158 if (i.xtype == 'Array') {
166 if (!aitems.length) {
170 aitems.forEach(function(i) {
172 if (!i.items || !i.items.length) {
175 var prop = i['*prop'] + '[]';
177 i.items.forEach(function(c) {
196 public override void save()
199 print("--- JsRender.Roo.save");
203 if (this.tree == null) {
206 // now write the js file..
209 Regex regex = new Regex("\\.(bjs|js)$");
211 js = regex.replace(this.path,this.path.length , 0 , ".js");
212 } catch (RegexError e) {
214 print("count not make filename from path");
219 //var d = new Date();
220 var js_src = this.toSource();
221 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
223 this.writeFile(js, js_src);
224 } catch (FileError e ) {
225 print("Save failed\n");
227 // for bootstrap - we can write the HTML to the templates directory..
229 //var top = this.guessName(this.items[0]);
230 //print ("TOP = " + top)
240 public override void saveHTML ( string html )
243 var top = this.tree.fqn();
244 print ("TOP = " + top + "\n" );
245 if (top.index_of("Roo.bootstrap.") < 0 &&
246 top.index_of("Roo.mailer.") < 0
252 // now write the js file..
255 Regex regex = new Regex("\\.(bjs|js)$");
257 fn = regex.replace(this.path,this.path.length , 0 , ".html");
258 } catch (RegexError e) {
260 print("count not make filename from path");
263 var bn = GLib.Path.get_basename(fn);
264 var dn = GLib.Path.get_dirname(fn);
266 var targetdir = dn + (
267 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
270 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
271 print("Skip save - templates folder does not exist : %s\n", targetdir);
274 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
276 this.writeFile(targetdir + "/" + bn , html);
277 } catch (FileError e ) {
278 print("SaveHtml failed\n");
285 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
288 if (node.props.has_key("* xinclude")) {
289 ret.add(node.props.get("* xinclude"));
291 for (var i =0; i < node.items.size; i++) {
292 this.findxincludes(node.items.get(i), ret);
297 Gee.HashMap<string,string> transStrings; // map of md5 -> string..
299 public string addTransString(string str)
301 this.transStrings.set(str, GLib.Checksum.compute_for_string (ChecksumType.MD5, str));
304 public Gee.ArrayList<string> transStrings(Node node, Gee.ArrayList<string> ret)
306 // iterate properties...
307 // use doubleStringProps
311 // iterate children..
312 for (var i =0; i < node.items.size; i++) {
313 this.transStrings(node.items.get(i), ret);
319 * javascript used in Webkit preview
322 public override string toSourcePreview()
324 print("to source preview\n");
325 if (this.tree == null) {
328 var top = this.tree.fqn();
329 var xinc = new Gee.ArrayList<string>();
331 this.findxincludes(this.tree, xinc);
332 print("got %d xincludes\n", xinc.size);
333 var prefix_data = "";
334 if (xinc.size > 0 ) {
335 for(var i = 0; i < xinc.size; i++) {
336 print("check xinclude: %s\n", xinc.get(i));
337 var sf = this.project.getByName(xinc.get(i));
339 print("Failed to find file by name?\n");
344 var xinc_str = sf.toSource();
347 //FileUtils.get_contents(js, out xinc_str);
348 prefix_data += "\n" + xinc_str + "\n";
356 //print(JSON.stringify(this.items, null,4));
359 print ("guessname returned false");
364 if (top.contains("Dialog")) {
365 return prefix_data + this.toSourceDialog(true);
368 if (top.contains("Modal")) {
369 return prefix_data + this.toSourceModal(true);
372 return prefix_data + this.toSourceLayout(true);
379 * This needs to use some options on the project
380 * to determine how the file is output..
382 * At present we are hard coding it..
386 public override string toSource()
388 // dump the file tree back out to a string.
390 // we have 2 types = dialogs and components
392 if (this.tree == null) {
395 var top = this.tree.fqn();
399 if (top.contains("Dialog")) {
400 return this.toSourceDialog(false);
403 if (top.contains("Modal")) {
404 return this.toSourceModal(false);
406 return this.toSourceLayout(false);
409 eventually support 'classes??'
410 return this.toSourceStdClass();
415 public string outputHeader()
418 "//<script type=\"text/javascript\">",
420 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
424 var ret= string.joinv("\n",s);
425 var bits = this.name.split(".");
426 if (bits.length > 1) {
427 ret += "\nRoo.namespace(\'" +
428 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
432 /// genericlly used..
438 // a standard dialog module.
439 // fixme - this could be alot neater..
440 public string toSourceDialog(bool isPreview)
443 //var items = JSON.parse(JSON.stringify(this.items[0]));
446 var o = this.mungeToString(" ");
449 string[] adda = { " = {",
454 " show : function(data, cb)",
456 " if (!this.dialog) {",
460 " this.callback = cb;",
461 " this.data = data;",
462 " this.dialog.show(this.data._el);",
464 " this.form.reset();",
465 " this.form.setValues(data);",
466 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
471 " create : function()",
473 " var _this = this;",
474 " this.dialog = Roo.factory("
482 return this.outputHeader() + "\n" +
483 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
490 public string toSourceModal(bool isPreview)
494 //var items = JSON.parse(JSON.stringify(this.items[0]));
495 var o = this.mungeToString(" ");
497 string[] adda = { " = {",
502 " show : function(data, cb)",
504 " if (!this.dialog) {",
508 " this.callback = cb;",
509 " this.data = data;",
510 " this.dialog.show(this.data._el);",
512 " this.form.reset();",
513 " this.form.setValues(data);",
514 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
519 " create : function()",
521 " var _this = this;",
522 " this.dialog = Roo.factory("
530 return this.outputHeader() + "\n" +
531 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
538 public string pathToPart()
540 var dir = Path.get_basename(Path.get_dirname(this.path));
541 var ar = dir.split(".");
542 var modname = ar[ar.length-1];
544 // now we have the 'module name'..
545 var fbits = Path.get_basename(this.path).split(".");
548 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
549 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
550 npart = npart.substring(modname.length);
552 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
561 public string toSourceLayout(bool isPreview)
566 // topItem.region = 'center';
567 // topItem.background = false;
570 var o = this.mungeToString(" ");
571 var reg = new Regex("[^A-Za-z.]+");
573 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
575 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
580 // set to false to ensure this is the top level..
582 var topnode = this.tree.fqn();
583 print("topnode = %s\n", topnode);
584 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
585 topnode != "Roo.bootstrap.Body"
587 parent = "\"#bootstrap-body\"";
594 this.outputHeader() + "\n" +
596 this.name + " = new Roo.XComponent({\n" +
597 " part : "+ this.pathToPart() + ",\n" +
598 /// critical used by builder to associate modules/parts/persm
599 " order : '" +modkey+"',\n" +
600 " region : '" + this.region +"',\n" +
601 " parent : "+ parent + ",\n" +
602 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
603 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
604 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
606 // " tree : function() { return this._tree(); },\n" + //BC
607 " _tree : function()\n" +
609 " var _this = this;\n" + // bc
610 " var MODULE = this;\n" + /// this looks like a better name.
611 " return " + o + ";" +
619 public new string? guessName (Node? ar) // turns the object into full name.
621 // eg. xns: Roo, xtype: XXX -> Roo.xxx
627 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
630 if ( ar.get("xtype").length < 1) {
634 var xtype = ar.get("xtype");
636 if (xtype[0] == '*') { // prefixes????
637 xtype = xtype.substring(1);
639 if (! Regex.match_simple("^Roo", xtype)) {
641 // already starts with roo...
645 var str = string.joinv(".", ret);
649 //Palete.Palete.factory("Roo").guessName(str);
655 string getHelpUrl(string cls)
657 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";