1 //<Script type="text/javascript">
6 public errordomain Error {
11 public abstract class JsRender : Object {
13 * @cfg {Array} doubleStringProps list of properties that can be double quoted.
15 public Gee.ArrayList<string> doubleStringProps;
18 public string name; // is the JS name of the file.
19 public string fullname;
20 public string path; // is the full path to the file.
21 public string parent; // JS parent.
22 public string region; // RooJS - insert region.
24 public string title; // a title.. ?? nickname.. ??? -
25 public string build_module; // module to build if we compile (or are running tests...)
28 public string permname;
29 public string language;
30 public string modOrder;
32 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
34 public Project.Project project;
35 //Project : false, // link to container project!
37 public Node tree; // the tree of nodes.
39 public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
41 public bool hasParent;
43 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
46 public signal void changed (Node? node, string source);
52 //public Xcls_Editor editor;
56 public JsRender(Project.Project project, string path) {
58 this.cn = new GLib.List<JsRender>();
60 this.project = project;
61 this.hasParent = false;
69 print("JsRender.cto() - reset transStrings\n");
70 this.transStrings = new Gee.HashMap<string,string> ();
72 // should use basename reallly...
74 var ar = this.path.split("/");
75 // name is in theory filename without .bjs (or .js eventually...)
77 Regex regex = new Regex ("\\.(bjs|js)$");
79 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
83 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
85 this.doubleStringProps = new Gee.ArrayList<string>();
89 public void renameTo(string name)
92 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
93 if (FileUtils.test(bjs, FileTest.EXISTS)) {
94 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
96 GLib.FileUtils.remove(this.path);
98 // remove other files?
107 // not sure why xt is needed... -> project contains xtype..
109 public static JsRender factory(string xt, Project.Project project, string path)
114 return new Gtk(project, path);
116 return new Roo(project, path);
118 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
122 public string toJsonString()
124 var generator = new Json.Generator ();
125 generator.indent = 4;
126 generator.pretty = true;
127 var node = new Json.Node(Json.NodeType.OBJECT);
128 node.set_object(this.toJsonObject());
129 generator.set_root(node);
130 return generator.to_data(null);
133 public string nickType()
135 var ar = this.name.split(".");
137 for (var i =0; i < ar.length -1; i++) {
140 return string.joinv(".", ret);
143 public string nickName()
145 var ar = this.name.split(".");
146 return ar[ar.length-1];
151 public string getIconFileName(bool return_default)
154 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
156 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
157 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
158 File.new_for_path(dir).make_directory();
160 var fname = dir + "/" + m5 + ".png";
162 if (!return_default) {
163 print("getIconFileName return %s\n", fname);
167 if (FileUtils.test(fname, FileTest.EXISTS)) {
168 print("getIconFileName return %s\n", fname);
171 // we need to create this somehow...
172 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
173 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
178 public void saveBJS()
181 var generator = new Json.Generator ();
182 generator.indent = 1;
183 generator.pretty = true;
184 var node = new Json.Node(Json.NodeType.OBJECT);
185 node.set_object(this.toJsonObject());
186 generator.set_root(node);
188 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
190 this.writeFile(this.path, generator.to_data(null));
191 //generator.to_file(this.path);
193 print("Save failed");
199 public abstract void loadItems() throws GLib.Error;
203 * load from a javascript file.. rather than bjs..
208 _loadItems : function(cb)
211 if (this.items !== false) {
217 var tr = new TokenReader( {
222 collapseWhite : false,
224 ignoreBadGrammer: true
227 var str = File.read(this.path);
228 var toks = tr.tokenize(new TextStream(str));
229 var rf = new JsParser(toks);
233 this.modOrder = cfg.modOrder || '001';
234 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
235 this.parent = cfg.parent;
236 this.permname = cfg.permname || '';
237 this.title = cfg.title || cfg.name;;
238 this.items = cfg.items || [];
240 //this.fixItems(_this, false);
248 * { success : , failure : , scope : }
255 print("File.getTree tree called on base object?!?!");
258 public string jsonHasOrEmpty(Json.Object obj, string key) {
259 return obj.has_member(key) ?
260 obj.get_string_member(key) : "";
264 public Json.Object toJsonObject ()
268 var ret = new Json.Object();
269 //ret.set_string_member("id", this.id); // not relivant..
270 ret.set_string_member("name", this.name);
271 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
272 ret.set_string_member("title", this.title == null ? "" : this.title);
273 ret.set_string_member("path", this.path);
274 //ret.set_string_member("items", this.items);
275 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
276 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
278 if (this.transStrings.size > 0) {
279 var tr = new Json.Object();
280 var iter = this.transStrings.map_iterator();
281 while (iter.next()) {
282 tr.set_string_member(iter.get_value(), iter.get_key());
284 ret.set_object_member("strings", tr);
289 var ar = new Json.Array();
290 // empty files do not have a tree.
291 if (this.tree != null) {
292 ar.add_object_element(this.tree.toJsonObject());
294 ret.set_array_member("items", ar);
301 public string getTitle ()
303 if (this.title.length > 0) {
306 var a = this.path.split("/");
307 return a[a.length-1];
309 public string getTitleTip()
311 if (this.title.length > 0) {
312 return "<b>" + this.title + "</b> " + this.path;
319 this.cn.sort(function(a,b) {
320 return a.path > b.path;// ? 1 : -1;
324 // should be in palete provider really..
327 public Palete.Palete palete()
329 return Palete.factory(this.xtype);
333 public string guessName(Node ar) // turns the object into full name.
335 // eg. xns: Roo, xtype: XXX -> Roo.xxx
336 if (!ar.hasXnsType()) {
340 return ar.get("* xns") + "." + ar.get("* xtype");
345 * non-atomic write (replacement for put contents, as it creates temporary files.
347 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
351 var f = GLib.File.new_for_path(path);
352 var data_out = new GLib.DataOutputStream(
353 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
355 data_out.put_string(contents, null);
356 data_out.close(null);
359 copyTo: function(path, cb)
362 this.loadItems(function() {
373 * munge JSON tree into Javascript code.
375 * NOTE - needs a deep copy of original tree, before starting..
376 * - so that it does not modify current..
378 * FIXME: + or / prefixes to properties hide it from renderer.
379 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
380 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
382 * @arg {object} obj the object or array to munge..
383 * @arg {boolean} isListener - is the array being sent a listener..
384 * @arg {string} pad - the padding to indent with.
387 public string mungeToString(string pad)
389 if (this.tree == null) {
392 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
397 public abstract void save();
398 public abstract void saveHTML(string html);
399 public abstract string toSource() ;
400 public abstract string toSourcePreview() ;
401 public abstract void removeFiles() ;
402 public abstract void findTransStrings(Node? node );