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