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..
65 for (var i=0;i<dsp.length;i++) {
66 this.doubleStringProps.add(dsp[i]);
73 setNSID : function(id)
76 this.items[0]['|module'] = id;
88 public override void removeFiles() {
89 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
90 if (FileUtils.test(html, FileTest.EXISTS)) {
91 GLib.FileUtils.remove(html);
93 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
94 if (FileUtils.test(js, FileTest.EXISTS)) {
95 GLib.FileUtils.remove(js);
99 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
103 print("load Items!");
104 if (this.tree != null) {
107 print("load " + this.path);
109 var pa = new Json.Parser();
110 pa.load_from_file(this.path);
111 var node = pa.get_root();
113 if (node.get_node_type () != Json.NodeType.OBJECT) {
114 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
116 var obj = node.get_object ();
119 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
120 this.name = this.jsonHasOrEmpty(obj, "name");
121 this.parent = this.jsonHasOrEmpty(obj, "parent");
122 this.permname = this.jsonHasOrEmpty(obj, "permname");
123 this.title = this.jsonHasOrEmpty(obj, "title");
124 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
126 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
127 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
130 // load items[0] ??? into tree...
131 if (obj.has_member("items")
133 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
135 obj.get_array_member("items").get_length() > 0
137 this.tree = new Node();
138 var ar = obj.get_array_member("items");
139 var tree_base = ar.get_object_element(0);
140 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
147 * old code had broken xtypes and used arrays differently,
148 * this code should try and clean it up..
152 fixItems : function(node, fixthis)
156 var fn = this.guessName(node);
157 //print("guessname got " + fn);
159 var bits = fn.split('.');
160 node.xtype = bits.pop();
161 node['|xns'] = bits.join('.');
168 if (!node.items || !node.items.length) {
174 node.items.forEach(function(i) {
178 _this.fixItems(i, true);
179 if (i.xtype == 'Array') {
187 if (!aitems.length) {
191 aitems.forEach(function(i) {
193 if (!i.items || !i.items.length) {
196 var prop = i['*prop'] + '[]';
198 i.items.forEach(function(c) {
217 public override void save()
220 print("--- JsRender.Roo.save");
221 print("save() - reset transStrings\n");
222 this.transStrings = new Gee.HashMap<string,string>();
223 this.findTransStrings(this.tree);
228 if (this.tree == null) {
231 // now write the js file..
234 Regex regex = new Regex("\\.(bjs|js)$");
236 js = regex.replace(this.path,this.path.length , 0 , ".js");
237 } catch (RegexError e) {
239 print("count not make filename from path");
244 //var d = new Date();
245 var js_src = this.toSource();
246 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
248 this.writeFile(js, js_src);
249 } catch (FileError e ) {
250 print("Save failed\n");
252 // for bootstrap - we can write the HTML to the templates directory..
254 //var top = this.guessName(this.items[0]);
255 //print ("TOP = " + top)
265 public override void saveHTML ( string html )
268 var top = this.tree.fqn();
269 print ("TOP = " + top + "\n" );
270 if (top.index_of("Roo.bootstrap.") < 0 &&
271 top.index_of("Roo.mailer.") < 0
277 //now write the js file..
280 Regex regex = new Regex("\\.(bjs|js)$");
282 fn = regex.replace(this.path,this.path.length , 0 , ".html");
283 } catch (RegexError e) {
285 print("count not make filename from path");
288 var bn = GLib.Path.get_basename(fn);
289 var dn = GLib.Path.get_dirname(fn);
291 var targetdir = dn + (
292 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
295 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
296 print("Skip save - templates folder does not exist : %s\n", targetdir);
299 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
301 this.writeFile(targetdir + "/" + bn , html);
302 } catch (FileError e ) {
303 print("SaveHtml failed\n");
310 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
313 if (node.props.has_key("* xinclude")) {
314 ret.add(node.props.get("* xinclude"));
316 for (var i =0; i < node.items.size; i++) {
317 this.findxincludes(node.items.get(i), ret);
325 public override void findTransStrings(Node? node )
327 // iterate properties...
328 // use doubleStringProps
330 // flagging a translatable string..
331 // the code would use string _astring to indicate a translatable string
332 // the to use it it would do String.format(this._message, somedata);
334 // loop through and find string starting with '_'
339 var iter = node.props.map_iterator();
340 while (iter.next()) {
341 // key formats : XXXX
343 // string XXX - with type
344 // $ XXX - with flag (no type)
345 // $ string XXX - with flag
349 node.normalize_key(iter.get_key(), out kname, out kflag, out ktype);
353 var str = iter.get_value();
354 if (this.doubleStringProps.index_of(kname) > -1) {
355 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
356 this.transStrings.set(str,
357 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
362 if (ktype.down() == "string" && kname[0] == '_') {
363 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
364 this.transStrings.set(str,
365 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
374 // iterate children..
375 for (var i =0; i < node.items.size; i++) {
376 this.findTransStrings(node.items.get(i) );
382 public string transStringsToJs()
385 print("Roo.transStringsToJs()\n");
386 if (this.transStrings.size < 1) {
387 print("Roo.transStringsToJs() size < 1?\n");
392 var iter = this.transStrings.map_iterator();
393 while (iter.next()) {
394 kvs += (" '" + iter.get_value() + "' :" +
395 this.tree.quoteString(iter.get_key())
398 return " _strings : {\n" + string.joinv(",\n", kvs) + "\n" +
407 * javascript used in Webkit preview
410 public override string toSourcePreview()
412 print("toSourcePreview() - reset transStrings\n");
413 this.transStrings = new Gee.HashMap<string,string>();
416 print("to source preview\n");
417 if (this.tree == null) {
420 this.findTransStrings(this.tree);
421 var top = this.tree.fqn();
422 var xinc = new Gee.ArrayList<string>();
424 this.findxincludes(this.tree, xinc);
425 print("got %d xincludes\n", xinc.size);
426 var prefix_data = "";
427 if (xinc.size > 0 ) {
428 for(var i = 0; i < xinc.size; i++) {
429 print("check xinclude: %s\n", xinc.get(i));
430 var sf = this.project.getByName(xinc.get(i));
432 print("Failed to find file by name?\n");
437 sf.findTransStrings(sf.tree);
438 var xinc_str = sf.toSource();
441 //FileUtils.get_contents(js, out xinc_str);
442 prefix_data += "\n" + xinc_str + "\n";
450 //print(JSON.stringify(this.items, null,4));
453 print ("guessname returned false");
458 if (top.contains("Dialog")) {
459 return prefix_data + this.toSourceDialog(true);
462 if (top.contains("Modal")) {
463 return prefix_data + this.toSourceModal(true);
466 return prefix_data + this.toSourceLayout(true);
471 public override void setSource(string str) {}
473 * This needs to use some options on the project
474 * to determine how the file is output..
476 * At present we are hard coding it..
480 public override string toSource()
482 // dump the file tree back out to a string.
484 // we have 2 types = dialogs and components
488 if (this.tree == null) {
491 var top = this.tree.fqn();
495 // get the translatable strings..
499 if (top.contains("Dialog")) {
500 return this.toSourceDialog(false);
503 if (top.contains("Modal")) {
504 return this.toSourceModal(false);
506 return this.toSourceLayout(false);
509 eventually support 'classes??'
510 return this.toSourceStdClass();
515 public string outputHeader()
518 "//<script type=\"text/javascript\">",
520 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
524 var ret= string.joinv("\n",s);
525 var bits = this.name.split(".");
526 if (bits.length > 1) {
527 ret += "\nRoo.namespace(\'" +
528 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
532 /// genericlly used..
538 // a standard dialog module.
539 // fixme - this could be alot neater..
540 public string toSourceDialog(bool isPreview)
543 //var items = JSON.parse(JSON.stringify(this.items[0]));
546 var o = this.mungeToString(" ");
549 string[] adda = { " = {",
551 this.transStringsToJs() ,
556 " show : function(data, cb)",
558 " if (!this.dialog) {",
562 " this.callback = cb;",
563 " this.data = data;",
564 " this.dialog.show(this.data._el);",
566 " this.form.reset();",
567 " this.form.setValues(data);",
568 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
573 " create : function()",
575 " var _this = this;",
576 " this.dialog = Roo.factory("
584 return this.outputHeader() + "\n" +
585 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
592 public string toSourceModal(bool isPreview)
596 //var items = JSON.parse(JSON.stringify(this.items[0]));
597 var o = this.mungeToString(" ");
599 string[] adda = { " = {",
601 this.transStringsToJs() ,
606 " show : function(data, cb)",
608 " if (!this.dialog) {",
612 " this.callback = cb;",
613 " this.data = data;",
614 " this.dialog.show(this.data._el);",
616 " this.form.reset();",
617 " this.form.setValues(data);",
618 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
623 " create : function()",
625 " var _this = this;",
626 " this.dialog = Roo.factory("
634 return this.outputHeader() + "\n" +
635 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
642 public string pathToPart()
644 var dir = Path.get_basename(Path.get_dirname(this.path));
645 var ar = dir.split(".");
646 var modname = ar[ar.length-1];
648 // now we have the 'module name'..
649 var fbits = Path.get_basename(this.path).split(".");
652 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
653 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
654 npart = npart.substring(modname.length);
656 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
665 public string toSourceLayout(bool isPreview)
670 // topItem.region = 'center';
671 // topItem.background = false;
674 var o = this.mungeToString(" ");
675 var reg = new Regex("[^A-Za-z.]+");
677 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
679 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
684 // set to false to ensure this is the top level..
686 var topnode = this.tree.fqn();
687 print("topnode = %s\n", topnode);
688 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
689 topnode != "Roo.bootstrap.Body"
691 parent = "\"#bootstrap-body\"";
698 this.outputHeader() + "\n" +
700 this.name + " = new Roo.XComponent({\n" +
702 this.transStringsToJs() + "\n" +
704 " part : "+ this.pathToPart() + ",\n" +
705 /// critical used by builder to associate modules/parts/persm
706 " order : '" +modkey+"',\n" +
707 " region : '" + this.region +"',\n" +
708 " parent : "+ parent + ",\n" +
709 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
710 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
711 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
713 // " tree : function() { return this._tree(); },\n" + //BC
714 " _tree : function()\n" +
716 " var _this = this;\n" + // bc
717 " var MODULE = this;\n" + /// this looks like a better name.
718 " return " + o + ";" +
725 public new string? guessName (Node? ar) // turns the object into full name.
727 // eg. xns: Roo, xtype: XXX -> Roo.xxx
733 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
736 if ( ar.get("xtype").length < 1) {
740 var xtype = ar.get("xtype");
742 if (xtype[0] == '*') { // prefixes????
743 xtype = xtype.substring(1);
745 if (! Regex.match_simple("^Roo", xtype)) {
747 // already starts with roo...
751 var str = string.joinv(".", ret);
755 //Palete.Palete.factory("Roo").guessName(str);
761 string getHelpUrl(string cls)
763 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";