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