src/Builder4/DialogPluginWebkit.bjs
[app.Builder.js] / src / Builder4 / DialogPluginWebkit.vala
1 static Xcls_DialogPluginWebkit  _DialogPluginWebkit;
2
3 public class Xcls_DialogPluginWebkit : Object 
4 {
5     public Gtk.Dialog el;
6     private Xcls_DialogPluginWebkit  _this;
7
8     public static Xcls_DialogPluginWebkit singleton()
9     {
10         if (_DialogPluginWebkit == null) {
11             _DialogPluginWebkit= new Xcls_DialogPluginWebkit();
12         }
13         return _DialogPluginWebkit;
14     }
15     public Xcls_webview webview;
16
17         // my vars (def)
18     public string tmpjs;
19     public string result_json;
20
21     // ctor 
22     public Xcls_DialogPluginWebkit()
23     {
24         _this = this;
25         this.el = new Gtk.Dialog();
26
27         // my vars (dec)
28
29         // set gobject values
30         this.el.title = "Add / Edit Component";
31         this.el.default_height = 500;
32         this.el.default_width = 500;
33         this.el.deletable = true;
34         this.el.modal = true;
35         var child_0 = new Xcls_VBox2( _this );
36         child_0.ref();
37         this.el.get_content_area().add (  child_0.el  );
38         var child_1 = new Xcls_Button5( _this );
39         child_1.ref();
40         this.el.add_action_widget (  child_1.el , 3 );
41         var child_2 = new Xcls_Button6( _this );
42         child_2.ref();
43         this.el.add_action_widget (  child_2.el , 0 );
44         var child_3 = new Xcls_Button7( _this );
45         child_3.ref();
46         this.el.add_action_widget (  child_3.el , 1 );
47
48         // listeners 
49         this.el.delete_event.connect( (self, event) => {
50             this.el.hide();
51             return true; 
52             //test  
53         });
54     }
55
56     // user defined functions 
57     public string show (Gtk.Window ?parent, Project.Project project, string cls, string tbl) {// JsRender.Node node) {
58      
59         if (parent  != null) {
60             this.el.set_transient_for(parent);
61             this.el.modal = true;
62         }
63          var  db = project.roo_database;
64          
65         
66          this.el.show_all();
67          var   ret = "";
68          while (true) {
69         
70             var runhtml = "<script type=\"text/javascript\">\n" ;
71             string builderhtml;
72             
73             
74             GLib.FileUtils.get_contents(BuilderApplication.configDirectory() + "/resources/roo.builder.js", out builderhtml);
75     
76             runhtml += builderhtml + "\n";
77             
78             
79                runhtml += "\n" +
80                 "Builder.saveHTML = function() {};\n" + 
81             "Roo.onReady(function() {\n" +
82     
83             "Roo.XComponent.build();\n" +
84             "});\n";
85         
86         
87             
88     
89             var ar = db.readForeignKeys(tbl);
90             var  generator = new Json.Generator ();
91             var  root = new Json.Node(Json.NodeType.OBJECT);
92             root.init_object(ar);
93             generator.set_root (root);
94             
95             generator.pretty = true;
96             generator.indent = 4;
97             
98             runhtml += "\n" +
99             " Roo.XComponent.on('buildcomplete', function() {\n" +
100              "    Editor." + cls + ".panel.loadData(" + generator.to_data (null) + "); " +
101             "});\n";
102     
103             
104         
105         
106     
107             runhtml += "</script>\n" ;
108     
109             print(runhtml);
110             // fix to make sure they are the same..
111             
112             // need to modify paths
113     
114             string inhtml;
115             
116             GLib.FileUtils.get_contents(
117                 BuilderApplication.configDirectory() + "/resources/roo.builder.html"
118                     , out inhtml);
119             
120             
121             // fetch the json from the database...
122             
123             //print(runhtml);
124             
125             var html = inhtml.replace("</head>", runhtml + // + this.runhtml + 
126                 "<script type=\"text/javascript\" src=\"xhttp://localhost/app.Builder.js/resources/Editors/Editor." + cls + ".js\"></script>" + 
127           
128                             
129             "</head>");
130             //print("LOAD HTML " + html);
131             
132              //var rootURL = _this.file.project.rootURL;
133        
134             
135             
136             this.webview.el.load_html( html , 
137                 //fixme - should be a config option!
138                 "xhttp://localhost/app.Builder.js/"
139             );
140         
141             
142         
143        
144              var response_id = this.el.run();
145             
146              if (response_id == 1) { // OK...
147                  var loop = new MainLoop();
148                  // run toBJS to get the data... (calls back into alert handler)
149                    this.webview.el.run_javascript.begin("Editor." + cls + ".panel.toBJS();", null, (obj, res) => {
150     
151                      this.webview.el.run_javascript.end(res);
152     
153                         loop.quit();
154                     });
155                  loop.run();
156         //           print("LOOP END?");
157                  // try and get the resopse...
158                 break;
159              }
160             if (response_id < 1) {
161                 this.el.hide();
162                  return "";
163             }
164             // keep showing...?
165             continue;
166         }
167         
168         // now we save it..
169         this.el.hide();
170         
171         return ret;
172         
173         
174         
175     }
176     public bool has_plugin (string cls) {
177     
178          return GLib.FileUtils.test(
179                 BuilderApplication.configDirectory() + "/resources/Editors/Editor." + cls + ".js",
180                 GLib.FileTest.IS_REGULAR
181           );
182         
183     
184     
185     }
186     public class Xcls_VBox2 : Object 
187     {
188         public Gtk.VBox el;
189         private Xcls_DialogPluginWebkit  _this;
190
191
192             // my vars (def)
193
194         // ctor 
195         public Xcls_VBox2(Xcls_DialogPluginWebkit _owner )
196         {
197             _this = _owner;
198             this.el = new Gtk.VBox( false, 0 );
199
200             // my vars (dec)
201
202             // set gobject values
203             var child_0 = new Xcls_ScrolledWindow3( _this );
204             child_0.ref();
205             this.el.pack_start (  child_0.el , false,true,3 );
206         }
207
208         // user defined functions 
209     }
210     public class Xcls_ScrolledWindow3 : Object 
211     {
212         public Gtk.ScrolledWindow el;
213         private Xcls_DialogPluginWebkit  _this;
214
215
216             // my vars (def)
217
218         // ctor 
219         public Xcls_ScrolledWindow3(Xcls_DialogPluginWebkit _owner )
220         {
221             _this = _owner;
222             this.el = new Gtk.ScrolledWindow( null, null );
223
224             // my vars (dec)
225
226             // set gobject values
227             this.el.expand = true;
228             var child_0 = new Xcls_webview( _this );
229             child_0.ref();
230             this.el.add (  child_0.el  );
231
232             // init method 
233
234             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
235         }
236
237         // user defined functions 
238     }
239     public class Xcls_webview : Object 
240     {
241         public WebKit.WebView el;
242         private Xcls_DialogPluginWebkit  _this;
243
244
245             // my vars (def)
246
247         // ctor 
248         public Xcls_webview(Xcls_DialogPluginWebkit _owner )
249         {
250             _this = _owner;
251             _this.webview = this;
252             this.el = new WebKit.WebView();
253
254             // my vars (dec)
255
256             // set gobject values
257
258             // init method 
259
260             {
261                 // this may not work!?
262                 var settings =  this.el.get_settings();
263                 settings.enable_write_console_messages_to_stdout = true;
264                  
265                 var fs= new FakeServer(this.el);
266                 fs.ref();
267                 // this was an attempt to change the url perms.. did not work..
268                 // settings.enable_file_access_from_file_uris = true;
269                 // settings.enable_offline_web_application_cache - true;
270                 // settings.enable_universal_access_from_file_uris = true;
271                
272                  
273                 
274                 
275                 
276             
277                  // FIXME - base url of script..
278                  // we need it so some of the database features work.
279                 this.el.load_html( "Render not ready" , 
280                         //fixme - should be a config option!
281                         // or should we catch stuff and fix it up..
282                         "xhttp://localhost/app.Builder/"
283                 );
284                     
285                     
286                 
287                 
288             }
289
290             // listeners 
291             this.el.script_dialog.connect( (dialog) => {
292                 if (this.el == null) {
293                     return true;
294                 }
295                 
296                  var msg = dialog.get_message();
297                  if (msg.length < 4) {
298                     return false;
299                  }
300                  if (msg.substring(0,4) != "IPC:") {
301                      return false;
302                  }
303                  var ar = msg.split(":", 3);
304                 if (ar.length < 3) {
305                     return false;
306                 }
307                 print("CMD: %s\n",ar[1]);
308                     print("ARGS: %s\n",ar[2]);
309                 switch(ar[1]) {
310                     case "SAVEHTML":
311                       print("%sw",ar[2]);
312                       //  _this.file.saveHTML(ar[2]);
313                         return true;
314                     default:
315                         return true;
316                 }
317                 
318             });
319         }
320
321         // user defined functions 
322     }
323     public class Xcls_Button5 : Object 
324     {
325         public Gtk.Button el;
326         private Xcls_DialogPluginWebkit  _this;
327
328
329             // my vars (def)
330
331         // ctor 
332         public Xcls_Button5(Xcls_DialogPluginWebkit _owner )
333         {
334             _this = _owner;
335             this.el = new Gtk.Button();
336
337             // my vars (dec)
338
339             // set gobject values
340             this.el.label = "Reload";
341         }
342
343         // user defined functions 
344     }
345     public class Xcls_Button6 : Object 
346     {
347         public Gtk.Button el;
348         private Xcls_DialogPluginWebkit  _this;
349
350
351             // my vars (def)
352
353         // ctor 
354         public Xcls_Button6(Xcls_DialogPluginWebkit _owner )
355         {
356             _this = _owner;
357             this.el = new Gtk.Button();
358
359             // my vars (dec)
360
361             // set gobject values
362             this.el.label = "Cancel";
363         }
364
365         // user defined functions 
366     }
367     public class Xcls_Button7 : Object 
368     {
369         public Gtk.Button el;
370         private Xcls_DialogPluginWebkit  _this;
371
372
373             // my vars (def)
374
375         // ctor 
376         public Xcls_Button7(Xcls_DialogPluginWebkit _owner )
377         {
378             _this = _owner;
379             this.el = new Gtk.Button();
380
381             // my vars (dec)
382
383             // set gobject values
384             this.el.label = "OK";
385         }
386
387         // user defined functions 
388     }
389 }