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",
59 for (var i=0;i<dsp.length;i++) {
60 this.doubleStringProps.add(dsp[i]);
67 setNSID : function(id)
70 this.items[0]['|module'] = id;
82 public override void removeFiles() {
83 var html = GLib.Path.get_dirname(this.path) +"/templates/" + name + ".html";
84 if (FileUtils.test(html, FileTest.EXISTS)) {
85 GLib.FileUtils.remove(html);
87 var js = GLib.Path.get_dirname(this.path) +"/" + name + ".html";
88 if (FileUtils.test(js, FileTest.EXISTS)) {
89 GLib.FileUtils.remove(js);
93 public override void loadItems() throws GLib.Error // : function(cb, sync) == original was async.
98 if (this.tree != null) {
101 print("load " + this.path);
103 var pa = new Json.Parser();
104 pa.load_from_file(this.path);
105 var node = pa.get_root();
107 if (node.get_node_type () != Json.NodeType.OBJECT) {
108 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
110 var obj = node.get_object ();
113 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
114 this.name = this.jsonHasOrEmpty(obj, "name");
115 this.parent = this.jsonHasOrEmpty(obj, "parent");
116 this.permname = this.jsonHasOrEmpty(obj, "permname");
117 this.title = this.jsonHasOrEmpty(obj, "title");
118 this.modOrder = this.jsonHasOrEmpty(obj, "modOrder");
120 var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
121 bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
124 // load items[0] ??? into tree...
125 if (obj.has_member("items")
127 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
129 obj.get_array_member("items").get_length() > 0
131 this.tree = new Node();
132 var ar = obj.get_array_member("items");
133 var tree_base = ar.get_object_element(0);
134 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
141 * old code had broken xtypes and used arrays differently,
142 * this code should try and clean it up..
146 fixItems : function(node, fixthis)
150 var fn = this.guessName(node);
151 //print("guessname got " + fn);
153 var bits = fn.split('.');
154 node.xtype = bits.pop();
155 node['|xns'] = bits.join('.');
162 if (!node.items || !node.items.length) {
168 node.items.forEach(function(i) {
172 _this.fixItems(i, true);
173 if (i.xtype == 'Array') {
181 if (!aitems.length) {
185 aitems.forEach(function(i) {
187 if (!i.items || !i.items.length) {
190 var prop = i['*prop'] + '[]';
192 i.items.forEach(function(c) {
211 public override void save()
214 print("--- JsRender.Roo.save");
216 this.transStrings = new Gee.HashMap<string,string>();
217 this.findTransStrings(this.tree);
222 if (this.tree == null) {
225 // now write the js file..
228 Regex regex = new Regex("\\.(bjs|js)$");
230 js = regex.replace(this.path,this.path.length , 0 , ".js");
231 } catch (RegexError e) {
233 print("count not make filename from path");
238 //var d = new Date();
239 var js_src = this.toSource();
240 //print("TO SOURCE in " + ((new Date()) - d) + "ms");
242 this.writeFile(js, js_src);
243 } catch (FileError e ) {
244 print("Save failed\n");
246 // for bootstrap - we can write the HTML to the templates directory..
248 //var top = this.guessName(this.items[0]);
249 //print ("TOP = " + top)
259 public override void saveHTML ( string html )
262 var top = this.tree.fqn();
263 print ("TOP = " + top + "\n" );
264 if (top.index_of("Roo.bootstrap.") < 0 &&
265 top.index_of("Roo.mailer.") < 0
271 //now write the js file..
274 Regex regex = new Regex("\\.(bjs|js)$");
276 fn = regex.replace(this.path,this.path.length , 0 , ".html");
277 } catch (RegexError e) {
279 print("count not make filename from path");
282 var bn = GLib.Path.get_basename(fn);
283 var dn = GLib.Path.get_dirname(fn);
285 var targetdir = dn + (
286 top.index_of("Roo.mailer.") < 0 ? "/templates" : "" );
289 if (!FileUtils.test(targetdir, FileTest.IS_DIR)) {
290 print("Skip save - templates folder does not exist : %s\n", targetdir);
293 print("SAVE HTML -- %s\n%s\n",targetdir + "/" + bn, html);
295 this.writeFile(targetdir + "/" + bn , html);
296 } catch (FileError e ) {
297 print("SaveHtml failed\n");
304 public Gee.ArrayList<string> findxincludes(Node node, Gee.ArrayList<string> ret)
307 if (node.props.has_key("* xinclude")) {
308 ret.add(node.props.get("* xinclude"));
310 for (var i =0; i < node.items.size; i++) {
311 this.findxincludes(node.items.get(i), ret);
319 public void findTransStrings(Node node )
321 // iterate properties...
322 // use doubleStringProps
324 // flagging a translatable string..
325 // the code would use string _astring to indicate a translatable string
326 // the to use it it would do String.format(this._message, somedata);
328 // loop through and find string starting with '_'
333 var iter = node.props.map_iterator();
334 while (iter.next()) {
335 // key formats : XXXX
337 // string XXX - with type
338 // $ XXX - with flag (no type)
339 // $ string XXX - with flag
343 node.normalize_key(iter.get_key(), out kname, out kflag, out ktype);
347 var str = iter.get_value();
348 if (this.doubleStringProps.index_of(kname) > -1) {
349 this.transStrings.set(str,
350 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
354 print("flag=%s type=%s name=%s\n", kflag,ktype,kname);
355 if (ktype.ascii_casecmp("string") == 0 && kname[0] == '_') {
356 this.transStrings.set(str,
357 GLib.Checksum.compute_for_string (ChecksumType.MD5, str)
366 // iterate children..
367 for (var i =0; i < node.items.size; i++) {
368 this.findTransStrings(node.items.get(i) );
374 public string transStringsToJs()
376 if (this.transStrings.size < 1) {
379 string ret = " strings : {\n";
381 var iter = this.transStrings.map_iterator();
382 while (iter.next()) {
383 kvs += (" '" + iter.get_value() + "' :" +
384 this.tree.quoteString(iter.get_key())
387 return " strings : {\n" + string.joinv(",\n", kvs) + "\n" +
396 * javascript used in Webkit preview
399 public override string toSourcePreview()
401 print("to source preview\n");
402 if (this.tree == null) {
405 var top = this.tree.fqn();
406 var xinc = new Gee.ArrayList<string>();
408 this.findxincludes(this.tree, xinc);
409 print("got %d xincludes\n", xinc.size);
410 var prefix_data = "";
411 if (xinc.size > 0 ) {
412 for(var i = 0; i < xinc.size; i++) {
413 print("check xinclude: %s\n", xinc.get(i));
414 var sf = this.project.getByName(xinc.get(i));
416 print("Failed to find file by name?\n");
421 var xinc_str = sf.toSource();
424 //FileUtils.get_contents(js, out xinc_str);
425 prefix_data += "\n" + xinc_str + "\n";
433 //print(JSON.stringify(this.items, null,4));
436 print ("guessname returned false");
441 if (top.contains("Dialog")) {
442 return prefix_data + this.toSourceDialog(true);
445 if (top.contains("Modal")) {
446 return prefix_data + this.toSourceModal(true);
449 return prefix_data + this.toSourceLayout(true);
456 * This needs to use some options on the project
457 * to determine how the file is output..
459 * At present we are hard coding it..
463 public override string toSource()
465 // dump the file tree back out to a string.
467 // we have 2 types = dialogs and components
471 if (this.tree == null) {
474 var top = this.tree.fqn();
478 // get the translatable strings..
482 if (top.contains("Dialog")) {
483 return this.toSourceDialog(false);
486 if (top.contains("Modal")) {
487 return this.toSourceModal(false);
489 return this.toSourceLayout(false);
492 eventually support 'classes??'
493 return this.toSourceStdClass();
498 public string outputHeader()
501 "//<script type=\"text/javascript\">",
503 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
507 var ret= string.joinv("\n",s);
508 var bits = this.name.split(".");
509 if (bits.length > 1) {
510 ret += "\nRoo.namespace(\'" +
511 this.name.substring(0, this.name.length - (bits[bits.length-1].length + 1)) +
515 /// genericlly used..
521 // a standard dialog module.
522 // fixme - this could be alot neater..
523 public string toSourceDialog(bool isPreview)
526 //var items = JSON.parse(JSON.stringify(this.items[0]));
529 var o = this.mungeToString(" ");
532 string[] adda = { " = {",
534 this.transStringsToJs() ,
539 " show : function(data, cb)",
541 " if (!this.dialog) {",
545 " this.callback = cb;",
546 " this.data = data;",
547 " this.dialog.show(this.data._el);",
549 " this.form.reset();",
550 " this.form.setValues(data);",
551 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
556 " create : function()",
558 " var _this = this;",
559 " this.dialog = Roo.factory("
567 return this.outputHeader() + "\n" +
568 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
575 public string toSourceModal(bool isPreview)
579 //var items = JSON.parse(JSON.stringify(this.items[0]));
580 var o = this.mungeToString(" ");
582 string[] adda = { " = {",
584 this.transStringsToJs() ,
589 " show : function(data, cb)",
591 " if (!this.dialog) {",
595 " this.callback = cb;",
596 " this.data = data;",
597 " this.dialog.show(this.data._el);",
599 " this.form.reset();",
600 " this.form.setValues(data);",
601 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
606 " create : function()",
608 " var _this = this;",
609 " this.dialog = Roo.factory("
617 return this.outputHeader() + "\n" +
618 this.name + string.joinv("\n", adda) + o + string.joinv("\n", addb);
625 public string pathToPart()
627 var dir = Path.get_basename(Path.get_dirname(this.path));
628 var ar = dir.split(".");
629 var modname = ar[ar.length-1];
631 // now we have the 'module name'..
632 var fbits = Path.get_basename(this.path).split(".");
635 var npart = fbits[fbits.length - 2]; // this should be 'AdminProjectManager' for example...
636 if (modname.length < npart.length && npart.substring(0, modname.length) == modname) {
637 npart = npart.substring(modname.length);
639 return "[" + this.tree.quoteString(modname) + ", " + this.tree.quoteString(npart) + " ]";
648 public string toSourceLayout(bool isPreview)
653 // topItem.region = 'center';
654 // topItem.background = false;
657 var o = this.mungeToString(" ");
658 var reg = new Regex("[^A-Za-z.]+");
660 string modkey = this.modOrder + "-" + reg.replace(this.name, this.name.length, 0 , "-");
662 string parent = (this.parent.length > 0 ? "'" + this.parent + "'" : "false");
667 // set to false to ensure this is the top level..
669 var topnode = this.tree.fqn();
670 print("topnode = %s\n", topnode);
671 if (GLib.Regex.match_simple("^Roo\\.bootstrap\\.",topnode) &&
672 topnode != "Roo.bootstrap.Body"
674 parent = "\"#bootstrap-body\"";
681 this.outputHeader() + "\n" +
683 this.name + " = new Roo.XComponent({\n" +
685 this.transStringsToJs() + "\n" +
687 " part : "+ this.pathToPart() + ",\n" +
688 /// critical used by builder to associate modules/parts/persm
689 " order : '" +modkey+"',\n" +
690 " region : '" + this.region +"',\n" +
691 " parent : "+ parent + ",\n" +
692 " name : " + this.tree.quoteString(this.title.length > 0 ? this.title : "unnamed module") + ",\n" +
693 " disabled : " + (this.disabled ? "true" : "false") +", \n" +
694 " permname : '" + (this.permname.length > 0 ? this.permname : "") +"', \n" +
696 // " tree : function() { return this._tree(); },\n" + //BC
697 " _tree : function()\n" +
699 " var _this = this;\n" + // bc
700 " var MODULE = this;\n" + /// this looks like a better name.
701 " return " + o + ";" +
708 public new string? guessName (Node? ar) // turns the object into full name.
710 // eg. xns: Roo, xtype: XXX -> Roo.xxx
716 ret += (ar.get("|xns").length < 1 ? "Roo": ar.get("|xns"));
719 if ( ar.get("xtype").length < 1) {
723 var xtype = ar.get("xtype");
725 if (xtype[0] == '*') { // prefixes????
726 xtype = xtype.substring(1);
728 if (! Regex.match_simple("^Roo", xtype)) {
730 // already starts with roo...
734 var str = string.joinv(".", ret);
738 //Palete.Palete.factory("Roo").guessName(str);
744 string getHelpUrl(string cls)
746 return "http://www.roojs.com/roojs1/docs/symbols/" + cls + ".html";