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.isLoaded = false;
69 this.sortVar = 'name';
\r
70 this.sortOrder = 'asc';
\r
71 this.on( "sort", this.sorted );
\r
74 relatedCollections: [],
\r
75 foreignCollections: [],
\r
77 sorted: function() {
\r
78 this.isLoaded = true;
\r
81 simpleComparator: function( model ){
\r
82 parts=this.sortVar.split(".");
\r
83 result = model.get(parts[0]);
\r
84 for (index=1; index<parts.length; ++index) {
\r
85 result=result[parts[index]];
\r
90 comparator: function (left, right) {
\r
91 var l = this.simpleComparator(left);
\r
92 var r = this.simpleComparator(right);
\r
94 if (l === void 0) return -1;
\r
95 if (r === void 0) return 1;
\r
97 if (this.sortOrder=="desc") {
\r
98 return l < r ? 1 : l > r ? -1 : 0;
\r
100 return l < r ? -1 : l > r ? 1 : 0;
\r
104 startPolling: function() {
\r
105 if (!this._polling) {
\r
107 setInterval(function() { collection.fetch(); }, 10000);
113 maybeFetch: function(options){
114 // Helper function to fetch only if this collection has not been fetched before.
116 // If this has already been fetched, call the success, if it exists
117 options.success && options.success();
118 console.log("alreadyFetched");
122 // when the original success function completes mark this collection as fetched
124 successWrapper = function(success){
126 self._fetched = true;
127 success && success.apply(this, arguments);
130 options.success = successWrapper(options.success);
131 console.log("call fetch");
135 getOrFetch: function(id, options){
136 // Helper function to use this collection as a cache for models on the server
137 var model = this.get(id);
140 options.success && options.success(model);
144 model = new this.model({
148 model.fetch(options);
151 filterBy: function(fieldName, value) {
152 filtered = this.filter(function(obj) {
153 return obj.get(fieldName) == value;
155 return new this.constructor(filtered);
158 /* from backbone-tastypie.js */
159 url: function( models ) {
160 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
161 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
163 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
164 // (set to 'resource_uri') contains the model's id.
165 if ( models && models.length ) {
166 var ids = _.map( models, function( model ) {
167 var parts = _.compact( model.id.split('/') );
168 return parts[ parts.length - 1 ];
170 url += 'set/' + ids.join(';') + '/';
173 url && ( url += "?no_hyperlinks=1" );
178 listMethods: function() {
180 for(var m in this) {
\r
181 if(typeof this[m] == "function") {
\r
191 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API, modelName: "sliver" });
192 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
193 relatedCollections: {"networkSlivers": "sliver"},
194 foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
195 model: this.sliver});
196 this.slivers = new this.sliverCollection();
198 this.slice = XOSModel.extend({ urlRoot: SLICE_API, modelName: "slice" });
199 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
200 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"},
201 foreignCollections: ["services", "sites"],
203 this.slices = new this.sliceCollection();
205 this.sliceDeployment = XOSModel.extend({ urlRoot: SLICEDEPLOYMENT_API, modelName: "sliceDeployment" });
206 this.sliceDeploymentCollection = XOSCollection.extend({ urlRoot: SLICEDEPLOYMENT_API,
207 foreignCollections: ["slices", "deployments"],
209 this.sliceDeployments = new this.sliceDeploymentCollection();
211 this.slicePrivilege = XOSModel.extend({ urlRoot: SLICEPRIVILEGE_API, modelName: "slicePrivilege" });
212 this.slicePrivilegeCollection = XOSCollection.extend({ urlRoot: SLICEPRIVILEGE_API,
213 foreignCollections: ["slices", "users", "sliceRoles"],
215 this.slicePrivileges = new this.slicePrivilegeCollection();
217 this.sliceRole = XOSModel.extend({ urlRoot: SLICEROLE_API, modelName: "sliceRole" });
218 this.sliceRoleCollection = XOSCollection.extend({ urlRoot: SLICEROLE_API,
220 this.sliceRoles = new this.sliceRoleCollection();
222 this.node = XOSModel.extend({ urlRoot: NODE_API, modelName: "node" });
223 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
224 foreignCollections: ["sites", "deployments"],
226 this.nodes = new this.nodeCollection();
228 this.site = XOSModel.extend({ urlRoot: SITE_API, modelName: "site" });
229 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
230 relatedCollections: {"users": "site", "slices": "site", "nodes": "site"},
232 this.sites = new this.siteCollection();
234 this.user = XOSModel.extend({ urlRoot: USER_API, modelName: "user" });
235 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
236 relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"},
237 foreignCollections: ["sites"],
239 this.users = new this.userCollection();
241 this.userDeployment = XOSModel.extend({ urlRoot: USERDEPLOYMENT_API, modelName: "userDeployment" });
242 this.userDeploymentCollection = XOSCollection.extend({ urlRoot: USERDEPLOYMENT_API,
243 foreignCollections: ["users","deployments"],
245 this.userDeployments = new this.userDeploymentCollection();
247 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API, modelName: "deployment" });
248 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
249 relatedCollections: {"nodes": "deployment", "slivers": "deploymentNetwork", "networkDeployments": "deployment", "userDeployments": "deployment"},
250 model: this.deployment});
251 this.deployments = new this.deploymentCollection();
253 this.image = XOSModel.extend({ urlRoot: IMAGE_API, modelName: "image" });
254 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
256 this.images = new this.imageCollection();
258 this.networkTemplate = XOSModel.extend({ urlRoot: NETWORKTEMPLATE_API, modelName: "networkTemplate" });
259 this.networkTemplateCollection = XOSCollection.extend({ urlRoot: NETWORKTEMPLATE_API,
260 model: this.networkTemplate});
261 this.networkTemplates = new this.networkTemplateCollection();
263 this.network = XOSModel.extend({ urlRoot: NETWORK_API, modelName: "network" });
264 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
265 relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"},
266 foreignCollections: ["slices", "networkTemplates"],
267 model: this.network});
268 this.networks = new this.networkCollection();
270 this.networkSliver = XOSModel.extend({ urlRoot: NETWORKSLIVER_API, modelName: "networkSliver" });
271 this.networkSliverCollection = XOSCollection.extend({ urlRoot: NETWORKSLIVER_API,
272 model: this.networkSliver});
273 this.networkSlivers = new this.networkSliverCollection();
275 this.networkDeployment = XOSModel.extend({ urlRoot: NETWORKDEPLOYMENT_API, modelName: "networkDeployment" });
276 this.networkDeploymentCollection = XOSCollection.extend({ urlRoot: NETWORKDEPLOYMENT_API,
277 model: this.networkDeployment});
278 this.networkDeployments = new this.networkDeploymentCollection();
280 this.service = XOSModel.extend({ urlRoot: SERVICE_API, modelName: "sliver" });
281 this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
282 model: this.service});
283 this.services = new this.serviceCollection();
286 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API, modelName: "slicePlus" });
287 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
288 relatedCollections: {'slivers': "slice"},
289 model: this.slicePlus});
290 this.slicesPlus = new this.slicePlusCollection();
292 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
297 function getCookie(name) {
298 var cookieValue = null;
\r
299 if (document.cookie && document.cookie != '') {
\r
300 var cookies = document.cookie.split(';');
\r
301 for (var i = 0; i < cookies.length; i++) {
\r
302 var cookie = jQuery.trim(cookies[i]);
\r
303 // Does this cookie string begin with the name we want?
\r
304 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
305 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
310 return cookieValue;
\r
314 var _sync = Backbone.sync;
\r
315 Backbone.sync = function(method, model, options){
\r
316 options.beforeSend = function(xhr){
\r
317 var token = getCookie("csrftoken");
\r
318 xhr.setRequestHeader('X-CSRFToken', token);
\r
320 return _sync(method, model, options);
\r