Attribute changed old-javascript
[app.Builder.js] / old-javascript / Palete / Base.js
1 //<Script type="text/javascript">
2  
3 console = imports.console;
4 XObject = imports.XObject.XObject;
5  
6 GLib = imports.gi.GLib; 
7 File = imports.File.File;
8
9 // Palete Provider..
10 Base = XObject.define(
11     function(cfg) {
12     
13         XObject.extend(this, cfg);
14         // various loader methods..
15    
16     },
17     Object, 
18     {
19         
20         
21         map : false, // array of mappings   left: [] , right : []
22         
23         items : false, // the tree of nodes.
24         
25         
26         guessName : function(ar) // turns the object into full name.
27         {
28              // eg. xns: Roo, xtype: XXX -> Roo.xxx
29             if (typeof( ar['|xns'] ) == 'undefined' || typeof( ar['xtype'] ) == 'undefined') {
30                 return '';
31                }
32              
33             return ar['|xns'] +'.' + ar['xtype'];
34                             
35                                  
36         },
37         /**
38          * gather a  list of potentional objects that can be added..
39          * 
40          */
41         gatherList: function (existing) {
42             existing = existing || [];
43            // existing.push('*top'); // always have top
44             var ret  = []; 
45             console.log("GATHER LIST? " + this.map.length);
46             
47             
48             function addRight(right) {
49                 right.forEach(function(r) {
50                     if (ret.indexOf(r) > -1) {
51                         return;
52                     }
53                     ret.push(r);
54                 });
55             }
56             
57             this.map.forEach(function(m) {
58                 var done = false
59                 m.left.forEach( function(left) {
60                     if (done) return; 
61                     
62                     var l = left.replace(/:.*$/, '');
63                    // print("chk:" + l + " in " + existing.join(',')); 
64                     if (existing.indexOf(l) > -1) {
65                         addRight(m.right);
66                         done =true;
67                         //return true; // no more needed..
68                     }
69                 });
70                 
71             });
72             ret.sort();
73             
74            // console.dump(ret);
75             return ret;
76             
77             
78             
79         },
80         
81         getDropList : function(rval)
82         {
83             
84             var ret = [];
85             this.map.forEach( function(m) {
86                 if (m.right.indexOf(rval) > -1) {
87                     m.left.forEach(function(l) {
88                         if (ret.indexOf(l) > -1) {
89                             return;
90                         }
91                         ret.push(l)
92                     });
93                 }
94                 
95             });
96             console.log("DROP LIST:");
97             console.dump(ret);
98             return ret;
99             
100         },
101         /**
102          * basic guess type.. 
103          * 
104          */
105         findType : function (data, prop, value)
106         {
107             if (prop[0] == '|') {
108                 return 'function';
109             }
110             return typeof(value);
111         },
112         
113         
114         findOptions : function(ename)
115         {
116             switch(ename.toLowerCase()) {
117                 case 'boolean': 
118                     return [ 'true', 'false' ];
119                 // everything else does not have options.
120                 case 'string': 
121                 case 'utf8': 
122                 case 'int': 
123                 case 'uint': 
124                 case 'function': 
125                     return false;
126                 default: 
127                     console.log("OOPS: = unknown type: " + ename);
128                     return false;
129             }
130         },
131         confirmCanAdd: function(parent, child) {
132             // confirms that one obj can be added to another.
133             // returns true, for items, or list of properties that can hold it..
134             return true;
135             
136         },
137         getDefaultPack: function(pname, cname) {
138             return 'add';
139         },
140         saveTemplate: function(name, data)
141         {
142             var gn = this.guessName(JSON.parse(data));
143             // store it in user's directory..
144             var appdir = GLib.get_home_dir() + '/.Builder'; 
145             
146             if (!File.isDirectory(appdir+ '/' + gn)) {
147                 File.mkdir(appdir+ '/' + gn);
148             }
149             File.write(appdir+ '/' + gn + '/' +  name + '.json', data);
150             
151         },
152         /**
153          * list templates - in home directory (and app dir in future...)
154          * @param {String} name  - eg. Gtk.Window..
155          * @return {Array} list of templates available..
156          */
157         listTemplates : function(name)
158         {
159             
160             var gn = name;
161             if (typeof(gn) != 'string') {
162                 gn = this.guessName(gn);
163             }
164             
165             
166             var dir= GLib.get_home_dir() + '/.Builder/' + gn; 
167             if (!File.isDirectory(dir)) {
168                 return [];
169             }
170             var ret =  [];
171             File.list(dir).forEach(function(n) {
172                 if (!n.match(/\.json$/)) {
173                     return;
174                 }
175                 
176                 ret.push({
177                     path : dir + '/' + n,
178                     name:  n.replace(/\.json$/,'')
179                 });
180             });
181             return ret;
182             
183         },
184         loadTemplate : function(path)
185         {
186             return JSON.parse(File.read(path));
187         }
188         
189         
190     }
191 );
192
193
194