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 content_type;
31 public string modOrder;
33 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
35 public Project.Project project;
36 //Project : false, // link to container project!
38 public Node tree; // the tree of nodes.
40 public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
42 public bool hasParent;
46 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
49 public signal void changed (Node? node, string source);
52 public signal void compile_notice(string type, string file, int line, string message);
57 //public Xcls_Editor editor;
61 public JsRender(Project.Project project, string path) {
63 this.cn = new GLib.List<JsRender>();
65 this.project = project;
66 this.hasParent = false;
74 this.content_type = "";
75 this.build_module = "";
77 print("JsRender.cto() - reset transStrings\n");
78 this.transStrings = new Gee.HashMap<string,string> ();
80 // should use basename reallly...
82 var ar = this.path.split("/");
83 // name is in theory filename without .bjs (or .js eventually...)
85 Regex regex = new Regex ("\\.(bjs|js)$");
87 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
88 } catch (GLib.Error e) {
91 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
93 this.doubleStringProps = new Gee.ArrayList<string>();
97 public void renameTo(string name) throws Error
99 if (this.xtype == "PlainFile") {
102 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
103 if (FileUtils.test(bjs, FileTest.EXISTS)) {
104 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
106 GLib.FileUtils.remove(this.path);
108 // remove other files?
117 // not sure why xt is needed... -> project contains xtype..
119 public static JsRender factory(string xt, Project.Project project, string path) throws Error
124 return new Gtk(project, path);
126 return new Roo(project, path);
128 return new PlainFile(project, path);
130 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
134 public string toJsonString()
136 if (this.xtype == "PlainFile") {
139 var generator = new Json.Generator ();
140 generator.indent = 4;
141 generator.pretty = true;
142 var node = new Json.Node(Json.NodeType.OBJECT);
143 node.set_object(this.toJsonObject());
144 generator.set_root(node);
145 return generator.to_data(null);
148 public string nickType()
150 var ar = this.name.split(".");
152 for (var i =0; i < ar.length -1; i++) {
155 return string.joinv(".", ret);
158 public string nickName()
160 var ar = this.name.split(".");
161 return ar[ar.length-1];
166 public string getIconFileName(bool return_default)
169 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
171 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
172 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
173 File.new_for_path(dir).make_directory();
175 var fname = dir + "/" + m5 + ".png";
177 if (!return_default) {
178 print("getIconFileName return %s\n", fname);
182 if (FileUtils.test(fname, FileTest.EXISTS)) {
183 print("getIconFileName return %s\n", fname);
186 // we need to create this somehow...
187 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
188 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
193 public void saveBJS()
198 if (this.xtype == "PlainFile") {
201 var generator = new Json.Generator ();
202 generator.indent = 1;
203 generator.pretty = true;
204 var node = new Json.Node(Json.NodeType.OBJECT);
205 node.set_object(this.toJsonObject());
206 generator.set_root(node);
208 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
210 this.writeFile(this.path, generator.to_data(null));
211 //generator.to_file(this.path);
213 print("Save failed");
219 public abstract void loadItems() throws GLib.Error;
224 public string jsonHasOrEmpty(Json.Object obj, string key) {
225 return obj.has_member(key) ?
226 obj.get_string_member(key) : "";
230 public Json.Object toJsonObject ()
234 var ret = new Json.Object();
235 if (this.xtype == "PlainFile") {
239 //ret.set_string_member("id", this.id); // not relivant..
240 ret.set_string_member("name", this.name);
241 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
242 ret.set_string_member("title", this.title == null ? "" : this.title);
243 ret.set_string_member("path", this.path);
244 //ret.set_string_member("items", this.items);
245 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
246 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
247 if (this.project.xtype == "Gtk") {
248 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
251 if (this.transStrings.size > 0) {
252 var tr = new Json.Object();
253 var iter = this.transStrings.map_iterator();
254 while (iter.next()) {
255 tr.set_string_member(iter.get_value(), iter.get_key());
257 ret.set_object_member("strings", tr);
262 var ar = new Json.Array();
263 // empty files do not have a tree.
264 if (this.tree != null) {
265 ar.add_object_element(this.tree.toJsonObject());
267 ret.set_array_member("items", ar);
274 public string getTitle ()
276 if (this.title.length > 0) {
279 var a = this.path.split("/");
280 return a[a.length-1];
282 public string getTitleTip()
284 if (this.title.length > 0) {
285 return "<b>" + this.title + "</b> " + this.path;
292 this.cn.sort(function(a,b) {
293 return a.path > b.path;// ? 1 : -1;
297 // should be in palete provider really..
300 public Palete.Palete palete()
302 // error on plainfile?
303 return Palete.factory(this.project.xtype);
307 public string guessName(Node ar) // turns the object into full name.
309 // eg. xns: Roo, xtype: XXX -> Roo.xxx
310 if (!ar.hasXnsType()) {
314 return ar.get("* xns") + "." + ar.get("* xtype");
319 * non-atomic write (replacement for put contents, as it creates temporary files.
321 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
325 var f = GLib.File.new_for_path(path);
326 var data_out = new GLib.DataOutputStream(
327 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
329 data_out.put_string(contents, null);
330 data_out.close(null);
336 * munge JSON tree into Javascript code.
338 * NOTE - needs a deep copy of original tree, before starting..
339 * - so that it does not modify current..
341 * FIXME: + or / prefixes to properties hide it from renderer.
342 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
343 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
345 * @arg {object} obj the object or array to munge..
346 * @arg {boolean} isListener - is the array being sent a listener..
347 * @arg {string} pad - the padding to indent with.
350 public string mungeToString(string pad)
352 if (this.tree == null) {
355 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
360 public Node? lineToNode(int line)
362 if (this.tree == null) {
365 return this.tree.lineToNode(line);
371 public abstract void save();
372 public abstract void saveHTML(string html);
373 public abstract string toSource() ;
374 public abstract void setSource(string str);
375 public abstract string toSourcePreview() ;
376 public abstract void removeFiles() ;
377 public abstract void findTransStrings(Node? node );