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