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