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");
216 if (this.tree == null) {
219 // now write the js file..
222 Regex regex = new Regex("\\.(bjs|js)$");
224 js = regex.replace(this.path,this.path.length , 0 , ".js");
225 } catch (RegexError e) {
227 print("count not make filename from path");
232 //var d = new Date();
233 var js_src = this.toSource();
234 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
236 this.writeFile(js, js_src);
237 } catch (FileError e ) {
238 print("Save failed\n");
240 // for bootstrap - we can write the HTML to the templates directory..
242 //var top = this.guessName(this.items[0]);
243 //print ("TOP = " + top)
253 public override void saveHTML ( string html )
256 var top = this.tree.fqn();
257 print ("TOP = " + top + "\n" );
258 if (top.index_of("Roo.bootstrap.") < 0 &&
259 top.index_of("Roo.mailer.") < 0
265 // now write the js file..
268 Regex regex = new Regex("\\.(bjs|js)$");
270 fn = regex.replace(this.path,this.path.length , 0 , ".html");
271 } catch (RegexError e) {
273 print("count not make filename from path");
276 var bn = GLib.Path.get_basename(fn);
277 var dn = GLib.Path.get_dirname(fn);
279 var targetdir = dn + (
280 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
283 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
284 print("Skip save - templates folder does not exist : %s\n", targetdir);
287 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
289 this.writeFile(targetdir + "/" + bn , html);
290 } catch (FileError e ) {
291 print("SaveHtml failed\n");
298 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
301 if (node.props.has_key("* xinclude")) {
302 ret.add(node.props.get("* xinclude"));
304 for (var i =0; i < node.items.size; i++) {
305 this.findxincludes(node.items.get(i), ret);
310 Gee.HashMap<string,string> transStrings; // map of md5 -> string..
313 public void findTransStrings(Node node )
315 // iterate properties...
316 // use doubleStringProps
318 // flagging a translatable string..
319 // the code would use string _astring to indicate a translatable string
320 // the to use it it would do String.format(this._message, somedata);
322 // loop through and find string starting with '_'
326 var iter = node.props.map_iterator();
327 while (iter.next()) {
328 // key formats : XXXX
330 // string XXX - with type
331 // $ XXX - with flag (no type)
332 // $ string XXX - with flag
336 node.normalize_key(iter.get_key(), out kname, out ktype, out kflag);
340 var str = iter.get_value();
341 if (this.doubleStringProps.index_of(kname) > -1) {
342 this.transStrings.set(str,
343 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
347 if (ktype == "string" && kname[0] == '_') {
348 this.transStrings.set(str,
349 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
358 // iterate children..
359 for (var i =0; i < node.items.size; i++) {
360 this.findTransStrings(node.items.get(i) );
366 * javascript used in Webkit preview
369 public override string toSourcePreview()
371 print("to source preview\n");
372 if (this.tree == null) {
375 var top = this.tree.fqn();
376 var xinc = new Gee.ArrayList<string>();
378 this.findxincludes(this.tree, xinc);
379 print("got %d xincludes\n", xinc.size);
380 var prefix_data = "";
381 if (xinc.size > 0 ) {
382 for(var i = 0; i < xinc.size; i++) {
383 print("check xinclude: %s\n", xinc.get(i));
384 var sf = this.project.getByName(xinc.get(i));
386 print("Failed to find file by name?\n");
391 var xinc_str = sf.toSource();
394 //FileUtils.get_contents(js, out xinc_str);
395 prefix_data += "\n" + xinc_str + "\n";
403 //print(JSON.stringify(this.items, null,4));
406 print ("guessname returned false");
411 if (top.contains("Dialog")) {
412 return prefix_data + this.toSourceDialog(true);
415 if (top.contains("Modal")) {
416 return prefix_data + this.toSourceModal(true);
419 return prefix_data + this.toSourceLayout(true);
426 * This needs to use some options on the project
427 * to determine how the file is output..
429 * At present we are hard coding it..
433 public override string toSource()
435 // dump the file tree back out to a string.
437 // we have 2 types = dialogs and components
439 this.transStrings = new Gee.HashMap<string,string>();
440 if (this.tree == null) {
443 var top = this.tree.fqn();
447 if (top.contains("Dialog")) {
448 return this.toSourceDialog(false);
451 if (top.contains("Modal")) {
452 return this.toSourceModal(false);
454 return this.toSourceLayout(false);
457 eventually support 'classes??'
458 return this.toSourceStdClass();
463 public string outputHeader()
466 "//<script type=\"text/javascript\">",
468 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
472 var ret= string.joinv("\n",s);
473 var bits = this.name.split(".");
474 if (bits.length > 1) {
475 ret += "\nRoo.namespace(\'" +
476 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
480 /// genericlly used..
486 // a standard dialog module.
487 // fixme - this could be alot neater..
488 public string toSourceDialog(bool isPreview)
491 //var items = JSON.parse(JSON.stringify(this.items[0]));
494 var o = this.mungeToString(" ");
497 string[] adda = { " = {",
502 " show : function(data, cb)",
504 " if (!this.dialog) {",
508 " this.callback = cb;",
509 " this.data = data;",
510 " this.dialog.show(this.data._el);",
512 " this.form.reset();",
513 " this.form.setValues(data);",
514 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
519 " create : function()",
521 " var _this = this;",
522 " this.dialog = Roo.factory("
530 return this.outputHeader() + "\n" +
531 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
538 public string toSourceModal(bool isPreview)
542 //var items = JSON.parse(JSON.stringify(this.items[0]));
543 var o = this.mungeToString(" ");
545 string[] adda = { " = {",
550 " show : function(data, cb)",
552 " if (!this.dialog) {",
556 " this.callback = cb;",
557 " this.data = data;",
558 " this.dialog.show(this.data._el);",
560 " this.form.reset();",
561 " this.form.setValues(data);",
562 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
567 " create : function()",
569 " var _this = this;",
570 " this.dialog = Roo.factory("
578 return this.outputHeader() + "\n" +
579 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
586 public string pathToPart()
588 var dir = Path.get_basename(Path.get_dirname(this.path));
589 var ar = dir.split(".");
590 var modname = ar[ar.length-1];
592 // now we have the 'module name'..
593 var fbits = Path.get_basename(this.path).split(".");
596 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
597 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
598 npart = npart.substring(modname.length);
600 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
609 public string toSourceLayout(bool isPreview)
614 // topItem.region = 'center';
615 // topItem.background = false;
618 var o = this.mungeToString(" ");
619 var reg = new Regex("[^A-Za-z.]+");
621 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
623 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
628 // set to false to ensure this is the top level..
630 var topnode = this.tree.fqn();
631 print("topnode = %s\n", topnode);
632 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
633 topnode != "Roo.bootstrap.Body"
635 parent = "\"#bootstrap-body\"";
642 this.outputHeader() + "\n" +
644 this.name + " = new Roo.XComponent({\n" +
645 " part : "+ this.pathToPart() + ",\n" +
646 /// critical used by builder to associate modules/parts/persm
647 " order : '" +modkey+"',\n" +
648 " region : '" + this.region +"',\n" +
649 " parent : "+ parent + ",\n" +
650 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
651 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
652 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
654 // " tree : function() { return this._tree(); },\n" + //BC
655 " _tree : function()\n" +
657 " var _this = this;\n" + // bc
658 " var MODULE = this;\n" + /// this looks like a better name.
659 " return " + o + ";" +
667 public new string? guessName (Node? ar) // turns the object into full name.
669 // eg. xns: Roo, xtype: XXX -> Roo.xxx
675 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
678 if ( ar.get("xtype").length < 1) {
682 var xtype = ar.get("xtype");
684 if (xtype[0] == '*') { // prefixes????
685 xtype = xtype.substring(1);
687 if (! Regex.match_simple("^Roo", xtype)) {
689 // already starts with roo...
693 var str = string.joinv(".", ret);
697 //Palete.Palete.factory("Roo").guessName(str);
703 string getHelpUrl(string cls)
705 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";