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