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 this.build_module = "";
70 print("JsRender.cto() - reset transStrings\n");
71 this.transStrings = new Gee.HashMap<string,string> ();
73 // should use basename reallly...
75 var ar = this.path.split("/");
76 // name is in theory filename without .bjs (or .js eventually...)
78 Regex regex = new Regex ("\\.(bjs|js)$");
80 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
84 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
86 this.doubleStringProps = new Gee.ArrayList<string>();
90 public void renameTo(string name)
93 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
94 if (FileUtils.test(bjs, FileTest.EXISTS)) {
95 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
97 GLib.FileUtils.remove(this.path);
99 // remove other files?
108 // not sure why xt is needed... -> project contains xtype..
110 public static JsRender factory(string xt, Project.Project project, string path)
115 return new Gtk(project, path);
117 return new Roo(project, path);
119 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
123 public string toJsonString()
125 var generator = new Json.Generator ();
126 generator.indent = 4;
127 generator.pretty = true;
128 var node = new Json.Node(Json.NodeType.OBJECT);
129 node.set_object(this.toJsonObject());
130 generator.set_root(node);
131 return generator.to_data(null);
134 public string nickType()
136 var ar = this.name.split(".");
138 for (var i =0; i < ar.length -1; i++) {
141 return string.joinv(".", ret);
144 public string nickName()
146 var ar = this.name.split(".");
147 return ar[ar.length-1];
152 public string getIconFileName(bool return_default)
155 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
157 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
158 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
159 File.new_for_path(dir).make_directory();
161 var fname = dir + "/" + m5 + ".png";
163 if (!return_default) {
164 print("getIconFileName return %s\n", fname);
168 if (FileUtils.test(fname, FileTest.EXISTS)) {
169 print("getIconFileName return %s\n", fname);
172 // we need to create this somehow...
173 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
174 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
179 public void saveBJS()
182 var generator = new Json.Generator ();
183 generator.indent = 1;
184 generator.pretty = true;
185 var node = new Json.Node(Json.NodeType.OBJECT);
186 node.set_object(this.toJsonObject());
187 generator.set_root(node);
189 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
191 this.writeFile(this.path, generator.to_data(null));
192 //generator.to_file(this.path);
194 print("Save failed");
200 public abstract void loadItems() throws GLib.Error;
204 * load from a javascript file.. rather than bjs..
209 _loadItems : function(cb)
212 if (this.items !== false) {
218 var tr = new TokenReader( {
223 collapseWhite : false,
225 ignoreBadGrammer: true
228 var str = File.read(this.path);
229 var toks = tr.tokenize(new TextStream(str));
230 var rf = new JsParser(toks);
234 this.modOrder = cfg.modOrder || '001';
235 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
236 this.parent = cfg.parent;
237 this.permname = cfg.permname || '';
238 this.title = cfg.title || cfg.name;;
239 this.items = cfg.items || [];
241 //this.fixItems(_this, false);
249 * { success : , failure : , scope : }
256 print("File.getTree tree called on base object?!?!");
259 public string jsonHasOrEmpty(Json.Object obj, string key) {
260 return obj.has_member(key) ?
261 obj.get_string_member(key) : "";
265 public Json.Object toJsonObject ()
269 var ret = new Json.Object();
270 //ret.set_string_member("id", this.id); // not relivant..
271 ret.set_string_member("name", this.name);
272 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
273 ret.set_string_member("title", this.title == null ? "" : this.title);
274 ret.set_string_member("path", this.path);
275 //ret.set_string_member("items", this.items);
276 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
277 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
278 if (this.xtype == "Gtk") {
279 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
282 if (this.transStrings.size > 0) {
283 var tr = new Json.Object();
284 var iter = this.transStrings.map_iterator();
285 while (iter.next()) {
286 tr.set_string_member(iter.get_value(), iter.get_key());
288 ret.set_object_member("strings", tr);
293 var ar = new Json.Array();
294 // empty files do not have a tree.
295 if (this.tree != null) {
296 ar.add_object_element(this.tree.toJsonObject());
298 ret.set_array_member("items", ar);
305 public string getTitle ()
307 if (this.title.length > 0) {
310 var a = this.path.split("/");
311 return a[a.length-1];
313 public string getTitleTip()
315 if (this.title.length > 0) {
316 return "<b>" + this.title + "</b> " + this.path;
323 this.cn.sort(function(a,b) {
324 return a.path > b.path;// ? 1 : -1;
328 // should be in palete provider really..
331 public Palete.Palete palete()
333 return Palete.factory(this.xtype);
337 public string guessName(Node ar) // turns the object into full name.
339 // eg. xns: Roo, xtype: XXX -> Roo.xxx
340 if (!ar.hasXnsType()) {
344 return ar.get("* xns") + "." + ar.get("* xtype");
349 * non-atomic write (replacement for put contents, as it creates temporary files.
351 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
355 var f = GLib.File.new_for_path(path);
356 var data_out = new GLib.DataOutputStream(
357 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
359 data_out.put_string(contents, null);
360 data_out.close(null);
363 copyTo: function(path, cb)
366 this.loadItems(function() {
377 * munge JSON tree into Javascript code.
379 * NOTE - needs a deep copy of original tree, before starting..
380 * - so that it does not modify current..
382 * FIXME: + or / prefixes to properties hide it from renderer.
383 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
384 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
386 * @arg {object} obj the object or array to munge..
387 * @arg {boolean} isListener - is the array being sent a listener..
388 * @arg {string} pad - the padding to indent with.
391 public string mungeToString(string pad)
393 if (this.tree == null) {
396 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
401 public abstract void save();
402 public abstract void saveHTML(string html);
403 public abstract string toSource() ;
404 public abstract string toSourcePreview() ;
405 public abstract void removeFiles() ;
406 public abstract void findTransStrings(Node? node );