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.. ??? -
26 public string permname;
27 public string language;
28 public string modOrder;
30 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
32 public Project.Project project;
33 //Project : false, // link to container project!
35 public Node tree; // the tree of nodes.
37 public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
39 public bool hasParent;
41 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
44 public signal void changed (Node? node, string source);
50 //public Xcls_Editor editor;
54 public JsRender(Project.Project project, string path) {
56 this.cn = new GLib.List<JsRender>();
58 this.project = project;
59 this.hasParent = false;
67 print("JsRender.cto() - reset transStrings\n");
68 this.transStrings = new Gee.HashMap<string,string> ();
70 // should use basename reallly...
72 var ar = this.path.split("/");
73 // name is in theory filename without .bjs (or .js eventually...)
75 Regex regex = new Regex ("\\.(bjs|js)$");
77 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
81 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
83 this.doubleStringProps = new Gee.ArrayList<string>();
87 public void renameTo(string name)
90 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
91 if (FileUtils.test(bjs, FileTest.EXISTS)) {
92 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
94 GLib.FileUtils.remove(this.path);
96 // remove other files?
105 // not sure why xt is needed... -> project contains xtype..
107 public static JsRender factory(string xt, Project.Project project, string path)
112 return new Gtk(project, path);
114 return new Roo(project, path);
116 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
120 public string toJsonString()
122 var generator = new Json.Generator ();
123 generator.indent = 4;
124 generator.pretty = true;
125 var node = new Json.Node(Json.NodeType.OBJECT);
126 node.set_object(this.toJsonObject());
127 generator.set_root(node);
128 return generator.to_data(null);
131 public string nickType()
133 var ar = this.name.split(".");
135 for (var i =0; i < ar.length -1; i++) {
138 return string.joinv(".", ret);
141 public string nickName()
143 var ar = this.name.split(".");
144 return ar[ar.length-1];
149 public string getIconFileName(bool return_default)
152 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
154 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
155 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
156 File.new_for_path(dir).make_directory();
158 var fname = dir + "/" + m5 + ".png";
160 if (!return_default) {
161 print("getIconFileName return %s\n", fname);
165 if (FileUtils.test(fname, FileTest.EXISTS)) {
166 print("getIconFileName return %s\n", fname);
169 // we need to create this somehow...
170 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
171 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
176 public void saveBJS()
179 var generator = new Json.Generator ();
180 generator.indent = 1;
181 generator.pretty = true;
182 var node = new Json.Node(Json.NodeType.OBJECT);
183 node.set_object(this.toJsonObject());
184 generator.set_root(node);
186 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
188 this.writeFile(this.path, generator.to_data(null));
189 //generator.to_file(this.path);
191 print("Save failed");
197 public abstract void loadItems() throws GLib.Error;
201 * load from a javascript file.. rather than bjs..
206 _loadItems : function(cb)
209 if (this.items !== false) {
215 var tr = new TokenReader( {
220 collapseWhite : false,
222 ignoreBadGrammer: true
225 var str = File.read(this.path);
226 var toks = tr.tokenize(new TextStream(str));
227 var rf = new JsParser(toks);
231 this.modOrder = cfg.modOrder || '001';
232 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
233 this.parent = cfg.parent;
234 this.permname = cfg.permname || '';
235 this.title = cfg.title || cfg.name;;
236 this.items = cfg.items || [];
238 //this.fixItems(_this, false);
246 * { success : , failure : , scope : }
253 print("File.getTree tree called on base object?!?!");
256 public string jsonHasOrEmpty(Json.Object obj, string key) {
257 return obj.has_member(key) ?
258 obj.get_string_member(key) : "";
262 public Json.Object toJsonObject ()
266 var ret = new Json.Object();
267 //ret.set_string_member("id", this.id); // not relivant..
268 ret.set_string_member("name", this.name);
269 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
270 ret.set_string_member("title", this.title == null ? "" : this.title);
271 ret.set_string_member("path", this.path);
272 //ret.set_string_member("items", this.items);
273 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
274 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
276 if (this.transStrings.size > 0) {
277 var tr = new Json.Object();
278 var iter = this.transStrings.map_iterator();
279 while (iter.next()) {
280 tr.set_string_member(iter.get_value(), iter.get_key());
282 ret.set_object_member("strings", tr);
287 var ar = new Json.Array();
288 // empty files do not have a tree.
289 if (this.tree != null) {
290 ar.add_object_element(this.tree.toJsonObject());
292 ret.set_array_member("items", ar);
299 public string getTitle ()
301 if (this.title.length > 0) {
304 var a = this.path.split("/");
305 return a[a.length-1];
307 public string getTitleTip()
309 if (this.title.length > 0) {
310 return "<b>" + this.title + "</b> " + this.path;
317 this.cn.sort(function(a,b) {
318 return a.path > b.path;// ? 1 : -1;
322 // should be in palete provider really..
325 public Palete.Palete palete()
327 return Palete.factory(this.xtype);
331 public string guessName(Node ar) // turns the object into full name.
333 // eg. xns: Roo, xtype: XXX -> Roo.xxx
334 if (!ar.hasXnsType()) {
338 return ar.get("* xns") + "." + ar.get("* xtype");
343 * non-atomic write (replacement for put contents, as it creates temporary files.
345 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
349 var f = GLib.File.new_for_path(path);
350 var data_out = new GLib.DataOutputStream(
351 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
353 data_out.put_string(contents, null);
354 data_out.close(null);
357 copyTo: function(path, cb)
360 this.loadItems(function() {
371 * munge JSON tree into Javascript code.
373 * NOTE - needs a deep copy of original tree, before starting..
374 * - so that it does not modify current..
376 * FIXME: + or / prefixes to properties hide it from renderer.
377 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
378 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
380 * @arg {object} obj the object or array to munge..
381 * @arg {boolean} isListener - is the array being sent a listener..
382 * @arg {string} pad - the padding to indent with.
385 public string mungeToString(string pad)
387 if (this.tree == null) {
390 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
395 public abstract void save();
396 public abstract void saveHTML(string html);
397 public abstract string toSource() ;
398 public abstract string toSourcePreview() ;
399 public abstract void removeFiles() ;
400 public abstract void findTransStrings(Node node );