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