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