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