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.
47 public Gee.HashMap<string,string> namedStrings;
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>();
79 this.namedStrings = 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 Flutter(project, path);
130 return new PlainFile(project, path);
132 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
136 public string toJsonString()
138 if (this.xtype == "PlainFile") {
141 var generator = new Json.Generator ();
142 generator.indent = 4;
143 generator.pretty = true;
144 var node = new Json.Node(Json.NodeType.OBJECT);
145 node.set_object(this.toJsonObject());
146 generator.set_root(node);
147 return generator.to_data(null);
150 public string nickType()
152 var ar = this.name.split(".");
154 for (var i =0; i < ar.length -1; i++) {
157 return string.joinv(".", ret);
160 public string nickName()
162 var ar = this.name.split(".");
163 return ar[ar.length-1];
167 public string nickNameSplit()
169 var n = this.nickName();
172 for (var i = 0; i < n.length; i++) {
173 if (i!=0 && n.get(i).isupper() && len > 10) {
177 ret += n.get(i).to_string();
188 public string getIconFileName(bool return_default)
191 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
193 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
195 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
196 File.new_for_path(dir).make_directory();
198 } catch (GLib.Error e) {
199 // eakk.. what to do here...
201 var fname = dir + "/" + m5 + ".png";
203 if (!return_default) {
204 print("getIconFileName return %s\n", fname);
208 if (FileUtils.test(fname, FileTest.EXISTS)) {
209 print("getIconFileName return %s\n", fname);
212 // we need to create this somehow...
213 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
214 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
219 public void saveBJS()
224 if (this.xtype == "PlainFile") {
227 var generator = new Json.Generator ();
228 generator.indent = 1;
229 generator.pretty = true;
230 var node = new Json.Node(Json.NodeType.OBJECT);
231 node.set_object(this.toJsonObject());
232 generator.set_root(node);
234 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
236 this.writeFile(this.path, generator.to_data(null));
237 //generator.to_file(this.path);
239 print("Save failed");
245 public abstract void loadItems() throws GLib.Error;
250 public string jsonHasOrEmpty(Json.Object obj, string key) {
251 return obj.has_member(key) ?
252 obj.get_string_member(key) : "";
256 public Json.Object toJsonObject ()
260 var ret = new Json.Object();
261 if (this.xtype == "PlainFile") {
265 //ret.set_string_member("id", this.id); // not relivant..
266 ret.set_string_member("name", this.name);
267 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
268 ret.set_string_member("title", this.title == null ? "" : this.title);
269 ret.set_string_member("path", this.path);
270 //ret.set_string_member("items", this.items);
271 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
272 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
273 if (this.project.xtype == "Gtk") {
274 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
277 if (this.transStrings.size > 0) {
278 var tr = new Json.Object();
279 var iter = this.transStrings.map_iterator();
280 while (iter.next()) {
281 tr.set_string_member(iter.get_value(), iter.get_key());
283 ret.set_object_member("strings", tr);
288 if (this.namedStrings.size > 0) {
289 var tr = new Json.Object();
290 var iter = this.namedStrings.map_iterator();
291 while (iter.next()) {
292 tr.set_string_member(iter.get_key(), iter.get_value());
294 ret.set_object_member("named_strings", tr);
297 var ar = new Json.Array();
298 // empty files do not have a tree.
299 if (this.tree != null) {
300 ar.add_object_element(this.tree.toJsonObject());
302 ret.set_array_member("items", ar);
309 public string getTitle ()
311 if (this.title.length > 0) {
314 var a = this.path.split("/");
315 return a[a.length-1];
317 public string getTitleTip()
319 if (this.title.length > 0) {
320 return "<b>" + this.title + "</b> " + this.path;
327 this.cn.sort(function(a,b) {
328 return a.path > b.path;// ? 1 : -1;
332 // should be in palete provider really..
335 public Palete.Palete palete()
337 // error on plainfile?
338 return this.project.palete;
342 public string guessName(Node ar) // turns the object into full name.
344 // eg. xns: Roo, xtype: XXX -> Roo.xxx
345 if (!ar.hasXnsType()) {
349 return ar.get("* xns") + "." + ar.get("* xtype");
354 * non-atomic write (replacement for put contents, as it creates temporary files.
356 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
360 var f = GLib.File.new_for_path(path);
361 var data_out = new GLib.DataOutputStream(
362 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
364 data_out.put_string(contents, null);
365 data_out.close(null);
370 public Node? lineToNode(int line)
372 if (this.tree == null) {
375 return this.tree.lineToNode(line);
381 public abstract void save();
382 public abstract void saveHTML(string html);
383 public abstract string toSource() ;
384 public abstract string toSourceCode() ; // used by commandline tester..
385 public abstract void setSource(string str);
386 public abstract string toSourcePreview() ;
387 public abstract void removeFiles() ;
388 public abstract void findTransStrings(Node? node );