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