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