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