1 if (! window.XOSLIB_LOADED ) {
2 window.XOSLIB_LOADED=true;
4 SLIVER_API = "/plstackapi/slivers/";
5 SLICE_API = "/plstackapi/slices/";
6 SLICEROLE_API = "/plstackapi/slice_roles/";
7 NODE_API = "/plstackapi/nodes/";
8 SITE_API = "/plstackapi/sites/";
9 USER_API = "/plstackapi/users/";
10 USERDEPLOYMENT_API = "/plstackapi/user_deployments/";
11 DEPLOYMENT_API = "/plstackapi/deployments/";
12 IMAGE_API = "/plstackapi/images/";
13 NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
14 NETWORK_API = "/plstackapi/networks/";
15 NETWORKSLIVER_API = "/plstackapi/networkslivers/";
16 SERVICE_API = "/plstackapi/services/";
17 SLICEPRIVILEGE_API = "/plstackapi/slice_privileges/";
18 NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/";
20 /* changed as a side effect of the big rename
21 SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
22 USERDEPLOYMENT_API = "/plstackapi/user_deployments/";
25 SLICEDEPLOYMENT_API = "/plstackapi/slicedeployments/";
26 USERDEPLOYMENT_API = "/plstackapi/userdeployments/";
28 SLICEPLUS_API = "/xoslib/slicesplus/";
30 XOSModel = Backbone.Model.extend({
31 /* from backbone-tastypie.js */
32 //idAttribute: 'resource_uri',
34 /* from backbone-tastypie.js */
36 var url = this.attributes.resource_uri;
40 url = this.urlRoot + this.id;
42 // this happens when creating a new model.
48 // XXX I'm not sure this does anything useful
49 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
50 url = url || this.urlRoot;
53 // remove any existing query parameters
54 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
56 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
58 url && ( url += "?no_hyperlinks=1" );
63 listMethods: function() {
65 for(var m in this) {
\r
66 if(typeof this[m] == "function") {
\r
74 XOSCollection = Backbone.Collection.extend({
76 return this.models.map(function(element) { return element.attributes; });
79 initialize: function(){
80 this.isLoaded = false;
81 this.failedLoad = false;
82 this.startedLoad = false;
83 this.sortVar = 'name';
\r
84 this.sortOrder = 'asc';
\r
85 this.on( "sort", this.sorted );
\r
88 relatedCollections: [],
\r
89 foreignCollections: [],
\r
91 sorted: function() {
\r
92 //console.log("sorted " + this.modelName);
\r
95 simpleComparator: function( model ){
\r
96 parts=this.sortVar.split(".");
\r
97 result = model.get(parts[0]);
\r
98 for (index=1; index<parts.length; ++index) {
\r
99 result=result[parts[index]];
\r
104 comparator: function (left, right) {
\r
105 var l = this.simpleComparator(left);
\r
106 var r = this.simpleComparator(right);
\r
108 if (l === void 0) return -1;
\r
109 if (r === void 0) return 1;
\r
111 if (this.sortOrder=="desc") {
\r
112 return l < r ? 1 : l > r ? -1 : 0;
\r
114 return l < r ? -1 : l > r ? 1 : 0;
\r
118 fetchSuccess: function(collection, response, options) {
\r
119 //console.log("fetch succeeded " + collection.modelName);
\r
120 this.failedLoad = false;
\r
121 this.fetching = false;
\r
122 if (!this.isLoaded) {
\r
123 this.isLoaded = true;
\r
124 Backbone.trigger("xoslib:collectionLoadChange", this);
\r
126 this.trigger("fetchStateChange");
\r
127 if (options["orig_success"]) {
\r
128 options["orig_success"](collection, response, options);
\r
132 fetchFailure: function(collection, response, options) {
\r
133 //console.log("fetch failed " + collection.modelName);
\r
134 this.fetching = false;
\r
135 if ((!this.isLoaded) && (!this.failedLoad)) {
\r
136 this.failedLoad=true;
\r
137 Backbone.trigger("xoslib:collectionLoadChange", this);
\r
139 this.trigger("fetchStateChange");
\r
140 if (options["orig_failure"]) {
\r
141 options["orig_failure"](collection, response, options);
\r
145 fetch: function(options) {
\r
147 this.fetching=true;
\r
148 //console.log("fetch " + this.modelName);
\r
149 if (!this.startedLoad) {
\r
150 this.startedLoad=true;
\r
151 Backbone.trigger("xoslib:collectionLoadChange", this);
\r
153 this.trigger("fetchStateChange");
\r
154 if (options == undefined) {
\r
157 options["orig_success"] = options["success"];
\r
158 options["orig_failure"] = options["failure"];
\r
159 options["success"] = function(collection, response, options) { self.fetchSuccess.call(self, collection, response, options); };
\r
160 options["failure"] = this.fetchFailure;
\r
161 Backbone.Collection.prototype.fetch.call(this, options);
\r
164 startPolling: function() {
\r
165 if (!this._polling) {
\r
167 setInterval(function() { collection.fetch(); }, 10000);
173 refresh: function(refreshRelated) {
174 if (!this.fetching) {
177 if (refreshRelated) {
178 for (related in this.relatedCollections) {
179 related = xos[related];
180 if (!related.fetching) {
187 maybeFetch: function(options){
188 // Helper function to fetch only if this collection has not been fetched before.
190 // If this has already been fetched, call the success, if it exists
191 options.success && options.success();
192 console.log("alreadyFetched");
196 // when the original success function completes mark this collection as fetched
198 successWrapper = function(success){
200 self._fetched = true;
201 success && success.apply(this, arguments);
204 options.success = successWrapper(options.success);
205 console.log("call fetch");
209 getOrFetch: function(id, options){
210 // Helper function to use this collection as a cache for models on the server
211 var model = this.get(id);
214 options.success && options.success(model);
218 model = new this.model({
222 model.fetch(options);
225 filterBy: function(fieldName, value) {
226 filtered = this.filter(function(obj) {
227 return obj.get(fieldName) == value;
229 return new this.constructor(filtered);
232 /* from backbone-tastypie.js */
233 url: function( models ) {
234 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
235 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
237 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
238 // (set to 'resource_uri') contains the model's id.
239 if ( models && models.length ) {
240 var ids = _.map( models, function( model ) {
241 var parts = _.compact( model.id.split('/') );
242 return parts[ parts.length - 1 ];
244 url += 'set/' + ids.join(';') + '/';
247 url && ( url += "?no_hyperlinks=1" );
252 listMethods: function() {
254 for(var m in this) {
\r
255 if(typeof this[m] == "function") {
\r
263 function define_model(lib, attrs) {
264 modelName = attrs.modelName;
265 modelClassName = modelName;
266 collectionClassName = modelName + "Collection";
267 collectionName = modelName + "s";
274 if ($.inArray(key, ["urlRoot", "modelName"])>=0) {
275 modelAttrs[key] = value;
277 if ($.inArray(key, ["urlRoot", "modelName", "relatedCollections", "foreignCollections"])>=0) {
278 collectionAttrs[key] = value;
282 if (xosdefaults && xosdefaults[modelName]) {
283 modelAttrs["defaults"] = xosdefaults[modelName];
286 lib[modelName] = XOSModel.extend(modelAttrs);
288 collectionAttrs["model"] = lib[modelName];
290 lib[collectionClassName] = XOSCollection.extend(collectionAttrs);
291 lib[collectionName] = new lib[collectionClassName]();
293 lib.allCollectionNames.push(collectionName);
294 lib.allCollections.push(lib[collectionName]);
298 this.allCollectionNames = [];
299 this.allCollections = [];
301 define_model(this, {urlRoot: SLIVER_API,
302 relatedCollections: {"networkSlivers": "sliver"},
303 foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
304 modelName: "sliver"});
306 define_model(this, {urlRoot: SLICE_API,
307 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"},
308 foreignCollections: ["services", "sites"],
309 modelName: "slice"});
311 define_model(this, {urlRoot: SLICEDEPLOYMENT_API,
312 foreignCollections: ["slices", "deployments"],
313 modelName: "sliceDeployment"});
315 define_model(this, {urlRoot: SLICEPRIVILEGE_API,
316 foreignCollections: ["slices", "users", "sliceRoles"],
317 modelName: "slicePrivilege"});
319 define_model(this, {urlRoot: SLICEROLE_API,
320 modelName: "sliceRole"});
322 define_model(this, {urlRoot: NODE_API,
323 foreignCollections: ["sites", "deployments"],
326 define_model(this, {urlRoot: SITE_API,
327 relatedCollections: {"users": "site", "slices": "site", "nodes": "site"},
330 define_model(this, {urlRoot: USER_API,
331 relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"},
332 foreignCollections: ["sites"],
335 define_model(this, {urlRoot: USERDEPLOYMENT_API,
336 foreignCollections: ["users","deployments"],
337 modelName: "userDeployment"});
339 define_model(this, { urlRoot: DEPLOYMENT_API,
340 relatedCollections: {"nodes": "deployment", "slivers": "deploymentNetwork", "networkDeployments": "deployment", "userDeployments": "deployment"},
341 modelName: "deployment"});
343 define_model(this, {urlRoot: IMAGE_API,
345 modelName: "image"});
347 define_model(this, {urlRoot: NETWORKTEMPLATE_API,
348 modelName: "networkTemplate"});
350 define_model(this, {urlRoot: NETWORK_API,
351 relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"},
352 foreignCollections: ["slices", "networkTemplates"],
353 modelName: "network"});
355 define_model(this, {urlRoot: NETWORKSLIVER_API,
356 modelName: "networkSliver"});
358 define_model(this, {urlRoot: NETWORKDEPLOYMENT_API,
359 modelName: "networkDeployment"});
361 define_model(this, {urlRoot: SERVICE_API,
362 modelName: "service"});
365 define_model(this, {urlRoot: SLICEPLUS_API,
366 relatedCollections: {'slivers': "slice"},
367 modelName: "slicePlus"});
369 this.listObjects = function() { return this.allCollectionNames; };
371 this.getCollectionStatus = function() {
372 stats = {isLoaded: 0, failedLoad: 0, startedLoad: 0};
373 for (index in this.allCollections) {
374 collection = this.allCollections[index];
375 if (collection.isLoaded) {
376 stats["isLoaded"] = stats["isLoaded"] + 1;
378 if (collection.failedLoad) {
379 stats["failedLoad"] = stats["failedLoad"] + 1;
381 if (collection.startedLoad) {
382 stats["startedLoad"] = stats["startedLoad"] + 1;
385 stats["completedLoad"] = stats["failedLoad"] + stats["isLoaded"];
392 function getCookie(name) {
393 var cookieValue = null;
\r
394 if (document.cookie && document.cookie != '') {
\r
395 var cookies = document.cookie.split(';');
\r
396 for (var i = 0; i < cookies.length; i++) {
\r
397 var cookie = jQuery.trim(cookies[i]);
\r
398 // Does this cookie string begin with the name we want?
\r
399 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
400 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
405 return cookieValue;
\r
409 var _sync = Backbone.sync;
\r
410 Backbone.sync = function(method, model, options){
\r
411 options.beforeSend = function(xhr){
\r
412 var token = getCookie("csrftoken");
\r
413 xhr.setRequestHeader('X-CSRFToken', token);
\r
415 return _sync(method, model, options);
\r