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