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