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));
148 * old code had broken xtypes and used arrays differently,
149 * this code should try and clean it up..
153 fixItems : function(node, fixthis)
157 var fn = this.guessName(node);
158 //print("guessname got " + fn);
160 var bits = fn.split('.');
161 node.xtype = bits.pop();
162 node['|xns'] = bits.join('.');
169 if (!node.items || !node.items.length) {
175 node.items.forEach(function(i) {
179 _this.fixItems(i, true);
180 if (i.xtype == 'Array') {
188 if (!aitems.length) {
192 aitems.forEach(function(i) {
194 if (!i.items || !i.items.length) {
197 var prop = i['*prop'] + '[]';
199 i.items.forEach(function(c) {
218 public override void save()
221 print("--- JsRender.Roo.save");
222 print("save() - reset transStrings\n");
223 this.transStrings = new Gee.HashMap<string,string>();
224 this.findTransStrings(this.tree);
229 if (this.tree == null) {
232 // now write the js file..
235 Regex regex = new Regex("\\.(bjs|js)$");
237 js = regex.replace(this.path,this.path.length , 0 , ".js");
238 } catch (RegexError e) {
240 print("count not make filename from path");
245 //var d = new Date();
246 var js_src = this.toSource();
247 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
249 this.writeFile(js, js_src);
250 } catch (FileError e ) {
251 print("Save failed\n");
253 // for bootstrap - we can write the HTML to the templates directory..
255 //var top = this.guessName(this.items[0]);
256 //print ("TOP = " + top)
266 public override void saveHTML ( string html )
269 var top = this.tree.fqn();
270 print ("TOP = " + top + "\n" );
271 if (top.index_of("Roo.bootstrap.") < 0 &&
272 top.index_of("Roo.mailer.") < 0
278 //now write the js file..
281 Regex regex = new Regex("\\.(bjs|js)$");
283 fn = regex.replace(this.path,this.path.length , 0 , ".html");
284 } catch (RegexError e) {
286 print("count not make filename from path");
289 var bn = GLib.Path.get_basename(fn);
290 var dn = GLib.Path.get_dirname(fn);
292 var targetdir = dn + (
293 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
296 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
297 print("Skip save - templates folder does not exist : %s\n", targetdir);
300 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
302 this.writeFile(targetdir + "/" + bn , html);
303 } catch (FileError e ) {
304 print("SaveHtml failed\n");
311 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
314 if (node.props.has_key("* xinclude")) {
315 ret.add(node.props.get("* xinclude"));
317 for (var i =0; i < node.items.size; i++) {
318 this.findxincludes(node.items.get(i), ret);
326 public override void findTransStrings(Node? node )
328 // iterate properties...
329 // use doubleStringProps
331 // flagging a translatable string..
332 // the code would use string _astring to indicate a translatable string
333 // the to use it it would do String.format(this._message, somedata);
335 // loop through and find string starting with '_'
340 var iter = node.props.map_iterator();
341 while (iter.next()) {
342 // key formats : XXXX
344 // string XXX - with type
345 // $ XXX - with flag (no type)
346 // $ string XXX - with flag
350 node.normalize_key(iter.get_key(), out kname, out kflag, out ktype);
354 var str = iter.get_value();
355 if (this.doubleStringProps.index_of(kname) > -1) {
356 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
357 this.transStrings.set(str,
358 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
363 if (ktype.down() == "string" && kname[0] == '_') {
364 print("flag=%s type=%s name=%s : %s\n", kflag,ktype,kname,str);
365 this.transStrings.set(str,
366 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
375 // iterate children..
376 for (var i =0; i < node.items.size; i++) {
377 this.findTransStrings(node.items.get(i) );
383 public string transStringsToJs()
386 print("Roo.transStringsToJs()\n");
387 if (this.transStrings.size < 1) {
388 print("Roo.transStringsToJs() size < 1?\n");
393 var iter = this.transStrings.map_iterator();
394 while (iter.next()) {
395 kvs += (" '" + iter.get_value() + "' :" +
396 this.tree.quoteString(iter.get_key())
399 return " _strings : {\n" + string.joinv(",\n", kvs) + "\n" +
408 * javascript used in Webkit preview
411 public override string toSourcePreview()
413 print("toSourcePreview() - reset transStrings\n");
414 this.transStrings = new Gee.HashMap<string,string>();
417 print("to source preview\n");
418 if (this.tree == null) {
421 this.findTransStrings(this.tree);
422 var top = this.tree.fqn();
423 var xinc = new Gee.ArrayList<string>();
425 this.findxincludes(this.tree, xinc);
426 print("got %d xincludes\n", xinc.size);
427 var prefix_data = "";
428 if (xinc.size > 0 ) {
429 for(var i = 0; i < xinc.size; i++) {
430 print("check xinclude: %s\n", xinc.get(i));
431 var sf = this.project.getByName(xinc.get(i));
433 print("Failed to find file by name?\n");
438 sf.findTransStrings(sf.tree);
439 var xinc_str = sf.toSource();
442 //FileUtils.get_contents(js, out xinc_str);
443 prefix_data += "\n" + xinc_str + "\n";
451 //print(JSON.stringify(this.items, null,4));
454 print ("guessname returned false");
459 if (top.contains("Dialog")) {
460 return prefix_data + this.toSourceDialog(true);
463 if (top.contains("Modal")) {
464 return prefix_data + this.toSourceModal(true);
467 return prefix_data + this.toSourceLayout(true);
472 public override void setSource(string str) {}
474 * This needs to use some options on the project
475 * to determine how the file is output..
477 * At present we are hard coding it..
481 public override string toSource()
483 // dump the file tree back out to a string.
485 // we have 2 types = dialogs and components
489 if (this.tree == null) {
492 var top = this.tree.fqn();
496 // get the translatable strings..
500 if (top.contains("Dialog")) {
501 return this.toSourceDialog(false);
504 if (top.contains("Modal")) {
505 return this.toSourceModal(false);
507 return this.toSourceLayout(false);
510 eventually support 'classes??'
511 return this.toSourceStdClass();
516 public string outputHeader()
519 "//<script type=\"text/javascript\">",
521 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
525 var ret= string.joinv("\n",s);
526 var bits = this.name.split(".");
527 if (bits.length > 1) {
528 ret += "\nRoo.namespace(\'" +
529 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
533 /// genericlly used..
539 // a standard dialog module.
540 // fixme - this could be alot neater..
541 public string toSourceDialog(bool isPreview)
544 //var items = JSON.parse(JSON.stringify(this.items[0]));
547 var o = this.mungeToString(" ");
550 string[] adda = { " = {",
552 this.transStringsToJs() ,
557 " show : function(data, cb)",
559 " if (!this.dialog) {",
563 " this.callback = cb;",
564 " this.data = data;",
565 " this.dialog.show(this.data._el);",
567 " this.form.reset();",
568 " this.form.setValues(data);",
569 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
574 " create : function()",
576 " var _this = this;",
577 " this.dialog = Roo.factory("
585 return this.outputHeader() + "\n" +
586 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
593 public string toSourceModal(bool isPreview)
597 //var items = JSON.parse(JSON.stringify(this.items[0]));
598 var o = this.mungeToString(" ");
600 string[] adda = { " = {",
602 this.transStringsToJs() ,
607 " show : function(data, cb)",
609 " if (!this.dialog) {",
613 " this.callback = cb;",
614 " this.data = data;",
615 " this.dialog.show(this.data._el);",
617 " this.form.reset();",
618 " this.form.setValues(data);",
619 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
624 " create : function()",
626 " var _this = this;",
627 " this.dialog = Roo.factory("
635 return this.outputHeader() + "\n" +
636 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
643 public string pathToPart()
645 var dir = Path.get_basename(Path.get_dirname(this.path));
646 var ar = dir.split(".");
647 var modname = ar[ar.length-1];
649 // now we have the 'module name'..
650 var fbits = Path.get_basename(this.path).split(".");
653 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
654 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
655 npart = npart.substring(modname.length);
657 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
666 public string toSourceLayout(bool isPreview)
671 // topItem.region = 'center';
672 // topItem.background = false;
675 var o = this.mungeToString(" ");
676 var reg = new Regex("[^A-Za-z.]+");
678 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
680 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
685 // set to false to ensure this is the top level..
687 var topnode = this.tree.fqn();
688 print("topnode = %s\n", topnode);
689 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
690 topnode != "Roo.bootstrap.Body"
692 parent = "\"#bootstrap-body\"";
699 this.outputHeader() + "\n" +
701 this.name + " = new Roo.XComponent({\n" +
703 this.transStringsToJs() + "\n" +
705 " part : "+ this.pathToPart() + ",\n" +
706 /// critical used by builder to associate modules/parts/persm
707 " order : '" +modkey+"',\n" +
708 " region : '" + this.region +"',\n" +
709 " parent : "+ parent + ",\n" +
710 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
711 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
712 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
714 // " tree : function() { return this._tree(); },\n" + //BC
715 " _tree : function()\n" +
717 " var _this = this;\n" + // bc
718 " var MODULE = this;\n" + /// this looks like a better name.
719 " return " + o + ";" +
726 public new string? guessName (Node? ar) // turns the object into full name.
728 // eg. xns: Roo, xtype: XXX -> Roo.xxx
734 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
737 if ( ar.get("xtype").length < 1) {
741 var xtype = ar.get("xtype");
743 if (xtype[0] == '*') { // prefixes????
744 xtype = xtype.substring(1);
746 if (! Regex.match_simple("^Roo", xtype)) {
748 // already starts with roo...
752 var str = string.joinv(".", ret);
756 //Palete.Palete.factory("Roo").guessName(str);
762 string getHelpUrl(string cls)
764 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";