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 signal void changed (Node? node, string source);
47 //public Xcls_Editor editor;
51 public JsRender(Project.Project project, string path) {
53 this.cn = new GLib.List<JsRender>();
55 this.project = project;
56 this.hasParent = false;
66 // should use basename reallly...
68 var ar = this.path.split("/");
69 // name is in theory filename without .bjs (or .js eventually...)
71 Regex regex = new Regex ("\\.(bjs|js)$");
73 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
77 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
79 this.doubleStringProps = new Gee.ArrayList<string>();
83 public void renameTo(string name)
86 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
87 if (FileUtils.test(bjs, FileTest.EXISTS)) {
88 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
90 GLib.FileUtils.remove(this.path);
92 // remove other files?
101 // not sure why xt is needed... -> project contains xtype..
103 public static JsRender factory(string xt, Project.Project project, string path)
108 return new Gtk(project, path);
110 return new Roo(project, path);
112 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
116 public string toJsonString()
118 var generator = new Json.Generator ();
119 generator.indent = 4;
120 generator.pretty = true;
121 var node = new Json.Node(Json.NodeType.OBJECT);
122 node.set_object(this.toJsonObject());
123 generator.set_root(node);
124 return generator.to_data(null);
127 public string nickType()
129 var ar = this.name.split(".");
131 for (var i =0; i < ar.length -1; i++) {
134 return string.joinv(".", ret);
137 public string nickName()
139 var ar = this.name.split(".");
140 return ar[ar.length-1];
145 public string getIconFileName(bool return_default)
148 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
150 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
151 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
152 File.new_for_path(dir).make_directory();
154 var fname = dir + "/" + m5 + ".png";
156 if (!return_default) {
157 print("getIconFileName return %s\n", fname);
161 if (FileUtils.test(fname, FileTest.EXISTS)) {
162 print("getIconFileName return %s\n", fname);
165 // we need to create this somehow...
166 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
167 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
172 public void saveBJS()
175 var generator = new Json.Generator ();
176 generator.indent = 1;
177 generator.pretty = true;
178 var node = new Json.Node(Json.NodeType.OBJECT);
179 node.set_object(this.toJsonObject());
180 generator.set_root(node);
182 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
184 this.writeFile(this.path, generator.to_data(null));
185 //generator.to_file(this.path);
187 print("Save failed");
193 public abstract void loadItems() throws GLib.Error;
197 * load from a javascript file.. rather than bjs..
202 _loadItems : function(cb)
205 if (this.items !== false) {
211 var tr = new TokenReader( {
216 collapseWhite : false,
218 ignoreBadGrammer: true
221 var str = File.read(this.path);
222 var toks = tr.tokenize(new TextStream(str));
223 var rf = new JsParser(toks);
227 this.modOrder = cfg.modOrder || '001';
228 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
229 this.parent = cfg.parent;
230 this.permname = cfg.permname || '';
231 this.title = cfg.title || cfg.name;;
232 this.items = cfg.items || [];
234 //this.fixItems(_this, false);
242 * { success : , failure : , scope : }
249 print("File.getTree tree called on base object?!?!");
252 public string jsonHasOrEmpty(Json.Object obj, string key) {
253 return obj.has_member(key) ?
254 obj.get_string_member(key) : "";
258 public Json.Object toJsonObject ()
262 var ret = new Json.Object();
263 //ret.set_string_member("id", this.id); // not relivant..
264 ret.set_string_member("name", this.name);
265 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
266 ret.set_string_member("title", this.title == null ? "" : this.title);
267 ret.set_string_member("path", this.path);
268 //ret.set_string_member("items", this.items);
269 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
270 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
271 var ar = new Json.Array();
272 // empty files do not have a tree.
273 if (this.tree != null) {
274 ar.add_object_element(this.tree.toJsonObject());
276 ret.set_array_member("items", ar);
283 public string getTitle ()
285 if (this.title.length > 0) {
288 var a = this.path.split("/");
289 return a[a.length-1];
291 public string getTitleTip()
293 if (this.title.length > 0) {
294 return "<b>" + this.title + "</b> " + this.path;
301 this.cn.sort(function(a,b) {
302 return a.path > b.path;// ? 1 : -1;
306 // should be in palete provider really..
309 public Palete.Palete palete()
311 return Palete.factory(this.xtype);
315 public string guessName(Node ar) // turns the object into full name.
317 // eg. xns: Roo, xtype: XXX -> Roo.xxx
318 if (!ar.hasXnsType()) {
322 return ar.get("* xns") + "." + ar.get("* xtype");
327 * non-atomic write (replacement for put contents, as it creates temporary files.
329 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
333 var f = GLib.File.new_for_path(path);
334 var data_out = new GLib.DataOutputStream(
335 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
337 data_out.put_string(contents, null);
338 data_out.close(null);
341 copyTo: function(path, cb)
344 this.loadItems(function() {
355 * munge JSON tree into Javascript code.
357 * NOTE - needs a deep copy of original tree, before starting..
358 * - so that it does not modify current..
360 * FIXME: + or / prefixes to properties hide it from renderer.
361 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
362 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
364 * @arg {object} obj the object or array to munge..
365 * @arg {boolean} isListener - is the array being sent a listener..
366 * @arg {string} pad - the padding to indent with.
369 public string mungeToString(string pad)
371 if (this.tree == null) {
374 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
379 public abstract void save();
380 public abstract void saveHTML(string html);
381 public abstract string toSource() ;
382 public abstract string toSourcePreview() ;
383 public abstract void removeFiles() ;