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