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;
44 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
47 public signal void changed (Node? node, string source);
50 public signal void compile_notice(string type, string file, int line, string message);
55 //public Xcls_Editor editor;
59 public JsRender(Project.Project project, string path) {
61 this.cn = new GLib.List<JsRender>();
63 this.project = project;
64 this.hasParent = false;
72 this.build_module = "";
73 print("JsRender.cto() - reset transStrings\n");
74 this.transStrings = new Gee.HashMap<string,string> ();
76 // should use basename reallly...
78 var ar = this.path.split("/");
79 // name is in theory filename without .bjs (or .js eventually...)
81 Regex regex = new Regex ("\\.(bjs|js)$");
83 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
87 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
89 this.doubleStringProps = new Gee.ArrayList<string>();
93 public void renameTo(string name)
95 if (this.xtype == "PlainFile") {
98 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
99 if (FileUtils.test(bjs, FileTest.EXISTS)) {
100 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
102 GLib.FileUtils.remove(this.path);
104 // remove other files?
113 // not sure why xt is needed... -> project contains xtype..
115 public static JsRender factory(string xt, Project.Project project, string path)
120 return new Gtk(project, path);
122 return new Roo(project, path);
124 return new PlainFile(project, path);
126 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
130 public string toJsonString()
132 if (this.xtype == "PlainFile") {
135 var generator = new Json.Generator ();
136 generator.indent = 4;
137 generator.pretty = true;
138 var node = new Json.Node(Json.NodeType.OBJECT);
139 node.set_object(this.toJsonObject());
140 generator.set_root(node);
141 return generator.to_data(null);
144 public string nickType()
146 var ar = this.name.split(".");
148 for (var i =0; i < ar.length -1; i++) {
151 return string.joinv(".", ret);
154 public string nickName()
156 var ar = this.name.split(".");
157 return ar[ar.length-1];
162 public string getIconFileName(bool return_default)
165 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
167 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
168 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
169 File.new_for_path(dir).make_directory();
171 var fname = dir + "/" + m5 + ".png";
173 if (!return_default) {
174 print("getIconFileName return %s\n", fname);
178 if (FileUtils.test(fname, FileTest.EXISTS)) {
179 print("getIconFileName return %s\n", fname);
182 // we need to create this somehow...
183 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
184 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
189 public void saveBJS()
191 if (this.xtype == "PlainFile") {
194 var generator = new Json.Generator ();
195 generator.indent = 1;
196 generator.pretty = true;
197 var node = new Json.Node(Json.NodeType.OBJECT);
198 node.set_object(this.toJsonObject());
199 generator.set_root(node);
201 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
203 this.writeFile(this.path, generator.to_data(null));
204 //generator.to_file(this.path);
206 print("Save failed");
212 public abstract void loadItems() throws GLib.Error;
217 public string jsonHasOrEmpty(Json.Object obj, string key) {
218 return obj.has_member(key) ?
219 obj.get_string_member(key) : "";
223 public Json.Object toJsonObject ()
227 var ret = new Json.Object();
228 if (this.xtype == "PlainFile") {
232 //ret.set_string_member("id", this.id); // not relivant..
233 ret.set_string_member("name", this.name);
234 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
235 ret.set_string_member("title", this.title == null ? "" : this.title);
236 ret.set_string_member("path", this.path);
237 //ret.set_string_member("items", this.items);
238 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
239 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
240 if (this.project.xtype == "Gtk") {
241 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
244 if (this.transStrings.size > 0) {
245 var tr = new Json.Object();
246 var iter = this.transStrings.map_iterator();
247 while (iter.next()) {
248 tr.set_string_member(iter.get_value(), iter.get_key());
250 ret.set_object_member("strings", tr);
255 var ar = new Json.Array();
256 // empty files do not have a tree.
257 if (this.tree != null) {
258 ar.add_object_element(this.tree.toJsonObject());
260 ret.set_array_member("items", ar);
267 public string getTitle ()
269 if (this.title.length > 0) {
272 var a = this.path.split("/");
273 return a[a.length-1];
275 public string getTitleTip()
277 if (this.title.length > 0) {
278 return "<b>" + this.title + "</b> " + this.path;
285 this.cn.sort(function(a,b) {
286 return a.path > b.path;// ? 1 : -1;
290 // should be in palete provider really..
293 public Palete.Palete palete()
295 // error on plainfile?
296 return Palete.factory(this.xtype);
300 public string guessName(Node ar) // turns the object into full name.
302 // eg. xns: Roo, xtype: XXX -> Roo.xxx
303 if (!ar.hasXnsType()) {
307 return ar.get("* xns") + "." + ar.get("* xtype");
312 * non-atomic write (replacement for put contents, as it creates temporary files.
314 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
318 var f = GLib.File.new_for_path(path);
319 var data_out = new GLib.DataOutputStream(
320 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
322 data_out.put_string(contents, null);
323 data_out.close(null);
329 * munge JSON tree into Javascript code.
331 * NOTE - needs a deep copy of original tree, before starting..
332 * - so that it does not modify current..
334 * FIXME: + or / prefixes to properties hide it from renderer.
335 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
336 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
338 * @arg {object} obj the object or array to munge..
339 * @arg {boolean} isListener - is the array being sent a listener..
340 * @arg {string} pad - the padding to indent with.
343 public string mungeToString(string pad)
345 if (this.tree == null) {
348 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
353 public Node? lineToNode(int line)
355 if (this.tree == null) {
358 return this.tree.lineToNode(line);
364 public abstract void save();
365 public abstract void saveHTML(string html);
366 public abstract string toSource() ;
367 public abstract void setSource(string str);
368 public abstract string toSourcePreview() ;
369 public abstract void removeFiles() ;
370 public abstract void findTransStrings(Node? node );