11 Gee.HashMap<string,string> transStrings; // map of md5 -> string..
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 public Gee.ArrayList<string> transStrings(Node node, Gee.ArrayList<string> ret)
299 // iterate properties...
303 // iterate children..
304 for (var i =0; i < node.items.size; i++) {
305 this.transStrings(node.items.get(i), ret);
311 * javascript used in Webkit preview
314 public override string toSourcePreview()
316 print("to source preview\n");
317 if (this.tree == null) {
320 var top = this.tree.fqn();
321 var xinc = new Gee.ArrayList<string>();
323 this.findxincludes(this.tree, xinc);
324 print("got %d xincludes\n", xinc.size);
325 var prefix_data = "";
326 if (xinc.size > 0 ) {
327 for(var i = 0; i < xinc.size; i++) {
328 print("check xinclude: %s\n", xinc.get(i));
329 var sf = this.project.getByName(xinc.get(i));
331 print("Failed to find file by name?\n");
336 var xinc_str = sf.toSource();
339 //FileUtils.get_contents(js, out xinc_str);
340 prefix_data += "\n" + xinc_str + "\n";
348 //print(JSON.stringify(this.items, null,4));
351 print ("guessname returned false");
356 if (top.contains("Dialog")) {
357 return prefix_data + this.toSourceDialog(true);
360 if (top.contains("Modal")) {
361 return prefix_data + this.toSourceModal(true);
364 return prefix_data + this.toSourceLayout(true);
371 * This needs to use some options on the project
372 * to determine how the file is output..
374 * At present we are hard coding it..
378 public override string toSource()
380 // dump the file tree back out to a string.
382 // we have 2 types = dialogs and components
384 if (this.tree == null) {
387 var top = this.tree.fqn();
391 if (top.contains("Dialog")) {
392 return this.toSourceDialog(false);
395 if (top.contains("Modal")) {
396 return this.toSourceModal(false);
398 return this.toSourceLayout(false);
401 eventually support 'classes??'
402 return this.toSourceStdClass();
407 public string outputHeader()
410 "//<script type=\"text/javascript\">",
412 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
416 var ret= string.joinv("\n",s);
417 var bits = this.name.split(".");
418 if (bits.length > 1) {
419 ret += "\nRoo.namespace(\'" +
420 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
424 /// genericlly used..
430 // a standard dialog module.
431 // fixme - this could be alot neater..
432 public string toSourceDialog(bool isPreview)
435 //var items = JSON.parse(JSON.stringify(this.items[0]));
438 var o = this.mungeToString(" ");
441 string[] adda = { " = {",
446 " show : function(data, cb)",
448 " if (!this.dialog) {",
452 " this.callback = cb;",
453 " this.data = data;",
454 " this.dialog.show(this.data._el);",
456 " this.form.reset();",
457 " this.form.setValues(data);",
458 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
463 " create : function()",
465 " var _this = this;",
466 " this.dialog = Roo.factory("
474 return this.outputHeader() + "\n" +
475 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
482 public string toSourceModal(bool isPreview)
486 //var items = JSON.parse(JSON.stringify(this.items[0]));
487 var o = this.mungeToString(" ");
489 string[] adda = { " = {",
494 " show : function(data, cb)",
496 " if (!this.dialog) {",
500 " this.callback = cb;",
501 " this.data = data;",
502 " this.dialog.show(this.data._el);",
504 " this.form.reset();",
505 " this.form.setValues(data);",
506 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
511 " create : function()",
513 " var _this = this;",
514 " this.dialog = Roo.factory("
522 return this.outputHeader() + "\n" +
523 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
530 public string pathToPart()
532 var dir = Path.get_basename(Path.get_dirname(this.path));
533 var ar = dir.split(".");
534 var modname = ar[ar.length-1];
536 // now we have the 'module name'..
537 var fbits = Path.get_basename(this.path).split(".");
540 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
541 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
542 npart = npart.substring(modname.length);
544 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
553 public string toSourceLayout(bool isPreview)
558 // topItem.region = 'center';
559 // topItem.background = false;
562 var o = this.mungeToString(" ");
563 var reg = new Regex("[^A-Za-z.]+");
565 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
567 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
572 // set to false to ensure this is the top level..
574 var topnode = this.tree.fqn();
575 print("topnode = %s\n", topnode);
576 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
577 topnode != "Roo.bootstrap.Body"
579 parent = "\"#bootstrap-body\"";
586 this.outputHeader() + "\n" +
588 this.name + " = new Roo.XComponent({\n" +
589 " part : "+ this.pathToPart() + ",\n" +
590 /// critical used by builder to associate modules/parts/persm
591 " order : '" +modkey+"',\n" +
592 " region : '" + this.region +"',\n" +
593 " parent : "+ parent + ",\n" +
594 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
595 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
596 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
598 // " tree : function() { return this._tree(); },\n" + //BC
599 " _tree : function()\n" +
601 " var _this = this;\n" + // bc
602 " var MODULE = this;\n" + /// this looks like a better name.
603 " return " + o + ";" +
611 public new string? guessName (Node? ar) // turns the object into full name.
613 // eg. xns: Roo, xtype: XXX -> Roo.xxx
619 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
622 if ( ar.get("xtype").length < 1) {
626 var xtype = ar.get("xtype");
628 if (xtype[0] == '*') { // prefixes????
629 xtype = xtype.substring(1);
631 if (! Regex.match_simple("^Roo", xtype)) {
633 // already starts with roo...
637 var str = string.joinv(".", ret);
641 //Palete.Palete.factory("Roo").guessName(str);
647 string getHelpUrl(string cls)
649 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";