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 this.failedLoad = false;
\r
120 if (!this.isLoaded) {
\r
121 this.isLoaded = true;
\r
122 Backbone.trigger("xoslib:collectionLoadChange", this);
\r
124 if (options["orig_success"]) {
\r
125 options["orig_success"](collection, response, options);
\r
129 fetchFailure: function(collection, response, options) {
\r
130 if ((!this.isLoaded) && (!this.failedLoad)) {
\r
131 this.failedLoad=true;
\r
132 Backbone.trigger("xoslib:collectionLoadChange", this);
\r
134 if (options["orig_failure"]) {
\r
135 options["orig_failure"](collection, response, options);
\r
139 fetch: function(options) {
\r
141 if (!this.startedLoad) {
\r
142 this.startedLoad=true;
\r
143 Backbone.trigger("xoslib:collectionLoadChange", this);
\r
145 if (options == undefined) {
\r
148 options["orig_success"] = options["success"];
\r
149 options["orig_failure"] = options["failure"];
\r
150 options["success"] = function(collection, response, options) { self.fetchSuccess.call(self, collection, response, options); };
\r
151 options["failure"] = this.fetchFailure;
\r
152 Backbone.Collection.prototype.fetch.call(this, options);
\r
155 startPolling: function() {
\r
156 if (!this._polling) {
\r
158 setInterval(function() { collection.fetch(); }, 10000);
164 maybeFetch: function(options){
165 // Helper function to fetch only if this collection has not been fetched before.
167 // If this has already been fetched, call the success, if it exists
168 options.success && options.success();
169 console.log("alreadyFetched");
173 // when the original success function completes mark this collection as fetched
175 successWrapper = function(success){
177 self._fetched = true;
178 success && success.apply(this, arguments);
181 options.success = successWrapper(options.success);
182 console.log("call fetch");
186 getOrFetch: function(id, options){
187 // Helper function to use this collection as a cache for models on the server
188 var model = this.get(id);
191 options.success && options.success(model);
195 model = new this.model({
199 model.fetch(options);
202 filterBy: function(fieldName, value) {
203 filtered = this.filter(function(obj) {
204 return obj.get(fieldName) == value;
206 return new this.constructor(filtered);
209 /* from backbone-tastypie.js */
210 url: function( models ) {
211 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
212 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
214 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
215 // (set to 'resource_uri') contains the model's id.
216 if ( models && models.length ) {
217 var ids = _.map( models, function( model ) {
218 var parts = _.compact( model.id.split('/') );
219 return parts[ parts.length - 1 ];
221 url += 'set/' + ids.join(';') + '/';
224 url && ( url += "?no_hyperlinks=1" );
229 listMethods: function() {
231 for(var m in this) {
\r
232 if(typeof this[m] == "function") {
\r
240 function define_model(lib, attrs) {
241 modelName = attrs.modelName;
242 modelClassName = modelName;
243 collectionClassName = modelName + "Collection";
244 collectionName = modelName + "s";
251 if ($.inArray(key, ["urlRoot", "modelName"])>=0) {
252 modelAttrs[key] = value;
254 if ($.inArray(key, ["urlRoot", "modelName", "relatedCollections", "foreignCollections"])>=0) {
255 collectionAttrs[key] = value;
259 if (xosdefaults && xosdefaults[modelName]) {
260 modelAttrs["defaults"] = xosdefaults[modelName];
263 lib[modelName] = XOSModel.extend(modelAttrs);
265 collectionAttrs["model"] = lib[modelName];
267 lib[collectionClassName] = XOSCollection.extend(collectionAttrs);
268 lib[collectionName] = new lib[collectionClassName]();
270 lib.allCollectionNames.push(collectionName);
271 lib.allCollections.push(lib[collectionName]);
275 this.allCollectionNames = [];
276 this.allCollections = [];
278 define_model(this, {urlRoot: SLIVER_API,
279 relatedCollections: {"networkSlivers": "sliver"},
280 foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
281 modelName: "sliver"});
283 define_model(this, {urlRoot: SLICE_API,
284 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"},
285 foreignCollections: ["services", "sites"],
286 modelName: "slice"});
288 define_model(this, {urlRoot: SLICEDEPLOYMENT_API,
289 foreignCollections: ["slices", "deployments"],
290 modelName: "sliceDeployment"});
292 define_model(this, {urlRoot: SLICEPRIVILEGE_API,
293 foreignCollections: ["slices", "users", "sliceRoles"],
294 modelName: "slicePrivilege"});
296 define_model(this, {urlRoot: SLICEROLE_API,
297 modelName: "sliceRole"});
299 define_model(this, {urlRoot: NODE_API,
300 foreignCollections: ["sites", "deployments"],
303 define_model(this, {urlRoot: SITE_API,
304 relatedCollections: {"users": "site", "slices": "site", "nodes": "site"},
307 define_model(this, {urlRoot: USER_API,
308 relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"},
309 foreignCollections: ["sites"],
312 define_model(this, {urlRoot: USERDEPLOYMENT_API,
313 foreignCollections: ["users","deployments"],
314 modelName: "userDeployment"});
316 define_model(this, { urlRoot: DEPLOYMENT_API,
317 relatedCollections: {"nodes": "deployment", "slivers": "deploymentNetwork", "networkDeployments": "deployment", "userDeployments": "deployment"},
318 modelName: "deployment"});
320 define_model(this, {urlRoot: IMAGE_API,
322 modelName: "image"});
324 define_model(this, {urlRoot: NETWORKTEMPLATE_API,
325 modelName: "networkTemplate"});
327 define_model(this, {urlRoot: NETWORK_API,
328 relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"},
329 foreignCollections: ["slices", "networkTemplates"],
330 modelName: "network"});
332 define_model(this, {urlRoot: NETWORKSLIVER_API,
333 modelName: "networkSliver"});
335 define_model(this, {urlRoot: NETWORKDEPLOYMENT_API,
336 modelName: "networkDeployment"});
338 define_model(this, {urlRoot: SERVICE_API,
339 modelName: "service"});
342 define_model(this, {urlRoot: SLICEPLUS_API,
343 relatedCollections: {'slivers': "slice"},
344 modelName: "slicePlus"});
346 this.listObjects = function() { return this.allCollectionNames; };
348 this.getCollectionStatus = function() {
349 stats = {isLoaded: 0, failedLoad: 0, startedLoad: 0};
350 for (index in this.allCollections) {
351 collection = this.allCollections[index];
352 if (collection.isLoaded) {
353 stats["isLoaded"] = stats["isLoaded"] + 1;
355 if (collection.failedLoad) {
356 stats["failedLoad"] = stats["failedLoad"] + 1;
358 if (collection.startedLoad) {
359 stats["startedLoad"] = stats["startedLoad"] + 1;
362 stats["completedLoad"] = stats["failedLoad"] + stats["isLoaded"];
369 function getCookie(name) {
370 var cookieValue = null;
\r
371 if (document.cookie && document.cookie != '') {
\r
372 var cookies = document.cookie.split(';');
\r
373 for (var i = 0; i < cookies.length; i++) {
\r
374 var cookie = jQuery.trim(cookies[i]);
\r
375 // Does this cookie string begin with the name we want?
\r
376 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
377 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
382 return cookieValue;
\r
386 var _sync = Backbone.sync;
\r
387 Backbone.sync = function(method, model, options){
\r
388 options.beforeSend = function(xhr){
\r
389 var token = getCookie("csrftoken");
\r
390 xhr.setRequestHeader('X-CSRFToken', token);
\r
392 return _sync(method, model, options);
\r