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..
48 string[] dsp = { "title",
57 for (var i=0;i<dsp.length;i++) {
58 this.doubleStringProps.add(dsp[i]);
65 setNSID : function(id)
68 this.items[0]['|module'] = id;
80 public override void removeFiles() {
81 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
82 if (FileUtils.test(html, FileTest.EXISTS)) {
83 GLib.FileUtils.remove(html);
85 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
86 if (FileUtils.test(js, FileTest.EXISTS)) {
87 GLib.FileUtils.remove(js);
91 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
96 if (this.tree != null) {
99 print("load " + this.path);
101 var pa = new Json.Parser();
102 pa.load_from_file(this.path);
103 var node = pa.get_root();
105 if (node.get_node_type () != Json.NodeType.OBJECT) {
106 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
108 var obj = node.get_object ();
111 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
112 this.name = this.jsonHasOrEmpty(obj, "name");
113 this.parent = this.jsonHasOrEmpty(obj, "parent");
114 this.permname = this.jsonHasOrEmpty(obj, "permname");
115 this.title = this.jsonHasOrEmpty(obj, "title");
116 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
118 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
119 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
122 // load items[0] ??? into tree...
123 if (obj.has_member("items")
125 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
127 obj.get_array_member("items").get_length() > 0
129 this.tree = new Node();
130 var ar = obj.get_array_member("items");
131 var tree_base = ar.get_object_element(0);
132 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
139 * old code had broken xtypes and used arrays differently,
140 * this code should try and clean it up..
144 fixItems : function(node, fixthis)
148 var fn = this.guessName(node);
149 //print("guessname got " + fn);
151 var bits = fn.split('.');
152 node.xtype = bits.pop();
153 node['|xns'] = bits.join('.');
160 if (!node.items || !node.items.length) {
166 node.items.forEach(function(i) {
170 _this.fixItems(i, true);
171 if (i.xtype == 'Array') {
179 if (!aitems.length) {
183 aitems.forEach(function(i) {
185 if (!i.items || !i.items.length) {
188 var prop = i['*prop'] + '[]';
190 i.items.forEach(function(c) {
209 public override void save()
212 print("--- JsRender.Roo.save");
214 this.transStrings = new Gee.HashMap<string,string>();
215 this.findTransStrings(this.tree);
220 if (this.tree == null) {
223 // now write the js file..
226 Regex regex = new Regex("\\.(bjs|js)$");
228 js = regex.replace(this.path,this.path.length , 0 , ".js");
229 } catch (RegexError e) {
231 print("count not make filename from path");
236 //var d = new Date();
237 var js_src = this.toSource();
238 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
240 this.writeFile(js, js_src);
241 } catch (FileError e ) {
242 print("Save failed\n");
244 // for bootstrap - we can write the HTML to the templates directory..
246 //var top = this.guessName(this.items[0]);
247 //print ("TOP = " + top)
257 public override void saveHTML ( string html )
260 var top = this.tree.fqn();
261 print ("TOP = " + top + "\n" );
262 if (top.index_of("Roo.bootstrap.") < 0 &&
263 top.index_of("Roo.mailer.") < 0
269 //now write the js file..
272 Regex regex = new Regex("\\.(bjs|js)$");
274 fn = regex.replace(this.path,this.path.length , 0 , ".html");
275 } catch (RegexError e) {
277 print("count not make filename from path");
280 var bn = GLib.Path.get_basename(fn);
281 var dn = GLib.Path.get_dirname(fn);
283 var targetdir = dn + (
284 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
287 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
288 print("Skip save - templates folder does not exist : %s\n", targetdir);
291 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
293 this.writeFile(targetdir + "/" + bn , html);
294 } catch (FileError e ) {
295 print("SaveHtml failed\n");
302 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
305 if (node.props.has_key("* xinclude")) {
306 ret.add(node.props.get("* xinclude"));
308 for (var i =0; i < node.items.size; i++) {
309 this.findxincludes(node.items.get(i), ret);
317 public void findTransStrings(Node node )
319 // iterate properties...
320 // use doubleStringProps
322 // flagging a translatable string..
323 // the code would use string _astring to indicate a translatable string
324 // the to use it it would do String.format(this._message, somedata);
326 // loop through and find string starting with '_'
331 var iter = node.props.map_iterator();
332 while (iter.next()) {
333 // key formats : XXXX
335 // string XXX - with type
336 // $ XXX - with flag (no type)
337 // $ string XXX - with flag
341 node.normalize_key(iter.get_key(), out kname, out kflag, out ktype);
345 var str = iter.get_value();
346 if (this.doubleStringProps.index_of(kname) > -1) {
347 this.transStrings.set(str,
348 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
352 print("flag=%s type=%s name=%s\n", kflag,ktype,kname);
353 if (ktype.ascii_casecmp("string") == 0 && kname[0] == '_') {
354 this.transStrings.set(str,
355 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
364 // iterate children..
365 for (var i =0; i < node.items.size; i++) {
366 this.findTransStrings(node.items.get(i) );
372 public string transStringsToJs()
374 if (this.transStrings.size < 1) {
377 string ret = " strings : {\n";
379 var iter = this.transStrings.map_iterator();
380 while (iter.next()) {
381 kvs += " " + (this.tree.quoteString(iter.get_value()) + ":" +
382 this.tree.quoteString(iter.get_key())
385 return " strings : {\n " + string.joinv(",\n", kvs) + "\n" +
394 * javascript used in Webkit preview
397 public override string toSourcePreview()
399 print("to source preview\n");
400 if (this.tree == null) {
403 var top = this.tree.fqn();
404 var xinc = new Gee.ArrayList<string>();
406 this.findxincludes(this.tree, xinc);
407 print("got %d xincludes\n", xinc.size);
408 var prefix_data = "";
409 if (xinc.size > 0 ) {
410 for(var i = 0; i < xinc.size; i++) {
411 print("check xinclude: %s\n", xinc.get(i));
412 var sf = this.project.getByName(xinc.get(i));
414 print("Failed to find file by name?\n");
419 var xinc_str = sf.toSource();
422 //FileUtils.get_contents(js, out xinc_str);
423 prefix_data += "\n" + xinc_str + "\n";
431 //print(JSON.stringify(this.items, null,4));
434 print ("guessname returned false");
439 if (top.contains("Dialog")) {
440 return prefix_data + this.toSourceDialog(true);
443 if (top.contains("Modal")) {
444 return prefix_data + this.toSourceModal(true);
447 return prefix_data + this.toSourceLayout(true);
454 * This needs to use some options on the project
455 * to determine how the file is output..
457 * At present we are hard coding it..
461 public override string toSource()
463 // dump the file tree back out to a string.
465 // we have 2 types = dialogs and components
469 if (this.tree == null) {
472 var top = this.tree.fqn();
476 // get the translatable strings..
480 if (top.contains("Dialog")) {
481 return this.toSourceDialog(false);
484 if (top.contains("Modal")) {
485 return this.toSourceModal(false);
487 return this.toSourceLayout(false);
490 eventually support 'classes??'
491 return this.toSourceStdClass();
496 public string outputHeader()
499 "//<script type=\"text/javascript\">",
501 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
505 var ret= string.joinv("\n",s);
506 var bits = this.name.split(".");
507 if (bits.length > 1) {
508 ret += "\nRoo.namespace(\'" +
509 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
513 /// genericlly used..
519 // a standard dialog module.
520 // fixme - this could be alot neater..
521 public string toSourceDialog(bool isPreview)
524 //var items = JSON.parse(JSON.stringify(this.items[0]));
527 var o = this.mungeToString(" ");
530 string[] adda = { " = {",
532 this.transStringsToJs() ,
537 " show : function(data, cb)",
539 " if (!this.dialog) {",
543 " this.callback = cb;",
544 " this.data = data;",
545 " this.dialog.show(this.data._el);",
547 " this.form.reset();",
548 " this.form.setValues(data);",
549 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
554 " create : function()",
556 " var _this = this;",
557 " this.dialog = Roo.factory("
565 return this.outputHeader() + "\n" +
566 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
573 public string toSourceModal(bool isPreview)
577 //var items = JSON.parse(JSON.stringify(this.items[0]));
578 var o = this.mungeToString(" ");
580 string[] adda = { " = {",
582 this.transStringsToJs() ,
587 " show : function(data, cb)",
589 " if (!this.dialog) {",
593 " this.callback = cb;",
594 " this.data = data;",
595 " this.dialog.show(this.data._el);",
597 " this.form.reset();",
598 " this.form.setValues(data);",
599 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
604 " create : function()",
606 " var _this = this;",
607 " this.dialog = Roo.factory("
615 return this.outputHeader() + "\n" +
616 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
623 public string pathToPart()
625 var dir = Path.get_basename(Path.get_dirname(this.path));
626 var ar = dir.split(".");
627 var modname = ar[ar.length-1];
629 // now we have the 'module name'..
630 var fbits = Path.get_basename(this.path).split(".");
633 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
634 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
635 npart = npart.substring(modname.length);
637 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
646 public string toSourceLayout(bool isPreview)
651 // topItem.region = 'center';
652 // topItem.background = false;
655 var o = this.mungeToString(" ");
656 var reg = new Regex("[^A-Za-z.]+");
658 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
660 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
665 // set to false to ensure this is the top level..
667 var topnode = this.tree.fqn();
668 print("topnode = %s\n", topnode);
669 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
670 topnode != "Roo.bootstrap.Body"
672 parent = "\"#bootstrap-body\"";
679 this.outputHeader() + "\n" +
681 this.name + " = new Roo.XComponent({\n" +
683 this.transStringsToJs() ,
685 " part : "+ this.pathToPart() + ",\n" +
686 /// critical used by builder to associate modules/parts/persm
687 " order : '" +modkey+"',\n" +
688 " region : '" + this.region +"',\n" +
689 " parent : "+ parent + ",\n" +
690 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
691 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
692 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
694 // " tree : function() { return this._tree(); },\n" + //BC
695 " _tree : function()\n" +
697 " var _this = this;\n" + // bc
698 " var MODULE = this;\n" + /// this looks like a better name.
699 " return " + o + ";" +
706 public new string? guessName (Node? ar) // turns the object into full name.
708 // eg. xns: Roo, xtype: XXX -> Roo.xxx
714 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
717 if ( ar.get("xtype").length < 1) {
721 var xtype = ar.get("xtype");
723 if (xtype[0] == '*') { // prefixes????
724 xtype = xtype.substring(1);
726 if (! Regex.match_simple("^Roo", xtype)) {
728 // already starts with roo...
732 var str = string.joinv(".", ret);
736 //Palete.Palete.factory("Roo").guessName(str);
742 string getHelpUrl(string cls)
744 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";