1 if (! window.XOSLIB_LOADED ) {
2 window.XOSLIB_LOADED=true;
4 SLIVER_API = "/plstackapi/slivers/";
5 SLICE_API = "/plstackapi/slices/";
6 SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
7 SLICEPRIVILEGE_API = "/plstackapi/slice_privileges/";
8 SLICEROLE_API = "/plstackapi/slice_roles/";
9 NODE_API = "/plstackapi/nodes/";
10 SITE_API = "/plstackapi/sites/";
11 USER_API = "/plstackapi/users/";
12 USERDEPLOYMENT_API = "/plstackapi/user_deployments/";
13 DEPLOYMENT_API = "/plstackapi/deployments/";
14 IMAGE_API = "/plstackapi/images/";
15 NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
16 NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/";
17 NETWORK_API = "/plstackapi/networks/";
18 NETWORKSLIVER_API = "/plstackapi/networkslivers/";
19 SERVICE_API = "/plstackapi/services/";
21 SLICEPLUS_API = "/xoslib/slicesplus/";
23 XOSModel = Backbone.Model.extend({
24 /* from backbone-tastypie.js */
25 //idAttribute: 'resource_uri',
27 /* from backbone-tastypie.js */
29 var url = this.attributes.resource_uri;
32 url = this.urlRoot + this.id;
36 // XXX I'm not sure this does anything useful
37 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
38 url = url || this.urlRoot;
41 // remove any existing query parameters
42 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
44 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
46 url && ( url += "?no_hyperlinks=1" );
51 listMethods: function() {
53 for(var m in this) {
\r
54 if(typeof this[m] == "function") {
\r
62 XOSCollection = Backbone.Collection.extend({
64 return this.models.map(function(element) { return element.attributes; });
67 initialize: function(){
68 this.sortVar = 'name';
\r
69 this.sortOrder = 'asc';
\r
72 relatedCollections: [],
\r
73 foreignCollections: [],
\r
75 simpleComparator: function( model ){
\r
76 parts=this.sortVar.split(".");
\r
77 result = model.get(parts[0]);
\r
78 for (index=1; index<parts.length; ++index) {
\r
79 result=result[parts[index]];
\r
84 comparator: function (left, right) {
\r
85 var l = this.simpleComparator(left);
\r
86 var r = this.simpleComparator(right);
\r
88 if (l === void 0) return -1;
\r
89 if (r === void 0) return 1;
\r
91 if (this.sortOrder=="desc") {
\r
92 return l < r ? 1 : l > r ? -1 : 0;
\r
94 return l < r ? -1 : l > r ? 1 : 0;
\r
98 startPolling: function() {
\r
99 if (!this._polling) {
\r
101 setInterval(function() { collection.fetch(); }, 10000);
107 maybeFetch: function(options){
108 // Helper function to fetch only if this collection has not been fetched before.
110 // If this has already been fetched, call the success, if it exists
111 options.success && options.success();
112 console.log("alreadyFetched");
116 // when the original success function completes mark this collection as fetched
118 successWrapper = function(success){
120 self._fetched = true;
121 success && success.apply(this, arguments);
124 options.success = successWrapper(options.success);
125 console.log("call fetch");
129 getOrFetch: function(id, options){
130 // Helper function to use this collection as a cache for models on the server
131 var model = this.get(id);
134 options.success && options.success(model);
138 model = new this.model({
142 model.fetch(options);
145 filterBy: function(fieldName, value) {
146 filtered = this.filter(function(obj) {
147 return obj.get(fieldName) == value;
149 return new this.constructor(filtered);
152 /* from backbone-tastypie.js */
153 url: function( models ) {
154 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
155 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
157 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
158 // (set to 'resource_uri') contains the model's id.
159 if ( models && models.length ) {
160 var ids = _.map( models, function( model ) {
161 var parts = _.compact( model.id.split('/') );
162 return parts[ parts.length - 1 ];
164 url += 'set/' + ids.join(';') + '/';
167 url && ( url += "?no_hyperlinks=1" );
172 listMethods: function() {
174 for(var m in this) {
\r
175 if(typeof this[m] == "function") {
\r
185 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
186 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
187 relatedCollections: {"networkSlivers": "sliver"},
188 foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
189 model: this.sliver});
190 this.slivers = new this.sliverCollection();
192 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
193 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
194 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice"},
195 foreignCollections: ["services", "sites"],
197 this.slices = new this.sliceCollection();
199 this.sliceDeployment = XOSModel.extend({ urlRoot: SLICEDEPLOYMENT_API });
200 this.sliceDeploymentCollection = XOSCollection.extend({ urlRoot: SLICEDEPLOYMENT_API,
201 foreignCollections: ["slices", "deployments"],
203 this.sliceDeployments = new this.sliceDeploymentCollection();
205 this.slicePrivilege = XOSModel.extend({ urlRoot: SLICEPRIVILEGE_API });
206 this.slicePrivilegeCollection = XOSCollection.extend({ urlRoot: SLICEPRIVILEGE_API,
207 foreignCollections: ["slices", "users", "sliceRoles"],
209 this.slicePrivileges = new this.slicePrivilegeCollection();
211 this.sliceRole = XOSModel.extend({ urlRoot: SLICEROLE_API });
212 this.sliceRoleCollection = XOSCollection.extend({ urlRoot: SLICEROLE_API,
214 this.sliceRoles = new this.sliceRoleCollection();
216 this.node = XOSModel.extend({ urlRoot: NODE_API });
217 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
218 foreignCollections: ["sites", "deployments"],
220 this.nodes = new this.nodeCollection();
222 this.site = XOSModel.extend({ urlRoot: SITE_API });
223 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
225 this.sites = new this.siteCollection();
227 this.user = XOSModel.extend({ urlRoot: USER_API });
228 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
229 relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"},
230 foreignCollections: ["sites"],
232 this.users = new this.userCollection();
234 this.userDeployment = XOSModel.extend({ urlRoot: USER_API });
235 this.userDeploymentCollection = XOSCollection.extend({ urlRoot: USERDEPLOYMENT_API,
236 foreignCollections: ["users","deployments"],
238 this.userDeployments = new this.userDeploymentCollection();
240 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
241 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
242 relatedCollections: {"slivers": "deployment", "networkDeployments": "deployment", "userDeployments": "deployment"},
243 model: this.deployment});
244 this.deployments = new this.deploymentCollection();
246 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
247 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
249 this.images = new this.imageCollection();
251 this.networkTemplate = XOSModel.extend({ urlRoot: NETWORKTEMPLATE_API });
252 this.networkTemplateCollection = XOSCollection.extend({ urlRoot: NETWORKTEMPLATE_API,
253 model: this.networkTemplate});
254 this.networkTemplates = new this.networkTemplateCollection();
256 this.network = XOSModel.extend({ urlRoot: NETWORK_API });
257 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
258 relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"},
259 foreignCollections: ["slices", "networkTemplates"],
260 model: this.network});
261 this.networks = new this.networkCollection();
263 this.networkSliver = XOSModel.extend({ urlRoot: NETWORKSLIVER_API });
264 this.networkSliverCollection = XOSCollection.extend({ urlRoot: NETWORKSLIVER_API,
265 model: this.networkSliver});
266 this.networkSlivers = new this.networkSliverCollection();
268 this.networkDeployment = XOSModel.extend({ urlRoot: NETWORKDEPLOYMENT_API });
269 this.networkDeploymentCollection = XOSCollection.extend({ urlRoot: NETWORKDEPLOYMENT_API,
270 model: this.networkDeployment});
271 this.networkDeployments = new this.networkDeploymentCollection();
273 this.service = XOSModel.extend({ urlRoot: SERVICE_API });
274 this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
275 model: this.service});
276 this.services = new this.serviceCollection();
279 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API, relatedCollections: {'slivers': "slice"} });
280 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
281 model: this.slicePlus});
282 this.slicesPlus = new this.slicePlusCollection();
284 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
289 function getCookie(name) {
290 var cookieValue = null;
\r
291 if (document.cookie && document.cookie != '') {
\r
292 var cookies = document.cookie.split(';');
\r
293 for (var i = 0; i < cookies.length; i++) {
\r
294 var cookie = jQuery.trim(cookies[i]);
\r
295 // Does this cookie string begin with the name we want?
\r
296 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
297 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
302 return cookieValue;
\r
306 var _sync = Backbone.sync;
\r
307 Backbone.sync = function(method, model, options){
\r
308 options.beforeSend = function(xhr){
\r
309 var token = getCookie("csrftoken");
\r
310 xhr.setRequestHeader('X-CSRFToken', token);
\r
312 return _sync(method, model, options);
\r