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