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