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