add network templates to xoslib
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
1 if (! window.XOSLIB_LOADED ) {
2     window.XOSLIB_LOADED=true;
3
4     SLIVER_API = "/plstackapi/slivers/";
5     SLICE_API = "/plstackapi/slices/";
6     NODE_API = "/plstackapi/nodes/";
7     SITE_API = "/plstackapi/sites/";
8     USER_API = "/plstackapi/users/";
9     DEPLOYMENT_API = "/plstackapi/deployments";
10     IMAGE_API = "/plstackapi/images";
11     NETWORKTEMPLATE_API = "/plstackapi/networktemplates";
12     NETWORK_API = "/plstackapi/networks";
13     SERVICE_API = "/plstackapi/services";
14
15     SLICEPLUS_API = "/xoslib/slicesplus/";
16
17     XOSModel = Backbone.Model.extend({
18         /* from backbone-tastypie.js */
19         //idAttribute: 'resource_uri',
20
21         /* from backbone-tastypie.js */
22         url: function() {
23                     var url = this.attributes.resource_uri;
24
25                     if (!url) {
26                         url = this.urlRoot + this.id;
27                     }
28
29                     if (!url) {
30                         // XXX I'm not sure this does anything useful
31                         url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
32                         url = url || this.urlRoot;
33                     }
34
35                     // remove any existing query parameters
36                     url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
37
38                     url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
39
40                     url && ( url += "?no_hyperlinks=1" );
41
42                     return url;
43             },
44
45             listMethods: function() {
46                 var res = [];\r
47                 for(var m in this) {\r
48                     if(typeof this[m] == "function") {\r
49                         res.push(m)\r
50                     }\r
51                 }\r
52                 return res;\r
53             }
54     });
55
56     XOSCollection = Backbone.Collection.extend({
57         objects: function() {
58                     return this.models.map(function(element) { return element.attributes; });
59                  },
60
61         initialize: function(){
62           this.sortVar = 'name';\r
63           this.sortOrder = 'asc';\r
64         },\r
65 \r
66         simpleComparator: function( model ){\r
67           parts=this.sortVar.split(".");\r
68           result = model.get(parts[0]);\r
69           for (index=1; index<parts.length; ++index) {\r
70               result=result[parts[index]];\r
71           }\r
72           return result;\r
73         },\r
74 \r
75         comparator: function (left, right) {\r
76             var l = this.simpleComparator(left);\r
77             var r = this.simpleComparator(right);\r
78 \r
79             if (l === void 0) return -1;\r
80             if (r === void 0) return 1;\r
81 \r
82             if (this.sortOrder=="desc") {\r
83                 return l < r ? 1 : l > r ? -1 : 0;\r
84             } else {\r
85                 return l < r ? -1 : l > r ? 1 : 0;\r
86             }\r
87         },\r
88 \r
89         startPolling: function() {\r
90             if (!this._polling) {
91                 collection=this;
92                 setInterval(function() { collection.fetch(); }, 10000);
93                 this._polling=true;
94                 this.fetch();
95             }
96         },
97
98         maybeFetch: function(options){
99                 // Helper function to fetch only if this collection has not been fetched before.
100             if(this._fetched){
101                     // If this has already been fetched, call the success, if it exists
102                 options.success && options.success();
103                 console.log("alreadyFetched");
104                 return;
105             }
106
107                 // when the original success function completes mark this collection as fetched
108             var self = this,
109             successWrapper = function(success){
110                 return function(){
111                     self._fetched = true;
112                     success && success.apply(this, arguments);
113                 };
114             };
115             options.success = successWrapper(options.success);
116             console.log("call fetch");
117             this.fetch(options);
118         },
119
120         getOrFetch: function(id, options){
121                 // Helper function to use this collection as a cache for models on the server
122             var model = this.get(id);
123
124             if(model){
125                 options.success && options.success(model);
126                 return;
127             }
128
129             model = new this.model({
130                 resource_uri: id
131             });
132
133             model.fetch(options);
134         },
135
136         /* from backbone-tastypie.js */
137         url: function( models ) {
138                     var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
139                     url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
140
141                     // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
142                     // (set to 'resource_uri') contains the model's id.
143                     if ( models && models.length ) {
144                             var ids = _.map( models, function( model ) {
145                                             var parts = _.compact( model.id.split('/') );
146                                             return parts[ parts.length - 1 ];
147                                     });
148                             url += 'set/' + ids.join(';') + '/';
149                     }
150
151                     url && ( url += "?no_hyperlinks=1" );
152
153                     return url;
154             },
155
156             listMethods: function() {
157                 var res = [];\r
158                 for(var m in this) {\r
159                     if(typeof this[m] == "function") {\r
160                         res.push(m)\r
161                     }\r
162                 }\r
163                 return res;\r
164             }
165     });
166
167     function xoslib() {
168         // basic REST
169         this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
170         this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
171                                                        model: this.sliver});
172         this.slivers = new this.sliverCollection();
173
174         this.slice = XOSModel.extend({ urlRoot: SLICE_API });
175         this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
176                                                        model: this.slice});
177         this.slices = new this.sliceCollection();
178
179         this.node = XOSModel.extend({ urlRoot: NODE_API });
180         this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
181                                                        model: this.node});
182         this.nodes = new this.nodeCollection();
183
184         this.site = XOSModel.extend({ urlRoot: SITE_API });
185         this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
186                                                        model: this.site});
187         this.sites = new this.siteCollection();
188
189         this.user = XOSModel.extend({ urlRoot: USER_API });
190         this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
191                                                        model: this.user});
192         this.users = new this.userCollection();
193
194         this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
195         this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
196                                                            model: this.deployment});
197         this.deployments = new this.deploymentCollection();
198
199         this.image = XOSModel.extend({ urlRoot: IMAGE_API });
200         this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
201                                                            model: this.image});
202         this.images = new this.imageCollection();
203
204         this.networkTemplate = XOSModel.extend({ urlRoot: NETWORKTEMPLATE_API });
205         this.networkTemplateCollection = XOSCollection.extend({ urlRoot: NETWORKTEMPLATE_API,
206                                                            model: this.networkTemplate});
207         this.networkTemplates = new this.networkTemplateCollection();
208
209         this.network = XOSModel.extend({ urlRoot: NETWORK_API });
210         this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
211                                                            model: this.network});
212         this.networks = new this.networkCollection();
213
214         this.service = XOSModel.extend({ urlRoot: SERVICE_API });
215         this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
216                                                        model: this.service});
217         this.services = new this.serviceCollection();
218
219         // enhanced REST
220         this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
221         this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
222                                                           model: this.slicePlus});
223         this.slicesPlus = new this.slicePlusCollection();
224
225         this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
226     };
227
228     xos = new xoslib();
229 }