2 * Renderer for Javascript output (roo library based)
4 * - translation support
5 * - doubleStringProps contains elements that are 'translable'
6 * ** in the old method this our compression tool could extract them
7 * - the new idea is to make a list at the top of the javascript file
26 public Roo(Project.Project project, string path) {
34 // var jstr = JSON.parse(cfg.json);
35 // this.items = [ jstr ];
36 // //console.log(cfg.items.length);
37 // delete cfg.json; // not needed!
39 this.modOrder = "001"; /// sequence id that this uses.
40 this.region = "center";
41 this.disabled = false;
44 this.id = "file-roo-%d".printf(rid++);
46 // various loader methods..
64 for (var i=0;i<dsp.length;i++) {
65 this.doubleStringProps.add(dsp[i]);
72 setNSID : function(id)
75 this.items[0]['|module'] = id;
87 public override void removeFiles() {
88 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
89 if (FileUtils.test(html, FileTest.EXISTS)) {
90 GLib.FileUtils.remove(html);
92 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
93 if (FileUtils.test(js, FileTest.EXISTS)) {
94 GLib.FileUtils.remove(js);
98 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
102 print("load Items!");
103 if (this.tree != null) {
106 print("load " + this.path);
108 var pa = new Json.Parser();
109 pa.load_from_file(this.path);
110 var node = pa.get_root();
112 if (node.get_node_type () != Json.NodeType.OBJECT) {
113 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
115 var obj = node.get_object ();
118 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
119 this.name = this.jsonHasOrEmpty(obj, "name");
120 this.parent = this.jsonHasOrEmpty(obj, "parent");
121 this.permname = this.jsonHasOrEmpty(obj, "permname");
122 this.title = this.jsonHasOrEmpty(obj, "title");
123 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
125 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
126 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
129 // load items[0] ??? into tree...
130 if (obj.has_member("items")
132 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
134 obj.get_array_member("items").get_length() > 0
136 this.tree = new Node();
137 var ar = obj.get_array_member("items");
138 var tree_base = ar.get_object_element(0);
139 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
146 * old code had broken xtypes and used arrays differently,
147 * this code should try and clean it up..
151 fixItems : function(node, fixthis)
155 var fn = this.guessName(node);
156 //print("guessname got " + fn);
158 var bits = fn.split('.');
159 node.xtype = bits.pop();
160 node['|xns'] = bits.join('.');
167 if (!node.items || !node.items.length) {
173 node.items.forEach(function(i) {
177 _this.fixItems(i, true);
178 if (i.xtype == 'Array') {
186 if (!aitems.length) {
190 aitems.forEach(function(i) {
192 if (!i.items || !i.items.length) {
195 var prop = i['*prop'] + '[]';
197 i.items.forEach(function(c) {
216 public override void save()
219 print("--- JsRender.Roo.save");
220 print("save() - reset transStrings\n");
221 this.transStrings = new Gee.HashMap<string,string>();
222 this.findTransStrings(this.tree);
227 if (this.tree == null) {
230 // now write the js file..
233 Regex regex = new Regex("\\.(bjs|js)$");
235 js = regex.replace(this.path,this.path.length , 0 , ".js");
236 } catch (RegexError e) {
238 print("count not make filename from path");
243 //var d = new Date();
244 var js_src = this.toSource();
245 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
247 this.writeFile(js, js_src);
248 } catch (FileError e ) {
249 print("Save failed\n");
251 // for bootstrap - we can write the HTML to the templates directory..
253 //var top = this.guessName(this.items[0]);
254 //print ("TOP = " + top)
264 public override void saveHTML ( string html )
267 var top = this.tree.fqn();
268 print ("TOP = " + top + "\n" );
269 if (top.index_of("Roo.bootstrap.") < 0 &&
270 top.index_of("Roo.mailer.") < 0
276 //now write the js file..
279 Regex regex = new Regex("\\.(bjs|js)$");
281 fn = regex.replace(this.path,this.path.length , 0 , ".html");
282 } catch (RegexError e) {
284 print("count not make filename from path");
287 var bn = GLib.Path.get_basename(fn);
288 var dn = GLib.Path.get_dirname(fn);
290 var targetdir = dn + (
291 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
294 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
295 print("Skip save - templates folder does not exist : %s\n", targetdir);
298 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
300 this.writeFile(targetdir + "/" + bn , html);
301 } catch (FileError e ) {
302 print("SaveHtml failed\n");
309 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
312 if (node.props.has_key("* xinclude")) {
313 ret.add(node.props.get("* xinclude"));
315 for (var i =0; i < node.items.size; i++) {
316 this.findxincludes(node.items.get(i), ret);
324 public override void findTransStrings(Node? node )
326 // iterate properties...
327 // use doubleStringProps
329 // flagging a translatable string..
330 // the code would use string _astring to indicate a translatable string
331 // the to use it it would do String.format(this._message, somedata);
333 // loop through and find string starting with '_'
338 var iter = node.props.map_iterator();
339 while (iter.next()) {
340 // key formats : XXXX
342 // string XXX - with type
343 // $ XXX - with flag (no type)
344 // $ string XXX - with flag
348 node.normalize_key(iter.get_key(), out kname, out kflag, out ktype);
352 var str = iter.get_value();
353 if (this.doubleStringProps.index_of(kname) > -1) {
354 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
355 this.transStrings.set(str,
356 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
361 if (ktype.down() == "string" && kname[0] == '_') {
362 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
363 this.transStrings.set(str,
364 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
373 // iterate children..
374 for (var i =0; i < node.items.size; i++) {
375 this.findTransStrings(node.items.get(i) );
381 public string transStringsToJs()
384 print("Roo.transStringsToJs()\n");
385 if (this.transStrings.size < 1) {
386 print("Roo.transStringsToJs() size < 1?\n");
391 var iter = this.transStrings.map_iterator();
392 while (iter.next()) {
393 kvs += (" '" + iter.get_value() + "' :" +
394 this.tree.quoteString(iter.get_key())
397 return " _strings : {\n" + string.joinv(",\n", kvs) + "\n" +
406 * javascript used in Webkit preview
409 public override string toSourcePreview()
411 print("toSourcePreview() - reset transStrings\n");
412 this.transStrings = new Gee.HashMap<string,string>();
415 print("to source preview\n");
416 if (this.tree == null) {
419 this.findTransStrings(this.tree);
420 var top = this.tree.fqn();
421 var xinc = new Gee.ArrayList<string>();
423 this.findxincludes(this.tree, xinc);
424 print("got %d xincludes\n", xinc.size);
425 var prefix_data = "";
426 if (xinc.size > 0 ) {
427 for(var i = 0; i < xinc.size; i++) {
428 print("check xinclude: %s\n", xinc.get(i));
429 var sf = this.project.getByName(xinc.get(i));
431 print("Failed to find file by name?\n");
436 sf.findTransStrings(sf.tree);
437 var xinc_str = sf.toSource();
440 //FileUtils.get_contents(js, out xinc_str);
441 prefix_data += "\n" + xinc_str + "\n";
449 //print(JSON.stringify(this.items, null,4));
452 print ("guessname returned false");
457 if (top.contains("Dialog")) {
458 return prefix_data + this.toSourceDialog(true);
461 if (top.contains("Modal")) {
462 return prefix_data + this.toSourceModal(true);
465 return prefix_data + this.toSourceLayout(true);
470 public override string setSource(string str) {}
472 * This needs to use some options on the project
473 * to determine how the file is output..
475 * At present we are hard coding it..
479 public override string toSource()
481 // dump the file tree back out to a string.
483 // we have 2 types = dialogs and components
487 if (this.tree == null) {
490 var top = this.tree.fqn();
494 // get the translatable strings..
498 if (top.contains("Dialog")) {
499 return this.toSourceDialog(false);
502 if (top.contains("Modal")) {
503 return this.toSourceModal(false);
505 return this.toSourceLayout(false);
508 eventually support 'classes??'
509 return this.toSourceStdClass();
514 public string outputHeader()
517 "//<script type=\"text/javascript\">",
519 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
523 var ret= string.joinv("\n",s);
524 var bits = this.name.split(".");
525 if (bits.length > 1) {
526 ret += "\nRoo.namespace(\'" +
527 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
531 /// genericlly used..
537 // a standard dialog module.
538 // fixme - this could be alot neater..
539 public string toSourceDialog(bool isPreview)
542 //var items = JSON.parse(JSON.stringify(this.items[0]));
545 var o = this.mungeToString(" ");
548 string[] adda = { " = {",
550 this.transStringsToJs() ,
555 " show : function(data, cb)",
557 " if (!this.dialog) {",
561 " this.callback = cb;",
562 " this.data = data;",
563 " this.dialog.show(this.data._el);",
565 " this.form.reset();",
566 " this.form.setValues(data);",
567 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
572 " create : function()",
574 " var _this = this;",
575 " this.dialog = Roo.factory("
583 return this.outputHeader() + "\n" +
584 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
591 public string toSourceModal(bool isPreview)
595 //var items = JSON.parse(JSON.stringify(this.items[0]));
596 var o = this.mungeToString(" ");
598 string[] adda = { " = {",
600 this.transStringsToJs() ,
605 " show : function(data, cb)",
607 " if (!this.dialog) {",
611 " this.callback = cb;",
612 " this.data = data;",
613 " this.dialog.show(this.data._el);",
615 " this.form.reset();",
616 " this.form.setValues(data);",
617 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
622 " create : function()",
624 " var _this = this;",
625 " this.dialog = Roo.factory("
633 return this.outputHeader() + "\n" +
634 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
641 public string pathToPart()
643 var dir = Path.get_basename(Path.get_dirname(this.path));
644 var ar = dir.split(".");
645 var modname = ar[ar.length-1];
647 // now we have the 'module name'..
648 var fbits = Path.get_basename(this.path).split(".");
651 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
652 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
653 npart = npart.substring(modname.length);
655 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
664 public string toSourceLayout(bool isPreview)
669 // topItem.region = 'center';
670 // topItem.background = false;
673 var o = this.mungeToString(" ");
674 var reg = new Regex("[^A-Za-z.]+");
676 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
678 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
683 // set to false to ensure this is the top level..
685 var topnode = this.tree.fqn();
686 print("topnode = %s\n", topnode);
687 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
688 topnode != "Roo.bootstrap.Body"
690 parent = "\"#bootstrap-body\"";
697 this.outputHeader() + "\n" +
699 this.name + " = new Roo.XComponent({\n" +
701 this.transStringsToJs() + "\n" +
703 " part : "+ this.pathToPart() + ",\n" +
704 /// critical used by builder to associate modules/parts/persm
705 " order : '" +modkey+"',\n" +
706 " region : '" + this.region +"',\n" +
707 " parent : "+ parent + ",\n" +
708 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
709 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
710 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
712 // " tree : function() { return this._tree(); },\n" + //BC
713 " _tree : function()\n" +
715 " var _this = this;\n" + // bc
716 " var MODULE = this;\n" + /// this looks like a better name.
717 " return " + o + ";" +
724 public new string? guessName (Node? ar) // turns the object into full name.
726 // eg. xns: Roo, xtype: XXX -> Roo.xxx
732 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
735 if ( ar.get("xtype").length < 1) {
739 var xtype = ar.get("xtype");
741 if (xtype[0] == '*') { // prefixes????
742 xtype = xtype.substring(1);
744 if (! Regex.match_simple("^Roo", xtype)) {
746 // already starts with roo...
750 var str = string.joinv(".", ret);
754 //Palete.Palete.factory("Roo").guessName(str);
760 string getHelpUrl(string cls)
762 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";