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