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;
17 public string id = "";
18 public string name { get; set; default = ""; } // is the JS name of the file.
19 public string fullname = "";
20 public string path = ""; // is the full path to the file.
22 public string relpath {
24 return this.project.path == this.path ? "" : this.path.substring(this.project.path.length+1);
28 public string reldir {
30 return this.project.path == this.dir ? "" : this. dir.substring(this.project.path.length+1);
37 return GLib.Path.get_dirname(this.path);
43 public string file_namespace {
45 if (!this.name.contains(".")) {
48 var bits = this.name.split(".");
53 public string file_without_namespace {
55 if (!this.name.contains(".")) {
58 var bits = this.name.split(".");
59 return this.name.substring(bits[0].length +1);
64 public string file_ext {
66 if (!this.path.contains(".")) {
69 var bits = this.name.split(".");
70 return bits[bits.length-1];
74 public string parent = ""; // JS parent.
75 public string region = ""; // RooJS - insert region.
77 public string title = ""; // a title.. ?? nickname.. ??? -
79 private int _version = 1; // should we increment this based on the node..?
82 return ++this._version; // increased on every call? - bit of a kludge until we do real versioning
85 this._version = value;
90 public string permname;
91 public string language;
92 public string content_type;
93 public string modOrder;
95 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
96 public bool gen_extended = false; // nodetovala??
98 public Project.Project project;
102 public string build_module; // module to build if we compile (or are running tests...)
104 //Project : false, // link to container project!
106 public Node tree; // the tree of nodes.
108 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
110 public bool hasParent;
114 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
115 public Gee.HashMap<string,string> namedStrings;
116 public Gee.HashMap<string, GLib.ListStore> errorsByType;
120 public signal void changed (Node? node, string source);
123 public signal void compile_notice(string type, string file, int line, string message);
131 //public Xcls_Editor editor;
132 public GLib.ListStore childfiles; // used by directories..
135 //abstract JsRender(Project.Project project, string path);
137 protected JsRender(Project.Project project, string path)
140 //this.cn = new GLib.List<JsRender>();
141 GLib.debug("new jsrender %s", path);
143 this.project = project;
144 this.hasParent = false;
152 this.content_type = "";
153 this.build_module = "";
154 //this.loaded = false;
155 //print("JsRender.cto() - reset transStrings\n");
156 this.transStrings = new Gee.HashMap<string,string>();
157 this.namedStrings = new Gee.HashMap<string,string>();
158 // should use basename reallly...
160 var ar = this.path.split("/");
161 // name is in theory filename without .bjs (or .js eventually...)
163 Regex regex = new Regex ("\\.(bjs)$");
165 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
166 } catch (GLib.Error e) {
169 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
171 this.doubleStringProps = new Gee.ArrayList<string>();
172 this.childfiles = new GLib.ListStore(typeof(JsRender));
173 this.errorsByType = new Gee.HashMap<string, GLib.ListStore>();
175 if (this.relpath == "src/Lsp.vala") {
176 GLib.debug("got testing lsp");
181 public void renameTo(string name) throws Error
183 if (this.xtype == "PlainFile") {
186 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
187 if (FileUtils.test(bjs, FileTest.EXISTS)) {
188 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
190 GLib.FileUtils.remove(this.path);
192 // remove other files?
201 // not sure why xt is needed... -> project contains xtype..
203 public static JsRender factory(string xt, Project.Project project, string path) throws Error
208 return new Gtk(project, path);
211 return new Roo((Project.Roo) project, path);
213 // return new Flutter(project, path);
215 return new PlainFile(project, path);
217 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
223 public string nickType()
225 var ar = this.name.split(".");
227 for (var i =0; i < ar.length -1; i++) {
230 return string.joinv(".", ret);
233 public string nickName()
235 var ar = this.name.split(".");
236 return ar[ar.length-1];
240 public string nickNameSplit()
242 var n = this.nickName();
245 for (var i = 0; i < n.length; i++) {
246 if (i!=0 && n.get(i).isupper() && len > 10) {
250 ret += n.get(i).to_string();
260 Gdk.Pixbuf screenshot = null;
261 Gdk.Pixbuf screenshot92 = null;
262 Gdk.Pixbuf screenshot368 = null;
264 public Gdk.Pixbuf? getIcon(int size = 0) {
265 var fname = this.getIconFileName( );
266 if (!FileUtils.test(fname, FileTest.EXISTS)) {
267 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
273 if (this.screenshot == null) {
275 this.screenshot = new Gdk.Pixbuf.from_file(fname);
276 } catch (GLib.Error e) {}
278 return this.screenshot;
282 if (this.screenshot == null) {
284 if (this.screenshot == null) {
289 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
290 , Gdk.InterpType.NEAREST) ;
291 return this.screenshot92;
294 if (this.screenshot == null) {
296 if (this.screenshot == null) {
301 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
302 , Gdk.InterpType.NEAREST) ;
303 return this.screenshot368;
308 public void writeIcon(Gdk.Pixbuf pixbuf) {
310 this.screenshot92 = null;
311 this.screenshot368 = null;
312 this.screenshot = null;
314 GLib.debug("Wirte %s", this.getIconFileName( ));
315 pixbuf.save(this.getIconFileName( ),"png");
316 this.screenshot = pixbuf;
318 } catch (GLib.Error e) {
319 GLib.debug("failed to write pixbuf?");
327 public void widgetToIcon(global::Gtk.Widget widget) {
329 this.screenshot92 = null;
330 this.screenshot368 = null;
331 this.screenshot = null;
336 var filename = this.getIconFileName();
339 var p = new global::Gtk.WidgetPaintable(widget);
340 var s = new global::Gtk.Snapshot();
341 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
342 p.snapshot(s, widget.get_width(), widget.get_height());
343 var n = s.free_to_node();
344 var r = new Gsk.CairoRenderer();
346 var t = r.render_texture(n,null);
347 GLib.debug("write to %s", filename);
348 t.save_to_png(filename);
352 } catch (GLib.Error e) {
353 GLib.debug("failed to write pixbuf?");
363 public string getIconFileName( )
366 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
368 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
370 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
371 File.new_for_path(dir).make_directory();
373 } catch (GLib.Error e) {
374 // eakk.. what to do here...
376 var fname = dir + "/" + m5 + ".png";
383 public string toJsonString()
385 if (this.xtype == "PlainFile") {
388 var node = new Json.Node(Json.NodeType.OBJECT);
389 node.set_object(this.toJsonObject());
390 var generator = new JsonGen(node);
391 generator.indent = 1;
392 generator.pretty = true;
395 return generator.to_data();
398 public void saveBJS()
400 // if (!this.loaded) {
401 /// GLib.debug("saveBJS - skip - not loaded?");
404 if (this.xtype == "PlainFile") {
410 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
412 this.writeFile(this.path, this.toJsonString());
414 } catch(GLib.Error e) {
415 print("Save failed");
426 public string jsonHasOrEmpty(Json.Object obj, string key) {
427 return obj.has_member(key) ?
428 obj.get_string_member(key) : "";
432 public Json.Object toJsonObject ()
436 var ret = new Json.Object();
437 if (this.xtype == "PlainFile") {
441 //ret.set_string_member("id", this.id); // not relivant..
442 ret.set_string_member("name", this.name);
444 if (this.project.xtype == "Roo") {
445 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
446 ret.set_string_member("title", this.title == null ? "" : this.title);
447 //ret.set_string_member("path", this.path);
448 //ret.set_string_member("items", this.items);
449 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
450 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
452 if (this.project.xtype == "Gtk") {
454 ret.set_string_member("build_module", this.build_module );
456 ret.set_boolean_member("gen_extended", this.gen_extended);
458 if (this.transStrings.size > 0) {
459 var tr = new Json.Object();
460 var iter = this.transStrings.map_iterator();
461 while (iter.next()) {
462 tr.set_string_member(iter.get_value(), iter.get_key());
464 ret.set_object_member("strings", tr);
469 if (this.namedStrings.size > 0) {
470 var tr = new Json.Object();
471 var iter = this.namedStrings.map_iterator();
472 while (iter.next()) {
473 tr.set_string_member(iter.get_key(), iter.get_value());
475 ret.set_object_member("named_strings", tr);
478 var ar = new Json.Array();
479 // empty files do not have a tree.
480 if (this.tree != null) {
481 ar.add_object_element(this.tree.toJsonObject());
483 ret.set_array_member("items", ar);
490 public string getTitle ()
492 if (this.title == null) { // not sure why this happens..
495 if (this.title.length > 0) {
498 var a = this.path.split("/");
499 return a[a.length-1];
501 public string getTitleTip()
503 if (this.title.length > 0) {
504 return "<b>" + this.title + "</b> " + this.path;
511 this.cn.sort(function(a,b) {
512 return a.path > b.path;// ? 1 : -1;
516 // should be in palete provider really..
519 public Palete.Palete palete()
521 // error on plainfile?
522 return this.project.palete;
526 public string guessName(Node ar) // turns the object into full name.
528 // eg. xns: Roo, xtype: XXX -> Roo.xxx
529 if (!ar.hasXnsType()) {
533 return ar.get("xns") + "." + ar.get("xtype");
538 * non-atomic write (replacement for put contents, as it creates temporary files.
540 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
544 var f = GLib.File.new_for_path(path);
545 var data_out = new GLib.DataOutputStream(
546 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
548 data_out.put_string(contents, null);
549 data_out.close(null);
554 public Node? lineToNode(int line)
556 if (this.tree == null) {
559 return this.tree.lineToNode(line);
564 public GLib.ListStore toListStore()
566 var ret = new GLib.ListStore(typeof(Node));
567 ret.append(this.tree);
572 // used to handle list of files in project editor (really Gtk only)
573 public bool compile_group_selected {
575 var gproj = (Project.Gtk) this.project;
577 if (gproj.active_cg == null) {
580 if (this.xtype == "Dir") {
581 // show ticked if all ticked..
583 for(var i = 0; i < this.childfiles.n_items; i++ ) {
584 var f = (JsRender) this.childfiles.get_item(i);
585 if (!f.compile_group_selected) {
594 if (gproj.active_cg.sources == null) {
595 GLib.debug("compile_group_selected - sources is null? ");
599 return gproj.active_cg.sources.contains(this.relpath);
604 var gproj = (Project.Gtk) this.project;
606 if (gproj.active_cg == null) {
609 if (gproj.active_cg.loading_ui) {
613 if (this.xtype == "Dir") {
614 for(var i = 0; i < this.childfiles.n_items; i++ ) {
615 var f = (JsRender) this.childfiles.get_item(i);
616 f.compile_group_selected = value;
625 if (value == false) {
626 GLib.debug("REMOVE %s", this.relpath);
628 gproj.active_cg.sources.remove(this.relpath);
631 if (!gproj.active_cg.sources.contains(this.relpath)) {
632 GLib.debug("ADD %s", this.relpath);
633 gproj.active_cg.sources.add(this.relpath);
639 public bool compile_group_hidden {
641 var gproj = (Project.Gtk) this.project;
644 return gproj.hidden.contains(this.relpath);
649 var gproj = (Project.Gtk) this.project;
651 if (gproj.active_cg == null) {
654 if (gproj.active_cg.loading_ui) {
657 if (value == false) {
658 GLib.debug("REMOVE %s", this.relpath);
660 gproj.hidden.remove(this.relpath);
663 if (!gproj.hidden.contains(this.relpath)) {
664 gproj.hidden.add(this.relpath);
665 // hiding a project will auto clear it.
666 for(var i = 0; i < this.childfiles.n_items; i++ ) {
667 var f = (JsRender) this.childfiles.get_item(i);
668 f.compile_group_selected = false;
679 if (this.xtype == "Dir") {
682 // cleans up build (should really be in children..
683 this.removeFile(this.path);
684 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
685 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
688 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
689 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
690 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
691 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
693 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
694 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
695 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
701 private void removeFile(string path)
704 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
705 GLib.FileUtils.unlink(path);
709 public string relTargetName()
711 return this.targetName().substring(this.project.path.length +1);
715 public string to_url()
717 return File.new_for_path (this.targetName()).get_uri ();
719 public Palete.LanguageClient? getLanguageServer()
722 return this.project.getLanguageServer(this.language_id());
725 public GLib.ListStore getErrors(string n)
727 var ls = this.errorsByType.get(n);
729 ls = new GLib.ListStore(typeof(Palete.CompileError));
730 this.errorsByType.set(n, ls );
736 public abstract string language_id();
737 public abstract void save();
738 public abstract void saveHTML(string html);
739 public abstract string toSource() ;
740 public abstract string toSourceCode() ; // used by commandline tester..
741 public abstract void setSource(string str);
742 public abstract string toSourcePreview() ;
743 public abstract void removeFiles() ;
744 public abstract void findTransStrings(Node? node );
745 public abstract string toGlade();
746 public abstract string targetName();
747 public abstract void loadItems() throws GLib.Error;