sync
[app.Builder.js] / oldbuilder / DialogNewComponent.js
1 //<Script type="text/javascript">
2
3 Gtk = imports.gi.Gtk;
4 GObject = imports.gi.GObject;
5 Gio = imports.gi.Gio;
6 GLib = imports.gi.GLib;
7
8 console = imports.console;
9 XObject = imports.XObject.XObject;
10
11
12 StandardErrorDialog = imports.Builder.StandardErrorDialog.StandardErrorDialog;
13 /**
14  * add a component
15  * 
16  * basically uses a standard template ? pulled from the web?
17  * 
18  * you have to pick which template, and give it a name..
19  * 
20  * and pick which directory to put it in?
21  * 
22  */
23
24 DialogNewComponent = new XObject({
25         
26         xtype : Gtk.Dialog,
27       //  type: Gtk.WindowType.TOPLEVEL,
28         deletable : false,
29         modal : true,
30         title  : "New Component",
31         border_width : 0,
32         project : false,
33         init : function()
34         {
35             XObject.debug = true;
36             XObject.prototype.init.call(this); 
37             this.el.add_button("OK",1 );
38             this.el.add_button("Cancel",0 );
39             
40             this.el.set_default_size (600, 400);
41             console.log('shown all');
42             //show_all : []
43         },
44        
45         show : function (c) 
46         {
47             if (!this.el) {
48                 this.init();
49             }
50             c = c || { name : '' , xtype : '' };
51             // check whic project we are adding to..
52             XObject.extend(this, c);
53             // causes problems.. get_screen?? not transfer ownership?
54            /// var Window                = imports.Builder.Window.Window;
55             //this.el.set_screen(Window.el.get_screen());
56             
57             var paths = [];
58             for (var i in this.project.paths) {
59                 paths.push({
60                     id : i,
61                     desc : i
62                 });
63             }
64              console.log('load paths');
65              
66             // load the paths.
67             this.get('directory_model').loadData(paths);
68                 
69             
70             console.log('show all');
71             this.el.show_all();
72             this.success = c.success;
73             /*
74             var tm = this.get('template_model');
75             if (tm.templates) {
76                 return;
77             }
78             tm.templates = [];
79             var dir = __script_path__ + '/templates/';
80             
81             var f = Gio.file_new_for_path(dir);
82             f.enumerate_children_async ("*",  Gio.FileQueryInfoFlags.NONE, 
83                     GLib.PRIORITY_DEFAULT, null, function(o,ar) {
84                 // enum completed..
85                 var fe = f.enumerate_children_finish(ar);
86                 var ch = '';
87                 while (ch = fe.next_file(null)) {
88                     var add = dir + '/' + ch.get_name();
89                     if (!add.match(/\.js$/)) {
90                         continue;
91                     }
92                     tm.templates.push(add);
93                     
94                 }
95                 tm.loadData();
96                 
97             }, null);
98             */
99             
100         },
101         
102         listeners : 
103         {
104             'delete-event' : function (widget, event) {
105                 this.el.hide();
106                 return true;
107             },
108             
109             response : function (w, id) 
110             {
111                 if (id < 1) { // cancel!
112                     this.el.hide();
113                     return;
114                 }
115                 
116                 
117                     
118                    
119             
120                 if (!DialogNewComponent.get('xnsid').el.get_text().length || 
121                //    DialogNewComponent.get('template').getValue().length ||
122                    !DialogNewComponent.get('directory').getValue().length 
123                 ) {
124                     StandardErrorDialog.show(
125                         "You have to set Project name ,template and directory"
126                     );
127                      
128                     return;
129                 }
130                 
131                 var dir = DialogNewComponent.get('directory').getValue();
132                 var xidns = DialogNewComponent.get('xnsid').el.get_text();
133                 
134                 
135                  if (GLib.file_test (GLib.dir + '/' + xidns + '.bjs', GLib.FileTest.EXISTS)) {
136                     StandardErrorDialog.show(
137                         "That file already exists"
138                     ); 
139                     return;
140                 }
141                 this.el.hide();
142                 
143                 
144                 //var tmpl = this.project.loadFileOnly(DialogNewComponent.get('template').getValue());
145                 
146                 var _this = this;
147                 var nf = _this.project.create(dir + '/' + xidns + '.bjs');
148                 if (DialogNewComponent.success) {
149                     DialogNewComponent.success(_this.project, nf);
150                 }
151                 
152                 //tmpl.copyTo(dir + '/' + xidns + '.bjs', function() {
153                 //    tmpl.setNSID(xidns);
154                 ///    _this.project.addFile(tmpl);
155                 //    this.success(_this.project, tmpl);
156                 //});
157                 
158                 
159                 
160                 
161             }
162             
163             
164             
165         },
166         
167        
168      
169         items : [
170             {
171                 
172                 xtype : Gtk.VBox,
173                 
174                 pack: function(p,e) {
175                     p.el.get_content_area().add(e.el)
176                 },
177                 items : [
178                     {
179                         xtype : Gtk.HBox,
180                         pack : [ 'pack_start', false, true , 0 ],
181                         
182                         items : [
183                             {
184                                 xtype : Gtk.Label,
185                                 label : "Component Name:",
186                                 pack : [ 'pack_start', false, true , 0 ]
187                                 
188                             },
189                             
190                             {
191                                 id : 'xnsid',
192                                 xtype : Gtk.Entry,
193                                 pack : [ 'pack_end', true, true , 0 ],
194                                 setValue : function(v) 
195                                 {
196                                     this.el.set_text(v);
197                                 },
198                                 getValue : function()
199                                 {
200                                     return this.el.get_text();
201                                 }
202                             }
203                          
204                         ]
205                         
206                     },
207                     {
208                         xtype : Gtk.HBox,
209                         pack : [ 'pack_start', false, true , 0 ],
210                         
211                         items : [
212                             {
213                                 xtype : Gtk.Label,
214                                 label : "Using Template:",
215                                 pack : [ 'pack_start', false, true , 0 ],
216                                 
217                             },
218                             
219                             
220                             {
221                                 id : 'template',
222                                 xtype : Gtk.ComboBox,
223                                 pack : [ 'pack_end', true, true , 0 ],
224                                 init : function()
225                                 {
226                                     XObject.prototype.init.call(this); 
227                                     this.el.add_attribute(this.items[0].el , 'markup', 1 );  
228                                        
229                                 },
230                             
231                                 setValue : function(v)
232                                 {
233                                     var el = this.el;
234                                     el.set_active(-1);
235                                     DialogNewComponent.get('template_model').templates.forEach(
236                                         function(n, ix) {
237                                             if (v == n ) {
238                                                 el.set_active(ix);
239                                                 return false;
240                                             }
241                                         }
242                                     );
243                                 },
244                                 getValue : function() 
245                                 {
246                                     var ix = this.el.get_active();
247                                     if (ix < 0 ) {
248                                         return '';
249                                     }
250                                     return DialogNewComponent.get('template_model').templates[ix];
251                                   
252                                 },
253                                 
254                                  
255                                 items : [
256                                     {
257                                         
258                                         xtype : Gtk.CellRendererText,
259                                         pack : ['pack_start'],
260                                         
261                                     },
262                                     {
263                                         id : 'template_model',
264                                         pack : [ 'set_model' ],
265                                         xtype : Gtk.ListStore,
266                                         
267                                         init :   function ()
268                                         {
269                                             XObject.prototype.init.call(this); 
270                                             this.el.set_column_types ( 2, [
271                                                     GObject.TYPE_STRING,  // real key
272                                                     GObject.TYPE_STRING // real type
273                                             ] );
274                                              
275                                             
276                                         
277                                         },
278                                        
279                                         templates : false,
280                                         
281                                         loadData : function () {
282                                             this.el.clear();
283                                             var iter = new Gtk.TreeIter();
284                                             var el = this.el;
285                                             this.templates.forEach(function(p) {
286                                                 
287                                                 el.append(iter);
288                                                 
289                                                 el.set_value(iter, 0, p);
290                                                 el.set_value(iter, 1, p);
291                                                 
292                                             });
293                                              
294                                             
295                                             
296                                         }
297                                          
298                                     }
299                                   
300                                          
301                                 ]
302                             }
303                  
304                                 
305                         ]
306                     },
307                     {
308                         xtype : Gtk.HBox,
309                         
310                         pack : [ 'pack_start', false, true , 0 ],
311                         
312                         items : [
313                             {
314                                 xtype : Gtk.Label,
315                                 pack : [ 'pack_start', false, true , 0 ],
316                                 label : "In Directory:"
317                             },
318                             
319                             {
320                                 id : 'directory',
321                                 
322                                 xtype : Gtk.ComboBox,
323                                 pack : [ 'pack_end', true, true , 0 ],
324                             
325                                  init : function()
326                                 {
327                                     XObject.prototype.init.call(this); 
328                                    this.el.add_attribute(this.items[0].el , 'markup', 1 );  
329                                        
330                                 },
331                             
332                                 setValue : function(v)
333                                 {
334                                     var el = this.el;
335                                     el.set_active(-1);
336                                     this.get('directory_model').data.forEach(
337                                         function(n,ix) {
338                                             if (v == n.xtype) {
339                                                 el.set_active(ix);
340                                                 return false;
341                                             }
342                                         }
343                                     );
344                                 },
345                                 getValue : function() 
346                                 {
347                                     var ix = this.el.get_active();
348                                     if (ix < 0 ) {
349                                         return '';
350                                     }
351                                     var data = this.get('directory_model').data;
352                                     
353                                     return  data[ix].desc;
354                                     
355                                 },
356                                 
357                                  
358                                 items : [
359                                     {
360                                         xtype : Gtk.CellRendererText,
361                                         pack : ['pack_start'],
362                                     },
363                                     {
364                                         
365                                         xtype : Gtk.ListStore,
366                                         pack : [ 'set_model' ],
367                                         id: 'directory_model',
368                                         init : function()
369                                         {
370                                             XObject.prototype.init.call(this); 
371                                             this.el.set_column_types ( 2, [
372                                                 GObject.TYPE_STRING,  // real key
373                                                 GObject.TYPE_STRING // real type
374                                             ]); 
375                                         },
376                                      
377                                        
378                                        
379                                         
380                                         loadData : function (data) {
381                                             this.el.clear();
382                                             this.data   = data;
383                                             var iter = new Gtk.TreeIter();
384                                             var el = this.el;
385                                             data.forEach( function(p) {
386                                                 
387                                                 el.append(iter);
388                                                 
389                                                  
390                                                 el.set_value(iter, 0, p.id);
391                                                 el.set_value(iter, 1, p.desc);
392                                                 
393                                             });
394                                              
395                                         }
396                                          
397                                     }
398                                   
399                                          
400                                 ]
401                             }
402                  
403                                 
404                         ]
405                     },
406                     {
407                         xtype : Gtk.Label,
408                         pack : [ 'pack_end', true, true , 0 ],
409                         label : ""
410                     }
411                     
412                     
413                 ]
414             }
415         ]
416     }
417 );
418     
419
420
421 //XN.xnew(create());
422 //_win = XN.xnew(create());