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