11 public Roo(Project.Project project, string path) {
19 // var jstr = JSON.parse(cfg.json);
20 // this.items = [ jstr ];
21 // //console.log(cfg.items.length);
22 // delete cfg.json; // not needed!
24 this.modOrder = "001"; /// sequence id that this uses.
25 this.region = "center";
26 this.disabled = false;
29 this.id = "file-roo-%d".printf(rid++);
31 // various loader methods..
33 string[] dsp = { "title",
42 for (var i=0;i<dsp.length;i++) {
43 this.doubleStringProps.add(dsp[i]);
50 setNSID : function(id)
53 this.items[0]['|module'] = id;
65 public override void removeFiles() {
66 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
67 if (FileUtils.test(html, FileTest.EXISTS)) {
68 GLib.FileUtils.remove(html);
70 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
71 if (FileUtils.test(js, FileTest.EXISTS)) {
72 GLib.FileUtils.remove(js);
76 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
81 if (this.tree != null) {
84 print("load " + this.path);
86 var pa = new Json.Parser();
87 pa.load_from_file(this.path);
88 var node = pa.get_root();
90 if (node.get_node_type () != Json.NodeType.OBJECT) {
91 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
93 var obj = node.get_object ();
96 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
97 this.name = this.jsonHasOrEmpty(obj, "name");
98 this.parent = this.jsonHasOrEmpty(obj, "parent");
99 this.permname = this.jsonHasOrEmpty(obj, "permname");
100 this.title = this.jsonHasOrEmpty(obj, "title");
101 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
103 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
104 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
107 // load items[0] ??? into tree...
108 if (obj.has_member("items")
110 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
112 obj.get_array_member("items").get_length() > 0
114 this.tree = new Node();
115 var ar = obj.get_array_member("items");
116 var tree_base = ar.get_object_element(0);
117 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
124 * old code had broken xtypes and used arrays differently,
125 * this code should try and clean it up..
129 fixItems : function(node, fixthis)
133 var fn = this.guessName(node);
134 //print("guessname got " + fn);
136 var bits = fn.split('.');
137 node.xtype = bits.pop();
138 node['|xns'] = bits.join('.');
145 if (!node.items || !node.items.length) {
151 node.items.forEach(function(i) {
155 _this.fixItems(i, true);
156 if (i.xtype == 'Array') {
164 if (!aitems.length) {
168 aitems.forEach(function(i) {
170 if (!i.items || !i.items.length) {
173 var prop = i['*prop'] + '[]';
175 i.items.forEach(function(c) {
194 public override void save()
197 print("--- JsRender.Roo.save");
201 if (this.tree == null) {
204 // now write the js file..
207 Regex regex = new Regex("\\.(bjs|js)$");
209 js = regex.replace(this.path,this.path.length , 0 , ".js");
210 } catch (RegexError e) {
212 print("count not make filename from path");
217 //var d = new Date();
218 var js_src = this.toSource();
219 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
221 this.writeFile(js, js_src);
222 } catch (FileError e ) {
223 print("Save failed\n");
225 // for bootstrap - we can write the HTML to the templates directory..
227 //var top = this.guessName(this.items[0]);
228 //print ("TOP = " + top)
238 public override void saveHTML ( string html )
241 var top = this.tree.fqn();
242 print ("TOP = " + top + "\n" );
243 if (top.index_of("Roo.bootstrap.") < 0 &&
244 top.index_of("Roo.mailer.") < 0
250 // now write the js file..
253 Regex regex = new Regex("\\.(bjs|js)$");
255 fn = regex.replace(this.path,this.path.length , 0 , ".html");
256 } catch (RegexError e) {
258 print("count not make filename from path");
261 var bn = GLib.Path.get_basename(fn);
262 var dn = GLib.Path.get_dirname(fn);
264 var targetdir = dn + (
265 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
268 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
269 print("Skip save - templates folder does not exist : %s\n", targetdir);
272 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
274 this.writeFile(targetdir + "/" + bn , html);
275 } catch (FileError e ) {
276 print("SaveHtml failed\n");
283 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
286 if (node.props.has_key("* xinclude")) {
287 ret.add(node.props.get("* xinclude"));
289 for (var i =0; i < node.items.size; i++) {
290 this.findxincludes(node.items.get(i), ret);
297 * javascript used in Webkit preview
300 public override string toSourcePreview()
302 print("to source preview\n");
303 if (this.tree == null) {
306 var top = this.tree.fqn();
307 var xinc = new Gee.ArrayList<string>();
309 this.findxincludes(this.tree, xinc);
310 print("got %d xincludes\n", xinc.size);
311 var prefix_data = "";
312 if (xinc.size > 0 ) {
313 for(var i = 0; i < xinc.size; i++) {
314 print("check xinclude: %s\n", xinc.get(i));
315 var sf = this.project.getByName(xinc.get(i));
317 print("Failed to find file by name?\n");
322 var xinc_str = sf.toSource();
325 //FileUtils.get_contents(js, out xinc_str);
326 prefix_data += "\n" + xinc_str + "\n";
334 //print(JSON.stringify(this.items, null,4));
337 print ("guessname returned false");
342 if (top.contains("Dialog")) {
343 return prefix_data + this.toSourceDialog(true);
346 if (top.contains("Modal")) {
347 return prefix_data + this.toSourceModal(true);
350 return prefix_data + this.toSourceLayout(true);
357 * This needs to use some options on the project
358 * to determine how the file is output..
360 * At present we are hard coding it..
364 public override string toSource()
366 // dump the file tree back out to a string.
368 // we have 2 types = dialogs and components
370 if (this.tree == null) {
373 var top = this.tree.fqn();
377 if (top.contains("Dialog")) {
378 return this.toSourceDialog(false);
381 if (top.contains("Modal")) {
382 return this.toSourceModal(false);
384 return this.toSourceLayout(false);
387 eventually support 'classes??'
388 return this.toSourceStdClass();
393 public string outputHeader()
396 "//<script type=\"text/javascript\">",
398 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
402 var ret= string.joinv("\n",s);
403 var bits = this.name.split(".");
404 if (bits.length > 1) {
405 ret += "\nRoo.namespace(\'" +
406 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
410 /// genericlly used..
416 // a standard dialog module.
417 // fixme - this could be alot neater..
418 public string toSourceDialog(bool isPreview)
421 //var items = JSON.parse(JSON.stringify(this.items[0]));
424 var o = this.mungeToString(" ");
427 string[] adda = { " = {",
432 " show : function(data, cb)",
434 " if (!this.dialog) {",
438 " this.callback = cb;",
439 " this.data = data;",
440 " this.dialog.show(this.data._el);",
442 " this.form.reset();",
443 " this.form.setValues(data);",
444 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
449 " create : function()",
451 " var _this = this;",
452 " this.dialog = Roo.factory("
460 return this.outputHeader() + "\n" +
461 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
468 public string toSourceModal(bool isPreview)
472 //var items = JSON.parse(JSON.stringify(this.items[0]));
473 var o = this.mungeToString(" ");
475 string[] adda = { " = {",
480 " show : function(data, cb)",
482 " if (!this.dialog) {",
486 " this.callback = cb;",
487 " this.data = data;",
488 " this.dialog.show(this.data._el);",
490 " this.form.reset();",
491 " this.form.setValues(data);",
492 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
497 " create : function()",
499 " var _this = this;",
500 " this.dialog = Roo.factory("
508 return this.outputHeader() + "\n" +
509 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
516 public string pathToPart()
518 var dir = Path.get_basename(Path.get_dirname(this.path));
519 var ar = dir.split(".");
520 var modname = ar[ar.length-1];
522 // now we have the 'module name'..
523 var fbits = Path.get_basename(this.path).split(".");
526 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
527 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
528 npart = npart.substring(modname.length);
530 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
539 public string toSourceLayout(bool isPreview)
544 // topItem.region = 'center';
545 // topItem.background = false;
548 var o = this.mungeToString(" ");
549 var reg = new Regex("[^A-Za-z.]+");
551 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
553 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
558 // set to false to ensure this is the top level..
560 var topnode = this.tree.fqn();
561 print("topnode = %s\n", topnode);
562 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
563 topnode != "Roo.bootstrap.Body"
565 parent = "\"#bootstrap-body\"";
572 this.outputHeader() + "\n" +
574 this.name + " = new Roo.XComponent({\n" +
575 " part : "+ this.pathToPart() + ",\n" +
576 /// critical used by builder to associate modules/parts/persm
577 " order : '" +modkey+"',\n" +
578 " region : '" + this.region +"',\n" +
579 " parent : "+ parent + ",\n" +
580 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
581 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
582 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
584 // " tree : function() { return this._tree(); },\n" + //BC
585 " _tree : function()\n" +
587 " var _this = this;\n" + // bc
588 " var MODULE = this;\n" + /// this looks like a better name.
589 " return " + o + ";" +
597 public new string? guessName (Node? ar) // turns the object into full name.
599 // eg. xns: Roo, xtype: XXX -> Roo.xxx
605 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
608 if ( ar.get("xtype").length < 1) {
612 var xtype = ar.get("xtype");
614 if (xtype[0] == '*') { // prefixes????
615 xtype = xtype.substring(1);
617 if (! Regex.match_simple("^Roo", xtype)) {
619 // already starts with roo...
623 var str = string.joinv(".", ret);
627 //Palete.Palete.factory("Roo").guessName(str);
633 string getHelpUrl(string cls)
635 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";