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) {
31 this.content_type = "";
35 // var jstr = JSON.parse(cfg.json);
36 // this.items = [ jstr ];
37 // //console.log(cfg.items.length);
38 // delete cfg.json; // not needed!
40 this.modOrder = "001"; /// sequence id that this uses.
41 this.region = "center";
42 this.disabled = false;
45 this.id = "file-roo-%d".printf(rid++);
47 // various loader methods..
66 for (var i=0;i<dsp.length;i++) {
67 this.doubleStringProps.add(dsp[i]);
74 setNSID : function(id)
77 this.items[0]['|module'] = id;
89 public override void removeFiles() {
90 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
91 if (FileUtils.test(html, FileTest.EXISTS)) {
92 GLib.FileUtils.remove(html);
94 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
95 if (FileUtils.test(js, FileTest.EXISTS)) {
96 GLib.FileUtils.remove(js);
100 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
104 print("load Items!");
105 if (this.tree != null) {
108 print("load " + this.path);
110 var pa = new Json.Parser();
111 pa.load_from_file(this.path);
112 var node = pa.get_root();
114 if (node.get_node_type () != Json.NodeType.OBJECT) {
115 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
117 var obj = node.get_object ();
120 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
121 this.name = this.jsonHasOrEmpty(obj, "name");
122 this.parent = this.jsonHasOrEmpty(obj, "parent");
123 this.permname = this.jsonHasOrEmpty(obj, "permname");
124 this.title = this.jsonHasOrEmpty(obj, "title");
125 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
127 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
128 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
131 // load items[0] ??? into tree...
132 if (obj.has_member("items")
134 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
136 obj.get_array_member("items").get_length() > 0
138 this.tree = new Node();
139 var ar = obj.get_array_member("items");
140 var tree_base = ar.get_object_element(0);
141 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
149 * old code had broken xtypes and used arrays differently,
150 * this code should try and clean it up..
154 fixItems : function(node, fixthis)
158 var fn = this.guessName(node);
159 //print("guessname got " + fn);
161 var bits = fn.split('.');
162 node.xtype = bits.pop();
163 node['|xns'] = bits.join('.');
170 if (!node.items || !node.items.length) {
176 node.items.forEach(function(i) {
180 _this.fixItems(i, true);
181 if (i.xtype == 'Array') {
189 if (!aitems.length) {
193 aitems.forEach(function(i) {
195 if (!i.items || !i.items.length) {
198 var prop = i['*prop'] + '[]';
200 i.items.forEach(function(c) {
219 public override void save()
222 print("--- JsRender.Roo.save");
223 print("save() - reset transStrings\n");
224 this.transStrings = new Gee.HashMap<string,string>();
225 this.findTransStrings(this.tree);
230 if (this.tree == null) {
233 // now write the js file..
236 Regex regex = new Regex("\\.(bjs|js)$");
238 js = regex.replace(this.path,this.path.length , 0 , ".js");
239 } catch (RegexError e) {
241 print("count not make filename from path");
246 //var d = new Date();
247 var js_src = this.toSource();
248 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
250 this.writeFile(js, js_src);
251 } catch (FileError e ) {
252 print("Save failed\n");
254 // for bootstrap - we can write the HTML to the templates directory..
256 //var top = this.guessName(this.items[0]);
257 //print ("TOP = " + top)
267 public override void saveHTML ( string html )
270 var top = this.tree.fqn();
271 print ("TOP = " + top + "\n" );
272 if (top.index_of("Roo.bootstrap.") < 0 &&
273 top.index_of("Roo.mailer.") < 0
279 //now write the js file..
282 Regex regex = new Regex("\\.(bjs|js)$");
284 fn = regex.replace(this.path,this.path.length , 0 , ".html");
285 } catch (RegexError e) {
287 print("count not make filename from path");
290 var bn = GLib.Path.get_basename(fn);
291 var dn = GLib.Path.get_dirname(fn);
293 var targetdir = dn + (
294 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
297 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
298 print("Skip save - templates folder does not exist : %s\n", targetdir);
301 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
303 this.writeFile(targetdir + "/" + bn , html);
304 } catch (FileError e ) {
305 print("SaveHtml failed\n");
312 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
315 if (node.props.has_key("* xinclude")) {
316 ret.add(node.props.get("* xinclude"));
318 for (var i =0; i < node.items.size; i++) {
319 this.findxincludes(node.items.get(i), ret);
327 public override void findTransStrings(Node? node )
329 // iterate properties...
330 // use doubleStringProps
332 // flagging a translatable string..
333 // the code would use string _astring to indicate a translatable string
334 // the to use it it would do String.format(this._message, somedata);
336 // loop through and find string starting with '_'
341 var iter = node.props.map_iterator();
342 while (iter.next()) {
343 // key formats : XXXX
345 // string XXX - with type
346 // $ XXX - with flag (no type)
347 // $ string XXX - with flag
351 node.normalize_key(iter.get_key(), out kname, out kflag, out ktype);
355 var str = iter.get_value();
356 if (this.doubleStringProps.index_of(kname) > -1) {
357 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
358 this.transStrings.set(str,
359 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
364 if (ktype.down() == "string" && kname[0] == '_') {
365 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
366 this.transStrings.set(str,
367 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
376 // iterate children..
377 for (var i =0; i < node.items.size; i++) {
378 this.findTransStrings(node.items.get(i) );
384 public string transStringsToJs()
387 print("Roo.transStringsToJs()\n");
388 if (this.transStrings.size < 1) {
389 print("Roo.transStringsToJs() size < 1?\n");
394 var iter = this.transStrings.map_iterator();
395 while (iter.next()) {
396 kvs += (" '" + iter.get_value() + "' :" +
397 this.tree.quoteString(iter.get_key())
400 return " _strings : {\n" + string.joinv(",\n", kvs) + "\n" +
409 * javascript used in Webkit preview
412 public override string toSourcePreview()
414 print("toSourcePreview() - reset transStrings\n");
415 this.transStrings = new Gee.HashMap<string,string>();
418 print("to source preview\n");
419 if (this.tree == null) {
422 this.findTransStrings(this.tree);
423 var top = this.tree.fqn();
424 var xinc = new Gee.ArrayList<string>();
426 this.findxincludes(this.tree, xinc);
427 print("got %d xincludes\n", xinc.size);
428 var prefix_data = "";
429 if (xinc.size > 0 ) {
430 for(var i = 0; i < xinc.size; i++) {
431 print("check xinclude: %s\n", xinc.get(i));
432 var sf = this.project.getByName(xinc.get(i));
434 print("Failed to find file by name?\n");
439 sf.findTransStrings(sf.tree);
440 var xinc_str = sf.toSource();
443 //FileUtils.get_contents(js, out xinc_str);
444 prefix_data += "\n" + xinc_str + "\n";
452 //print(JSON.stringify(this.items, null,4));
455 print ("guessname returned false");
460 if (top.contains("Dialog")) {
461 return prefix_data + this.toSourceDialog(true);
464 if (top.contains("Modal")) {
465 return prefix_data + this.toSourceModal(true);
468 return prefix_data + this.toSourceLayout(true);
473 public override void setSource(string str) {}
475 * This needs to use some options on the project
476 * to determine how the file is output..
478 * At present we are hard coding it..
482 public override string toSource()
484 // dump the file tree back out to a string.
486 // we have 2 types = dialogs and components
490 if (this.tree == null) {
493 var top = this.tree.fqn();
497 // get the translatable strings..
501 if (top.contains("Dialog")) {
502 return this.toSourceDialog(false);
505 if (top.contains("Modal")) {
506 return this.toSourceModal(false);
508 return this.toSourceLayout(false);
511 eventually support 'classes??'
512 return this.toSourceStdClass();
517 public string outputHeader()
520 "//<script type=\"text/javascript\">",
522 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
526 var ret= string.joinv("\n",s);
527 var bits = this.name.split(".");
528 if (bits.length > 1) {
529 ret += "\nRoo.namespace(\'" +
530 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
534 /// genericlly used..
540 // a standard dialog module.
541 // fixme - this could be alot neater..
542 public string toSourceDialog(bool isPreview)
545 //var items = JSON.parse(JSON.stringify(this.items[0]));
548 var o = this.mungeToString(" ");
551 string[] adda = { " = {",
553 this.transStringsToJs() ,
558 " show : function(data, cb)",
560 " if (!this.dialog) {",
564 " this.callback = cb;",
565 " this.data = data;",
566 " this.dialog.show(this.data._el);",
568 " this.form.reset();",
569 " this.form.setValues(data);",
570 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
575 " create : function()",
577 " var _this = this;",
578 " this.dialog = Roo.factory("
586 return this.outputHeader() + "\n" +
587 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
594 public string toSourceModal(bool isPreview)
598 //var items = JSON.parse(JSON.stringify(this.items[0]));
599 var o = this.mungeToString(" ");
601 string[] adda = { " = {",
603 this.transStringsToJs() ,
608 " show : function(data, cb)",
610 " if (!this.dialog) {",
614 " this.callback = cb;",
615 " this.data = data;",
616 " this.dialog.show(this.data._el);",
618 " this.form.reset();",
619 " this.form.setValues(data);",
620 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
625 " create : function()",
627 " var _this = this;",
628 " this.dialog = Roo.factory("
636 return this.outputHeader() + "\n" +
637 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
644 public string pathToPart()
646 var dir = Path.get_basename(Path.get_dirname(this.path));
647 var ar = dir.split(".");
648 var modname = ar[ar.length-1];
650 // now we have the 'module name'..
651 var fbits = Path.get_basename(this.path).split(".");
654 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
655 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
656 npart = npart.substring(modname.length);
658 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
667 public string toSourceLayout(bool isPreview)
672 // topItem.region = 'center';
673 // topItem.background = false;
676 var o = this.mungeToString(" ");
677 var reg = new Regex("[^A-Za-z.]+");
679 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
681 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
686 // set to false to ensure this is the top level..
688 var topnode = this.tree.fqn();
689 print("topnode = %s\n", topnode);
690 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
691 topnode != "Roo.bootstrap.Body"
693 parent = "\"#bootstrap-body\"";
700 this.outputHeader() + "\n" +
702 this.name + " = new Roo.XComponent({\n" +
704 this.transStringsToJs() + "\n" +
706 " part : "+ this.pathToPart() + ",\n" +
707 /// critical used by builder to associate modules/parts/persm
708 " order : '" +modkey+"',\n" +
709 " region : '" + this.region +"',\n" +
710 " parent : "+ parent + ",\n" +
711 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
712 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
713 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
715 // " tree : function() { return this._tree(); },\n" + //BC
716 " _tree : function()\n" +
718 " var _this = this;\n" + // bc
719 " var MODULE = this;\n" + /// this looks like a better name.
720 " return " + o + ";" +
727 public new string? guessName (Node? ar) // turns the object into full name.
729 // eg. xns: Roo, xtype: XXX -> Roo.xxx
735 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
738 if ( ar.get("xtype").length < 1) {
742 var xtype = ar.get("xtype");
744 if (xtype[0] == '*') { // prefixes????
745 xtype = xtype.substring(1);
747 if (! Regex.match_simple("^Roo", xtype)) {
749 // already starts with roo...
753 var str = string.joinv(".", ret);
757 //Palete.Palete.factory("Roo").guessName(str);
763 string getHelpUrl(string cls)
765 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";