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