X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetstack%2Fcore%2Fxoslib%2Fstatic%2Fjs%2Fxoslib%2Fxos-backbone.js;h=bbf13a498aa91a1650a80e826cd4eaab1bdaf233;hb=912f63a970b2db8affe4ae87b6ff0b19fe5caa24;hp=f5431e7dc680a430679ccc37c926482abbf54697;hpb=8495f572478a7063d1481c1e112f8c551a7e288b;p=plstackapi.git diff --git a/planetstack/core/xoslib/static/js/xoslib/xos-backbone.js b/planetstack/core/xoslib/static/js/xoslib/xos-backbone.js index f5431e7..bbf13a4 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xos-backbone.js +++ b/planetstack/core/xoslib/static/js/xoslib/xos-backbone.js @@ -3,13 +3,27 @@ if (! window.XOSLIB_LOADED ) { SLIVER_API = "/plstackapi/slivers/"; SLICE_API = "/plstackapi/slices/"; + SLICEROLE_API = "/plstackapi/slice_roles/"; NODE_API = "/plstackapi/nodes/"; SITE_API = "/plstackapi/sites/"; USER_API = "/plstackapi/users/"; - DEPLOYMENT_API = "/plstackapi/deployments"; - IMAGE_API = "/plstackapi/images"; - NETWORK_API = "/plstackapi/networks"; - SERVICE_API = "/plstackapi/services"; + USERDEPLOYMENT_API = "/plstackapi/user_deployments/"; + DEPLOYMENT_API = "/plstackapi/deployments/"; + IMAGE_API = "/plstackapi/images/"; + NETWORKTEMPLATE_API = "/plstackapi/networktemplates/"; + NETWORK_API = "/plstackapi/networks/"; + NETWORKSLIVER_API = "/plstackapi/networkslivers/"; + SERVICE_API = "/plstackapi/services/"; + SLICEPRIVILEGE_API = "/plstackapi/slice_privileges/"; + NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/"; + + /* changed as a side effect of the big rename + SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/"; + USERDEPLOYMENT_API = "/plstackapi/user_deployments/"; + */ + + SLICEDEPLOYMENT_API = "/plstackapi/slicedeployments/"; + USERDEPLOYMENT_API = "/plstackapi/userdeployments/"; SLICEPLUS_API = "/xoslib/slicesplus/"; @@ -22,7 +36,12 @@ if (! window.XOSLIB_LOADED ) { var url = this.attributes.resource_uri; if (!url) { - url = this.urlRoot + this.id; + if (this.id) { + url = this.urlRoot + this.id; + } else { + // this happens when creating a new model. + url = this.urlRoot; + } } if (!url) { @@ -58,8 +77,19 @@ if (! window.XOSLIB_LOADED ) { }, initialize: function(){ + this.isLoaded = false; + this.failedLoad = false; + this.startedLoad = false; this.sortVar = 'name'; this.sortOrder = 'asc'; + this.on( "sort", this.sorted ); + }, + + relatedCollections: [], + foreignCollections: [], + + sorted: function() { + //console.log("sorted " + this.modelName); }, simpleComparator: function( model ){ @@ -85,15 +115,75 @@ if (! window.XOSLIB_LOADED ) { } }, + fetchSuccess: function(collection, response, options) { + //console.log("fetch succeeded " + collection.modelName); + this.failedLoad = false; + this.fetching = false; + if (!this.isLoaded) { + this.isLoaded = true; + Backbone.trigger("xoslib:collectionLoadChange", this); + } + this.trigger("fetchStateChange"); + if (options["orig_success"]) { + options["orig_success"](collection, response, options); + } + }, + + fetchFailure: function(collection, response, options) { + //console.log("fetch failed " + collection.modelName); + this.fetching = false; + if ((!this.isLoaded) && (!this.failedLoad)) { + this.failedLoad=true; + Backbone.trigger("xoslib:collectionLoadChange", this); + } + this.trigger("fetchStateChange"); + if (options["orig_failure"]) { + options["orig_failure"](collection, response, options); + } + }, + + fetch: function(options) { + var self=this; + this.fetching=true; + //console.log("fetch " + this.modelName); + if (!this.startedLoad) { + this.startedLoad=true; + Backbone.trigger("xoslib:collectionLoadChange", this); + } + this.trigger("fetchStateChange"); + if (options == undefined) { + options = {}; + } + options["orig_success"] = options["success"]; + options["orig_failure"] = options["failure"]; + options["success"] = function(collection, response, options) { self.fetchSuccess.call(self, collection, response, options); }; + options["failure"] = this.fetchFailure; + Backbone.Collection.prototype.fetch.call(this, options); + }, + startPolling: function() { - if (!this._polling) { - collection=this; + if (!this._polling) { + var collection=this; setInterval(function() { collection.fetch(); }, 10000); this._polling=true; this.fetch(); } }, + refresh: function(refreshRelated) { + if (!this.fetching) { + this.fetch(); + } + if (refreshRelated) { + for (related in this.relatedCollections) { + related = xos[related]; + if (!related.fetching) { + related.fetch(); + } + } + } + }, + maybeFetch: function(options){ // Helper function to fetch only if this collection has not been fetched before. if(this._fetched){ @@ -132,6 +222,13 @@ if (! window.XOSLIB_LOADED ) { model.fetch(options); }, + filterBy: function(fieldName, value) { + filtered = this.filter(function(obj) { + return obj.get(fieldName) == value; + }); + return new this.constructor(filtered); + }, + /* from backbone-tastypie.js */ url: function( models ) { var url = this.urlRoot || ( models && models.length && models[0].urlRoot ); @@ -152,7 +249,7 @@ if (! window.XOSLIB_LOADED ) { return url; }, - listMethods: function() { + listMethods: function() { var res = []; for(var m in this) { if(typeof this[m] == "function") { @@ -160,64 +257,167 @@ if (! window.XOSLIB_LOADED ) { } } return res; - } + }, }); + function define_model(lib, attrs) { + modelName = attrs.modelName; + modelClassName = modelName; + collectionClassName = modelName + "Collection"; + + if (!attrs.collectionName) { + attrs.collectionName = modelName + "s"; + } + collectionName = attrs.collectionName; + + modelAttrs = {} + collectionAttrs = {} + + for (key in attrs) { + value = attrs[key]; + if ($.inArray(key, ["urlRoot", "modelName"])>=0) { + modelAttrs[key] = value; + } + if ($.inArray(key, ["urlRoot", "modelName", "relatedCollections", "foreignCollections"])>=0) { + collectionAttrs[key] = value; + } + } + + if (xosdefaults && xosdefaults[modelName]) { + modelAttrs["defaults"] = xosdefaults[modelName]; + } + + lib[modelName] = XOSModel.extend(modelAttrs); + + collectionAttrs["model"] = lib[modelName]; + + lib[collectionClassName] = XOSCollection.extend(collectionAttrs); + lib[collectionName] = new lib[collectionClassName](); + + lib.allCollectionNames.push(collectionName); + lib.allCollections.push(lib[collectionName]); + }; + function xoslib() { - // basic REST - this.sliver = XOSModel.extend({ urlRoot: SLIVER_API }); - this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API, - model: this.sliver}); - this.slivers = new this.sliverCollection(); - - this.slice = XOSModel.extend({ urlRoot: SLICE_API }); - this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API, - model: this.slice}); - this.slices = new this.sliceCollection(); - - this.node = XOSModel.extend({ urlRoot: NODE_API }); - this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API, - model: this.node}); - this.nodes = new this.nodeCollection(); - - this.site = XOSModel.extend({ urlRoot: SITE_API }); - this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API, - model: this.site}); - this.sites = new this.siteCollection(); - - this.user = XOSModel.extend({ urlRoot: USER_API }); - this.userCollection = XOSCollection.extend({ urlRoot: USER_API, - model: this.user}); - this.users = new this.userCollection(); - - this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API }); - this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API, - model: this.deployment}); - this.deployments = new this.deploymentCollection(); - - this.image = XOSModel.extend({ urlRoot: IMAGE_API }); - this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API, - model: this.image}); - this.images = new this.imageCollection(); - - this.network = XOSModel.extend({ urlRoot: NETWORK_API }); - this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API, - model: this.network}); - this.networks = new this.networkCollection(); - - this.service = XOSModel.extend({ urlRoot: SERVICE_API }); - this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API, - model: this.service}); - this.services = new this.serviceCollection(); + this.allCollectionNames = []; + this.allCollections = []; - // enhanced REST - this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API }); - this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API, - model: this.slicePlus}); - this.slicesPlus = new this.slicePlusCollection(); + define_model(this, {urlRoot: SLIVER_API, + relatedCollections: {"networkSlivers": "sliver"}, + foreignCollections: ["slices", "deployments", "images", "nodes", "users"], + modelName: "sliver"}); + + define_model(this, {urlRoot: SLICE_API, + relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"}, + foreignCollections: ["services", "sites"], + modelName: "slice"}); + + define_model(this, {urlRoot: SLICEDEPLOYMENT_API, + foreignCollections: ["slices", "deployments"], + modelName: "sliceDeployment"}); + + define_model(this, {urlRoot: SLICEPRIVILEGE_API, + foreignCollections: ["slices", "users", "sliceRoles"], + modelName: "slicePrivilege"}); + + define_model(this, {urlRoot: SLICEROLE_API, + modelName: "sliceRole"}); + + define_model(this, {urlRoot: NODE_API, + foreignCollections: ["sites", "deployments"], + modelName: "node"}); + + define_model(this, {urlRoot: SITE_API, + relatedCollections: {"users": "site", "slices": "site", "nodes": "site"}, + modelName: "site"}); - this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; }; + define_model(this, {urlRoot: USER_API, + relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"}, + foreignCollections: ["sites"], + modelName: "user"}); + + define_model(this, {urlRoot: USERDEPLOYMENT_API, + foreignCollections: ["users","deployments"], + modelName: "userDeployment"}); + + define_model(this, { urlRoot: DEPLOYMENT_API, + relatedCollections: {"nodes": "deployment", "slivers": "deploymentNetwork", "networkDeployments": "deployment", "userDeployments": "deployment"}, + modelName: "deployment"}); + + define_model(this, {urlRoot: IMAGE_API, + model: this.image, + modelName: "image"}); + + define_model(this, {urlRoot: NETWORKTEMPLATE_API, + modelName: "networkTemplate"}); + + define_model(this, {urlRoot: NETWORK_API, + relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"}, + foreignCollections: ["slices", "networkTemplates"], + modelName: "network"}); + + define_model(this, {urlRoot: NETWORKSLIVER_API, + modelName: "networkSliver"}); + + define_model(this, {urlRoot: NETWORKDEPLOYMENT_API, + modelName: "networkDeployment"}); + + define_model(this, {urlRoot: SERVICE_API, + modelName: "service"}); + + // enhanced REST + define_model(this, {urlRoot: SLICEPLUS_API, + relatedCollections: {'slivers': "slice"}, + modelName: "slicePlus", + collectionName: "slicesPlus"}); + + this.listObjects = function() { return this.allCollectionNames; }; + + this.getCollectionStatus = function() { + stats = {isLoaded: 0, failedLoad: 0, startedLoad: 0}; + for (index in this.allCollections) { + collection = this.allCollections[index]; + if (collection.isLoaded) { + stats["isLoaded"] = stats["isLoaded"] + 1; + } + if (collection.failedLoad) { + stats["failedLoad"] = stats["failedLoad"] + 1; + } + if (collection.startedLoad) { + stats["startedLoad"] = stats["startedLoad"] + 1; + } + } + stats["completedLoad"] = stats["failedLoad"] + stats["isLoaded"]; + return stats; + }; }; xos = new xoslib(); + + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + + (function() { + var _sync = Backbone.sync; + Backbone.sync = function(method, model, options){ + options.beforeSend = function(xhr){ + var token = getCookie("csrftoken"); + xhr.setRequestHeader('X-CSRFToken', token); + }; + return _sync(method, model, options); + }; + })(); }