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                 this.success(_this.project, nf);
147                 //tmpl.copyTo(dir + '/' + xidns + '.bjs', function() {
148                 //    tmpl.setNSID(xidns);
149                 ///    _this.project.addFile(tmpl);
150                 //    this.success(_this.project, tmpl);
151                 //});
152                 
153                 
154                 
155                 
156             }
157             
158             
159             
160         },
161         
162        
163      
164         items : [
165             {
166                 
167                 xtype : Gtk.VBox,
168                 
169                 pack: function(p,e) {
170                     p.el.get_content_area().add(e.el)
171                 },
172                 items : [
173                     {
174                         xtype : Gtk.HBox,
175                         pack : [ 'pack_start', false, true , 0 ],
176                         
177                         items : [
178                             {
179                                 xtype : Gtk.Label,
180                                 label : "Component Name:",
181                                 pack : [ 'pack_start', false, true , 0 ]
182                                 
183                             },
184                             
185                             {
186                                 id : 'xnsid',
187                                 xtype : Gtk.Entry,
188                                 pack : [ 'pack_end', true, true , 0 ],
189                                 setValue : function(v) 
190                                 {
191                                     this.el.set_text(v);
192                                 },
193                                 getValue : function()
194                                 {
195                                     return this.el.get_text();
196                                 }
197                             }
198                          
199                         ]
200                         
201                     },
202                     {
203                         xtype : Gtk.HBox,
204                         pack : [ 'pack_start', false, true , 0 ],
205                         
206                         items : [
207                             {
208                                 xtype : Gtk.Label,
209                                 label : "Using Template:",
210                                 pack : [ 'pack_start', false, true , 0 ],
211                                 
212                             },
213                             
214                             
215                             {
216                                 id : 'template',
217                                 xtype : Gtk.ComboBox,
218                                 pack : [ 'pack_end', true, true , 0 ],
219                                 init : function()
220                                 {
221                                     XObject.prototype.init.call(this); 
222                                     this.el.add_attribute(this.items[0].el , 'markup', 1 );  
223                                        
224                                 },
225                             
226                                 setValue : function(v)
227                                 {
228                                     var el = this.el;
229                                     el.set_active(-1);
230                                     DialogNewComponent.get('template_model').templates.forEach(
231                                         function(n, ix) {
232                                             if (v == n ) {
233                                                 el.set_active(ix);
234                                                 return false;
235                                             }
236                                         }
237                                     );
238                                 },
239                                 getValue : function() 
240                                 {
241                                     var ix = this.el.get_active();
242                                     if (ix < 0 ) {
243                                         return '';
244                                     }
245                                     return DialogNewComponent.get('template_model').templates[ix];
246                                   
247                                 },
248                                 
249                                  
250                                 items : [
251                                     {
252                                         
253                                         xtype : Gtk.CellRendererText,
254                                         pack : ['pack_start'],
255                                         
256                                     },
257                                     {
258                                         id : 'template_model',
259                                         pack : [ 'set_model' ],
260                                         xtype : Gtk.ListStore,
261                                         
262                                         init :   function ()
263                                         {
264                                             XObject.prototype.init.call(this); 
265                                             this.el.set_column_types ( 2, [
266                                                     GObject.TYPE_STRING,  // real key
267                                                     GObject.TYPE_STRING // real type
268                                             ] );
269                                              
270                                             
271                                         
272                                         },
273                                        
274                                         templates : false,
275                                         
276                                         loadData : function () {
277                                             this.el.clear();
278                                             var iter = new Gtk.TreeIter();
279                                             var el = this.el;
280                                             this.templates.forEach(function(p) {
281                                                 
282                                                 el.append(iter);
283                                                 
284                                                 el.set_value(iter, 0, p);
285                                                 el.set_value(iter, 1, p);
286                                                 
287                                             });
288                                              
289                                             
290                                             
291                                         }
292                                          
293                                     }
294                                   
295                                          
296                                 ]
297                             }
298                  
299                                 
300                         ]
301                     },
302                     {
303                         xtype : Gtk.HBox,
304                         
305                         pack : [ 'pack_start', false, true , 0 ],
306                         
307                         items : [
308                             {
309                                 xtype : Gtk.Label,
310                                 pack : [ 'pack_start', false, true , 0 ],
311                                 label : "In Directory:"
312                             },
313                             
314                             {
315                                 id : 'directory',
316                                 
317                                 xtype : Gtk.ComboBox,
318                                 pack : [ 'pack_end', true, true , 0 ],
319                             
320                                  init : function()
321                                 {
322                                     XObject.prototype.init.call(this); 
323                                    this.el.add_attribute(this.items[0].el , 'markup', 1 );  
324                                        
325                                 },
326                             
327                                 setValue : function(v)
328                                 {
329                                     var el = this.el;
330                                     el.set_active(-1);
331                                     DialogNewComponent.get('directory_model').data.forEach(
332                                         function(n,ix) {
333                                             if (v == n.xtype) {
334                                                 el.set_active(ix);
335                                                 return false;
336                                             }
337                                         }
338                                     );
339                                 },
340                                 getValue : function() 
341                                 {
342                                     var ix = this.el.get_active();
343                                     if (ix < 0 ) {
344                                         return '';
345                                     }
346                                     var data = DialogNewComponent.get('directory_model').data;
347                                     
348                                     return  data[ix].desc;
349                                     
350                                 },
351                                 
352                                  
353                                 items : [
354                                     {
355                                         xtype : Gtk.CellRendererText,
356                                         pack : ['pack_start'],
357                                     },
358                                     {
359                                         
360                                         xtype : Gtk.ListStore,
361                                         pack : [ 'set_model' ],
362                                         id: 'directory_model',
363                                         init : function()
364                                         {
365                                             XObject.prototype.init.call(this); 
366                                             this.el.set_column_types ( 2, [
367                                                 GObject.TYPE_STRING,  // real key
368                                                 GObject.TYPE_STRING // real type
369                                             ]); 
370                                         },
371                                      
372                                        
373                                        
374                                         
375                                         loadData : function (data) {
376                                             this.el.clear();
377                                             this.data   = data;
378                                             var iter = new Gtk.TreeIter();
379                                             var el = this.el;
380                                             data.forEach( function(p) {
381                                                 
382                                                 el.append(iter);
383                                                 
384                                                  
385                                                 el.set_value(iter, 0, p.id);
386                                                 el.set_value(iter, 1, p.desc);
387                                                 
388                                             });
389                                              
390                                         }
391                                          
392                                     }
393                                   
394                                          
395                                 ]
396                             }
397                  
398                                 
399                         ]
400                     },
401                     {
402                         xtype : Gtk.Label,
403                         pack : [ 'pack_end', true, true , 0 ],
404                         label : ""
405                     }
406                     
407                     
408                 ]
409             }
410         ]
411     }
412 );
413     
414
415
416 //XN.xnew(create());
417 //_win = XN.xnew(create());