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