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)
95 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
96 if (FileUtils.test(bjs, FileTest.EXISTS)) {
97 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
99 GLib.FileUtils.remove(this.path);
101 // remove other files?
110 // not sure why xt is needed... -> project contains xtype..
112 public static JsRender factory(string xt, Project.Project project, string path)
117 return new Gtk(project, path);
119 return new Roo(project, path);
121 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
125 public string toJsonString()
127 var generator = new Json.Generator ();
128 generator.indent = 4;
129 generator.pretty = true;
130 var node = new Json.Node(Json.NodeType.OBJECT);
131 node.set_object(this.toJsonObject());
132 generator.set_root(node);
133 return generator.to_data(null);
136 public string nickType()
138 var ar = this.name.split(".");
140 for (var i =0; i < ar.length -1; i++) {
143 return string.joinv(".", ret);
146 public string nickName()
148 var ar = this.name.split(".");
149 return ar[ar.length-1];
154 public string getIconFileName(bool return_default)
157 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
159 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
160 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
161 File.new_for_path(dir).make_directory();
163 var fname = dir + "/" + m5 + ".png";
165 if (!return_default) {
166 print("getIconFileName return %s\n", fname);
170 if (FileUtils.test(fname, FileTest.EXISTS)) {
171 print("getIconFileName return %s\n", fname);
174 // we need to create this somehow...
175 print("getIconFileName return %s\n", GLib.Environment.get_home_dir() + "/.Builder/test.jpg");
176 return GLib.Environment.get_home_dir() + "/.Builder/test.jpg";
181 public void saveBJS()
184 var generator = new Json.Generator ();
185 generator.indent = 1;
186 generator.pretty = true;
187 var node = new Json.Node(Json.NodeType.OBJECT);
188 node.set_object(this.toJsonObject());
189 generator.set_root(node);
191 print("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
193 this.writeFile(this.path, generator.to_data(null));
194 //generator.to_file(this.path);
196 print("Save failed");
202 public abstract void loadItems() throws GLib.Error;
206 * load from a javascript file.. rather than bjs..
211 _loadItems : function(cb)
214 if (this.items !== false) {
220 var tr = new TokenReader( {
225 collapseWhite : false,
227 ignoreBadGrammer: true
230 var str = File.read(this.path);
231 var toks = tr.tokenize(new TextStream(str));
232 var rf = new JsParser(toks);
236 this.modOrder = cfg.modOrder || '001';
237 this.name = cfg.name.replace(/\.bjs/, ''); // BC!
238 this.parent = cfg.parent;
239 this.permname = cfg.permname || '';
240 this.title = cfg.title || cfg.name;;
241 this.items = cfg.items || [];
243 //this.fixItems(_this, false);
251 * { success : , failure : , scope : }
258 print("File.getTree tree called on base object?!?!");
261 public string jsonHasOrEmpty(Json.Object obj, string key) {
262 return obj.has_member(key) ?
263 obj.get_string_member(key) : "";
267 public Json.Object toJsonObject ()
271 var ret = new Json.Object();
272 //ret.set_string_member("id", this.id); // not relivant..
273 ret.set_string_member("name", this.name);
274 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
275 ret.set_string_member("title", this.title == null ? "" : this.title);
276 ret.set_string_member("path", this.path);
277 //ret.set_string_member("items", this.items);
278 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
279 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
280 if (this.project.xtype == "Gtk") {
281 ret.set_string_member("build_module", this.build_module == null ? "" : this.build_module);
284 if (this.transStrings.size > 0) {
285 var tr = new Json.Object();
286 var iter = this.transStrings.map_iterator();
287 while (iter.next()) {
288 tr.set_string_member(iter.get_value(), iter.get_key());
290 ret.set_object_member("strings", tr);
295 var ar = new Json.Array();
296 // empty files do not have a tree.
297 if (this.tree != null) {
298 ar.add_object_element(this.tree.toJsonObject());
300 ret.set_array_member("items", ar);
307 public string getTitle ()
309 if (this.title.length > 0) {
312 var a = this.path.split("/");
313 return a[a.length-1];
315 public string getTitleTip()
317 if (this.title.length > 0) {
318 return "<b>" + this.title + "</b> " + this.path;
325 this.cn.sort(function(a,b) {
326 return a.path > b.path;// ? 1 : -1;
330 // should be in palete provider really..
333 public Palete.Palete palete()
335 return Palete.factory(this.xtype);
339 public string guessName(Node ar) // turns the object into full name.
341 // eg. xns: Roo, xtype: XXX -> Roo.xxx
342 if (!ar.hasXnsType()) {
346 return ar.get("* xns") + "." + ar.get("* xtype");
351 * non-atomic write (replacement for put contents, as it creates temporary files.
353 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
357 var f = GLib.File.new_for_path(path);
358 var data_out = new GLib.DataOutputStream(
359 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
361 data_out.put_string(contents, null);
362 data_out.close(null);
365 copyTo: function(path, cb)
368 this.loadItems(function() {
379 * munge JSON tree into Javascript code.
381 * NOTE - needs a deep copy of original tree, before starting..
382 * - so that it does not modify current..
384 * FIXME: + or / prefixes to properties hide it from renderer.
385 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
386 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
388 * @arg {object} obj the object or array to munge..
389 * @arg {boolean} isListener - is the array being sent a listener..
390 * @arg {string} pad - the padding to indent with.
393 public string mungeToString(string pad)
395 if (this.tree == null) {
398 var x = new NodeToJs(this.tree, this.doubleStringProps, pad);
403 public JsRender.Node? lineToNode(int line)
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 );