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 : 
261                                         listeners :  function ()
262                                         {
263                                             XObject.prototype.init.call(this); 
264                                             this.el.set_column_types ( 2, [
265                                                     GObject.TYPE_STRING,  // real key
266                                                     GObject.TYPE_STRING // real type
267                                             ] );
268                                              
269                                             
270                                         
271                                         },
272                                        
273                                         templates : false,
274                                         
275                                         loadData : function () {
276                                             this.el.clear();
277                                             var iter = new Gtk.TreeIter();
278                                             var el = this.el;
279                                             this.templates.forEach(function(p) {
280                                                 
281                                                 el.append(iter);
282                                                 
283                                                 el.set_value(iter, 0, p);
284                                                 el.set_value(iter, 1, p);
285                                                 
286                                             });
287                                              
288                                             
289                                             
290                                         }
291                                          
292                                     }
293                                   
294                                          
295                                 ]
296                             }
297                  
298                                 
299                         ]
300                     },
301                     {
302                         xtype : Gtk.HBox,
303                         
304                         pack : [ 'pack_start', false, true , 0 ],
305                         
306                         items : [
307                             {
308                                 xtype : Gtk.Label,
309                                 pack : [ 'pack_start', false, true , 0 ],
310                                 label : "In Directory:"
311                             },
312                             
313                             {
314                                 id : 'directory',
315                                 
316                                 xtype : Gtk.ComboBox,
317                                 pack : [ 'pack_end', true, true , 0 ],
318                             
319                                  init : function()
320                                 {
321                                     XObject.prototype.init.call(this); 
322                                    this.el.add_attribute(this.items[0].el , 'markup', 1 );  
323                                        
324                                 },
325                             
326                                 setValue : function(v)
327                                 {
328                                     var el = this.el;
329                                     el.set_active(-1);
330                                     DialogNewComponent.get('directory_model').data.forEach(
331                                         function(n,ix) {
332                                             if (v == n.xtype) {
333                                                 el.set_active(ix);
334                                                 return false;
335                                             }
336                                         }
337                                     );
338                                 },
339                                 getValue : function() 
340                                 {
341                                     var ix = this.el.get_active();
342                                     if (ix < 0 ) {
343                                         return '';
344                                     }
345                                     return  DialogNewComponent.get('directory_model').data[ix].xtype;
346                                     
347                                 },
348                                 
349                                  
350                                 items : [
351                                     {
352                                         xtype : Gtk.CellRendererText,
353                                         pack : ['pack_start'],
354                                     },
355                                     {
356                                         
357                                         xtype : Gtk.ListStore,
358                                         pack : [ 'set_model' ],
359                                         id: 'directory_model',
360                                         init : function()
361                                         {
362                                             XObject.prototype.init.call(this); 
363                                             this.el.set_column_types ( 2, [
364                                                 GObject.TYPE_STRING,  // real key
365                                                 GObject.TYPE_STRING // real type
366                                             ]); 
367                                         },
368                                      
369                                        
370                                        
371                                         
372                                         loadData : function (data) {
373                                             this.el.clear();
374                                             var iter = new Gtk.TreeIter();
375                                             var el = this.el;
376                                             data.forEach( function(p) {
377                                                 
378                                                 el.append(iter);
379                                                 
380                                                  
381                                                 el.set_value(iter, 0, p.id);
382                                                 el.set_value(iter, 1, p.desc);
383                                                 
384                                             });
385                                              
386                                         }
387                                          
388                                     }
389                                   
390                                          
391                                 ]
392                             }
393                  
394                                 
395                         ]
396                     },
397                     {
398                         xtype : Gtk.Label,
399                         pack : [ 'pack_end', true, true , 0 ],
400                         label : ""
401                     }
402                     
403                     
404                 ]
405             }
406         ]
407     }
408 );
409     
410
411
412 //XN.xnew(create());
413 //_win = XN.xnew(create());