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);
49 public signal void compile_notice(string type, string file, int line, string message);
54 //public Xcls_Editor editor;
58 public JsRender(Project.Project project, string path) {
60 this.cn = new GLib.List<JsRender>();
62 this.project = project;
63 this.hasParent = false;
71 this.build_module = "";
72 print("JsRender.cto() - reset transStrings\n");
73 this.transStrings = new Gee.HashMap<string,string> ();
75 // should use basename reallly...
77 var ar = this.path.split("/");
78 // name is in theory filename without .bjs (or .js eventually...)
80 Regex regex = new Regex ("\\.(bjs|js)$");
82 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
86 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
88 this.doubleStringProps = new Gee.ArrayList<string>();
92 public void renameTo(string name)
94 if (this.xtype == "PlainFile") {
97 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
98 if (FileUtils.test(bjs, FileTest.EXISTS)) {
99 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
101 GLib.FileUtils.remove(this.path);
103 // remove other files?
112 // not sure why xt is needed... -> project contains xtype..
114 public static JsRender factory(string xt, Project.Project project, string path)
119 return new Gtk(project, path);
121 return new Roo(project, path);
123 return new PlainFile(project, path);
125 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
129 public string toJsonString()
131 if (this.xtype == "PlainFile") {
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()
190 if (this.xtype == "PlainFile") {
193 var generator = new Json.Generator ();
194 generator.indent = 1;
195 generator.pretty = true;
196 var node = new Json.Node(Json.NodeType.OBJECT);
197 node.set_object(this.toJsonObject());
198 generator.set_root(node);
200 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
202 this.writeFile(this.path, generator.to_data(null));
203 //generator.to_file(this.path);
205 print("Save failed");
211 public abstract void loadItems() throws GLib.Error;
215 * load from a javascript file.. rather than bjs..
220 _loadItems : function(cb)
223 if (this.items !== false) {
229 var tr = new TokenReader( {
234 collapseWhite : false,
236 ignoreBadGrammer: true
239 var str = File.read(this.path);
240 var toks = tr.tokenize(new TextStream(str));
241 var rf = new JsParser(toks);
245 this.modOrder = cfg.modOrder || '001';
246 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
247 this.parent = cfg.parent;
248 this.permname = cfg.permname || '';
249 this.title = cfg.title || cfg.name;;
250 this.items = cfg.items || [];
252 //this.fixItems(_this, false);
260 * { success : , failure : , scope : }
267 print("File.getTree tree called on base object?!?!");
270 public string jsonHasOrEmpty(Json.Object obj, string key) {
271 return obj.has_member(key) ?
272 obj.get_string_member(key) : "";
276 public Json.Object toJsonObject ()
280 var ret = new Json.Object();
281 if (this.xtype == "PlainFile") {
285 //ret.set_string_member("id", this.id); // not relivant..
286 ret.set_string_member("name", this.name);
287 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
288 ret.set_string_member("title", this.title == null ? "" : this.title);
289 ret.set_string_member("path", this.path);
290 //ret.set_string_member("items", this.items);
291 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
292 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
293 if (this.project.xtype == "Gtk") {
294 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
297 if (this.transStrings.size > 0) {
298 var tr = new Json.Object();
299 var iter = this.transStrings.map_iterator();
300 while (iter.next()) {
301 tr.set_string_member(iter.get_value(), iter.get_key());
303 ret.set_object_member("strings", tr);
308 var ar = new Json.Array();
309 // empty files do not have a tree.
310 if (this.tree != null) {
311 ar.add_object_element(this.tree.toJsonObject());
313 ret.set_array_member("items", ar);
320 public string getTitle ()
322 if (this.title.length > 0) {
325 var a = this.path.split("/");
326 return a[a.length-1];
328 public string getTitleTip()
330 if (this.title.length > 0) {
331 return "<b>" + this.title + "</b> " + this.path;
338 this.cn.sort(function(a,b) {
339 return a.path > b.path;// ? 1 : -1;
343 // should be in palete provider really..
346 public Palete.Palete palete()
348 return Palete.factory(this.xtype);
352 public string guessName(Node ar) // turns the object into full name.
354 // eg. xns: Roo, xtype: XXX -> Roo.xxx
355 if (!ar.hasXnsType()) {
359 return ar.get("* xns") + "." + ar.get("* xtype");
364 * non-atomic write (replacement for put contents, as it creates temporary files.
366 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
370 var f = GLib.File.new_for_path(path);
371 var data_out = new GLib.DataOutputStream(
372 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
374 data_out.put_string(contents, null);
375 data_out.close(null);
378 copyTo: function(path, cb)
381 this.loadItems(function() {
392 * munge JSON tree into Javascript code.
394 * NOTE - needs a deep copy of original tree, before starting..
395 * - so that it does not modify current..
397 * FIXME: + or / prefixes to properties hide it from renderer.
398 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
399 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
401 * @arg {object} obj the object or array to munge..
402 * @arg {boolean} isListener - is the array being sent a listener..
403 * @arg {string} pad - the padding to indent with.
406 public string mungeToString(string pad)
408 if (this.tree == null) {
411 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
416 public Node? lineToNode(int line)
418 if (this.tree == null) {
421 return this.tree.lineToNode(line);
427 public abstract void save();
428 public abstract void saveHTML(string html);
429 public abstract string toSource() ;
430 public abstract string toSourcePreview() ;
431 public abstract void removeFiles() ;
432 public abstract void findTransStrings(Node? node );