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 modOrder;
32 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
34 public Project.Project project;
35 //Project : false, // link to container project!
37 public Node tree; // the tree of nodes.
39 public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
41 public bool hasParent;
43 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
46 public signal void changed (Node? node, string source);
48 // emitted by valasource... after a compile...
49 public signal void compiled_notice (
50 Gee.HashMap<int,string> line_warnings,
51 Gee.HashMap<int,string> line_depr,
52 Gee.HashMap<int,string> line_errors
61 //public Xcls_Editor editor;
65 public JsRender(Project.Project project, string path) {
67 this.cn = new GLib.List<JsRender>();
69 this.project = project;
70 this.hasParent = false;
78 this.build_module = "";
79 print("JsRender.cto() - reset transStrings\n");
80 this.transStrings = new Gee.HashMap<string,string> ();
82 // should use basename reallly...
84 var ar = this.path.split("/");
85 // name is in theory filename without .bjs (or .js eventually...)
87 Regex regex = new Regex ("\\.(bjs|js)$");
89 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
93 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
95 this.doubleStringProps = new Gee.ArrayList<string>();
99 public void renameTo(string name)
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)
124 return new Gtk(project, path);
126 return new Roo(project, path);
128 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
132 public string toJsonString()
134 var generator = new Json.Generator ();
135 generator.indent = 4;
136 generator.pretty = true;
137 var node = new Json.Node(Json.NodeType.OBJECT);
138 node.set_object(this.toJsonObject());
139 generator.set_root(node);
140 return generator.to_data(null);
143 public string nickType()
145 var ar = this.name.split(".");
147 for (var i =0; i < ar.length -1; i++) {
150 return string.joinv(".", ret);
153 public string nickName()
155 var ar = this.name.split(".");
156 return ar[ar.length-1];
161 public string getIconFileName(bool return_default)
164 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
166 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
167 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
168 File.new_for_path(dir).make_directory();
170 var fname = dir + "/" + m5 + ".png";
172 if (!return_default) {
173 print("getIconFileName return %s\n", fname);
177 if (FileUtils.test(fname, FileTest.EXISTS)) {
178 print("getIconFileName return %s\n", fname);
181 // we need to create this somehow...
182 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
183 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
188 public void saveBJS()
191 var generator = new Json.Generator ();
192 generator.indent = 1;
193 generator.pretty = true;
194 var node = new Json.Node(Json.NodeType.OBJECT);
195 node.set_object(this.toJsonObject());
196 generator.set_root(node);
198 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
200 this.writeFile(this.path, generator.to_data(null));
201 //generator.to_file(this.path);
203 print("Save failed");
209 public abstract void loadItems() throws GLib.Error;
213 * load from a javascript file.. rather than bjs..
218 _loadItems : function(cb)
221 if (this.items !== false) {
227 var tr = new TokenReader( {
232 collapseWhite : false,
234 ignoreBadGrammer: true
237 var str = File.read(this.path);
238 var toks = tr.tokenize(new TextStream(str));
239 var rf = new JsParser(toks);
243 this.modOrder = cfg.modOrder || '001';
244 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
245 this.parent = cfg.parent;
246 this.permname = cfg.permname || '';
247 this.title = cfg.title || cfg.name;;
248 this.items = cfg.items || [];
250 //this.fixItems(_this, false);
258 * { success : , failure : , scope : }
265 print("File.getTree tree called on base object?!?!");
268 public string jsonHasOrEmpty(Json.Object obj, string key) {
269 return obj.has_member(key) ?
270 obj.get_string_member(key) : "";
274 public Json.Object toJsonObject ()
278 var ret = new Json.Object();
279 //ret.set_string_member("id", this.id); // not relivant..
280 ret.set_string_member("name", this.name);
281 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
282 ret.set_string_member("title", this.title == null ? "" : this.title);
283 ret.set_string_member("path", this.path);
284 //ret.set_string_member("items", this.items);
285 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
286 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
287 if (this.project.xtype == "Gtk") {
288 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
291 if (this.transStrings.size > 0) {
292 var tr = new Json.Object();
293 var iter = this.transStrings.map_iterator();
294 while (iter.next()) {
295 tr.set_string_member(iter.get_value(), iter.get_key());
297 ret.set_object_member("strings", tr);
302 var ar = new Json.Array();
303 // empty files do not have a tree.
304 if (this.tree != null) {
305 ar.add_object_element(this.tree.toJsonObject());
307 ret.set_array_member("items", ar);
314 public string getTitle ()
316 if (this.title.length > 0) {
319 var a = this.path.split("/");
320 return a[a.length-1];
322 public string getTitleTip()
324 if (this.title.length > 0) {
325 return "<b>" + this.title + "</b> " + this.path;
332 this.cn.sort(function(a,b) {
333 return a.path > b.path;// ? 1 : -1;
337 // should be in palete provider really..
340 public Palete.Palete palete()
342 return Palete.factory(this.xtype);
346 public string guessName(Node ar) // turns the object into full name.
348 // eg. xns: Roo, xtype: XXX -> Roo.xxx
349 if (!ar.hasXnsType()) {
353 return ar.get("* xns") + "." + ar.get("* xtype");
358 * non-atomic write (replacement for put contents, as it creates temporary files.
360 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
364 var f = GLib.File.new_for_path(path);
365 var data_out = new GLib.DataOutputStream(
366 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
368 data_out.put_string(contents, null);
369 data_out.close(null);
372 copyTo: function(path, cb)
375 this.loadItems(function() {
386 * munge JSON tree into Javascript code.
388 * NOTE - needs a deep copy of original tree, before starting..
389 * - so that it does not modify current..
391 * FIXME: + or / prefixes to properties hide it from renderer.
392 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
393 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
395 * @arg {object} obj the object or array to munge..
396 * @arg {boolean} isListener - is the array being sent a listener..
397 * @arg {string} pad - the padding to indent with.
400 public string mungeToString(string pad)
402 if (this.tree == null) {
405 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
410 public abstract void save();
411 public abstract void saveHTML(string html);
412 public abstract string toSource() ;
413 public abstract string toSourcePreview() ;
414 public abstract void removeFiles() ;
415 public abstract void findTransStrings(Node? node );