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             try {
121                 GLib.FileUtils.get_contents(
122                     BuilderApplication.configDirectory() + "/resources/roo.builder.html"
123                         , out inhtml);
124             
125             } catch (Error e) {
126                 inhtml = "";
127             }
128             // fetch the json from the database...
129             
130             //print(runhtml);
131             
132             var html = inhtml.replace("</head>", runhtml + // + this.runhtml + 
133                 "<script type=\"text/javascript\" src=\"resources://localhost/Editors/Editor." + cls + ".js\"></script>" + 
134           
135                             
136             "</head>");
137             //print("LOAD HTML " + html);
138             
139              //var rootURL = _this.file.project.rootURL;
140        
141             
142             
143             this.webview.el.load_html( html , 
144                 //fixme - should be a config option!
145                 "xhttp://localhost/app.Builder.js/"
146             );
147         
148             
149         
150        
151              var response_id = this.el.run();
152             
153              if (response_id == 1) { // OK...
154                  var loop = new MainLoop();
155                  // run toBJS to get the data... (calls back into alert handler)
156                    this.webview.el.run_javascript.begin("Editor." + cls + ".panel.toBJS();", null, (obj, res) => {
157     
158                      this.webview.el.run_javascript.end(res);
159     
160                         loop.quit();
161                     });
162                  loop.run();
163                  ret = _this.result_json;
164         //           print("LOOP END?");
165                  // try and get the resopse...
166                 break;
167              }
168             if (response_id < 1) {
169                 this.el.hide();
170                  return "";
171             }
172             // keep showing...?
173             continue;
174         }
175         
176         // now we save it..
177         this.el.hide();
178         
179         return ret;
180         
181         
182         
183     }
184     public bool has_plugin (string cls) {
185     
186          return GLib.FileUtils.test(
187                 BuilderApplication.configDirectory() + "/resources/Editors/Editor." + cls + ".js",
188                 GLib.FileTest.IS_REGULAR
189           );
190         
191     
192     
193     }
194     public class Xcls_Box2 : Object
195     {
196         public Gtk.Box el;
197         private Xcls_DialogPluginWebkit  _this;
198
199
200             // my vars (def)
201
202         // ctor
203         public Xcls_Box2(Xcls_DialogPluginWebkit _owner )
204         {
205             _this = _owner;
206             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
207
208             // my vars (dec)
209
210             // set gobject values
211             this.el.homogeneous = false;
212             var child_0 = new Xcls_ScrolledWindow3( _this );
213             child_0.ref();
214             this.el.pack_start (  child_0.el , false,true,3 );
215         }
216
217         // user defined functions
218     }
219     public class Xcls_ScrolledWindow3 : Object
220     {
221         public Gtk.ScrolledWindow el;
222         private Xcls_DialogPluginWebkit  _this;
223
224
225             // my vars (def)
226
227         // ctor
228         public Xcls_ScrolledWindow3(Xcls_DialogPluginWebkit _owner )
229         {
230             _this = _owner;
231             this.el = new Gtk.ScrolledWindow( null, null );
232
233             // my vars (dec)
234
235             // set gobject values
236             this.el.expand = true;
237             var child_0 = new Xcls_webview( _this );
238             child_0.ref();
239             this.el.add (  child_0.el  );
240
241             // init method
242
243             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
244         }
245
246         // user defined functions
247     }
248     public class Xcls_webview : Object
249     {
250         public WebKit.WebView el;
251         private Xcls_DialogPluginWebkit  _this;
252
253
254             // my vars (def)
255
256         // ctor
257         public Xcls_webview(Xcls_DialogPluginWebkit _owner )
258         {
259             _this = _owner;
260             _this.webview = this;
261             this.el = new WebKit.WebView();
262
263             // my vars (dec)
264
265             // set gobject values
266
267             // init method
268
269             {
270                 // this may not work!?
271                 var settings =  this.el.get_settings();
272                 settings.enable_write_console_messages_to_stdout = true;
273                  
274                 var fs= new FakeServer(this.el);
275                 fs.ref();
276                 // this was an attempt to change the url perms.. did not work..
277                 // settings.enable_file_access_from_file_uris = true;
278                 // settings.enable_offline_web_application_cache - true;
279                 // settings.enable_universal_access_from_file_uris = true;
280                
281                  
282                 
283                 
284                 
285             
286                  // FIXME - base url of script..
287                  // we need it so some of the database features work.
288                 this.el.load_html( "Render not ready" , 
289                         //fixme - should be a config option!
290                         // or should we catch stuff and fix it up..
291                         "xhttp://localhost/app.Builder/"
292                 );
293                     
294                     
295                 
296                 
297             }
298
299             //listeners
300             this.el.script_dialog.connect( (dialog) => {
301                 if (this.el == null) {
302                     return true;
303                 }
304                 
305                  var msg = dialog.get_message();
306                  if (msg.length < 4) {
307                     return false;
308                  }
309                  if (msg.substring(0,4) != "IPC:") {
310                      return false;
311                  }
312                  var ar = msg.split(":", 3);
313                 if (ar.length < 3) {
314                     return false;
315                 }
316                 print("CMD: %s\n",ar[1]);
317                 print("ARGS: %s\n",ar[2]);
318                 switch(ar[1]) {
319                 
320                     case "SAVEHTML":
321                         // print("%sw",ar[2]);
322                         //  _this.file.saveHTML(ar[2]);
323                         return true;
324                         
325                     case "OUT":
326                         _this.result_json = ar[2];
327                         return true;
328                         
329                     default:
330                         return true;
331                 }
332                 
333             });
334         }
335
336         // user defined functions
337     }
338
339
340
341     public class Xcls_Button5 : Object
342     {
343         public Gtk.Button el;
344         private Xcls_DialogPluginWebkit  _this;
345
346
347             // my vars (def)
348
349         // ctor
350         public Xcls_Button5(Xcls_DialogPluginWebkit _owner )
351         {
352             _this = _owner;
353             this.el = new Gtk.Button();
354
355             // my vars (dec)
356
357             // set gobject values
358             this.el.label = "Reload";
359         }
360
361         // user defined functions
362     }
363
364     public class Xcls_Button6 : Object
365     {
366         public Gtk.Button el;
367         private Xcls_DialogPluginWebkit  _this;
368
369
370             // my vars (def)
371
372         // ctor
373         public Xcls_Button6(Xcls_DialogPluginWebkit _owner )
374         {
375             _this = _owner;
376             this.el = new Gtk.Button();
377
378             // my vars (dec)
379
380             // set gobject values
381             this.el.label = "Cancel";
382         }
383
384         // user defined functions
385     }
386
387     public class Xcls_Button7 : Object
388     {
389         public Gtk.Button el;
390         private Xcls_DialogPluginWebkit  _this;
391
392
393             // my vars (def)
394
395         // ctor
396         public Xcls_Button7(Xcls_DialogPluginWebkit _owner )
397         {
398             _this = _owner;
399             this.el = new Gtk.Button();
400
401             // my vars (dec)
402
403             // set gobject values
404             this.el.label = "OK";
405         }
406
407         // user defined functions
408     }
409
410 }