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 DEPLOYMENT_API = "/plstackapi/deployments/";
13 IMAGE_API = "/plstackapi/images/";
14 NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
15 NETWORK_API = "/plstackapi/networks/";
16 NETWORKSLIVER_API = "/plstackapi/networkslivers/";
17 SERVICE_API = "/plstackapi/services/";
19 SLICEPLUS_API = "/xoslib/slicesplus/";
21 XOSModel = Backbone.Model.extend({
22 /* from backbone-tastypie.js */
23 //idAttribute: 'resource_uri',
25 /* from backbone-tastypie.js */
27 var url = this.attributes.resource_uri;
30 url = this.urlRoot + this.id;
34 // XXX I'm not sure this does anything useful
35 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
36 url = url || this.urlRoot;
39 // remove any existing query parameters
40 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
42 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
44 url && ( url += "?no_hyperlinks=1" );
49 listMethods: function() {
51 for(var m in this) {
\r
52 if(typeof this[m] == "function") {
\r
60 XOSCollection = Backbone.Collection.extend({
62 return this.models.map(function(element) { return element.attributes; });
65 initialize: function(){
66 this.sortVar = 'name';
\r
67 this.sortOrder = 'asc';
\r
70 relatedCollections: [],
\r
71 foreignCollections: [],
\r
73 simpleComparator: function( model ){
\r
74 parts=this.sortVar.split(".");
\r
75 result = model.get(parts[0]);
\r
76 for (index=1; index<parts.length; ++index) {
\r
77 result=result[parts[index]];
\r
82 comparator: function (left, right) {
\r
83 var l = this.simpleComparator(left);
\r
84 var r = this.simpleComparator(right);
\r
86 if (l === void 0) return -1;
\r
87 if (r === void 0) return 1;
\r
89 if (this.sortOrder=="desc") {
\r
90 return l < r ? 1 : l > r ? -1 : 0;
\r
92 return l < r ? -1 : l > r ? 1 : 0;
\r
96 startPolling: function() {
\r
97 if (!this._polling) {
\r
99 setInterval(function() { collection.fetch(); }, 10000);
105 maybeFetch: function(options){
106 // Helper function to fetch only if this collection has not been fetched before.
108 // If this has already been fetched, call the success, if it exists
109 options.success && options.success();
110 console.log("alreadyFetched");
114 // when the original success function completes mark this collection as fetched
116 successWrapper = function(success){
118 self._fetched = true;
119 success && success.apply(this, arguments);
122 options.success = successWrapper(options.success);
123 console.log("call fetch");
127 getOrFetch: function(id, options){
128 // Helper function to use this collection as a cache for models on the server
129 var model = this.get(id);
132 options.success && options.success(model);
136 model = new this.model({
140 model.fetch(options);
143 filterBy: function(fieldName, value) {
144 filtered = this.filter(function(obj) {
145 return obj.get(fieldName) == value;
147 return new this.constructor(filtered);
150 /* from backbone-tastypie.js */
151 url: function( models ) {
152 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
153 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
155 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
156 // (set to 'resource_uri') contains the model's id.
157 if ( models && models.length ) {
158 var ids = _.map( models, function( model ) {
159 var parts = _.compact( model.id.split('/') );
160 return parts[ parts.length - 1 ];
162 url += 'set/' + ids.join(';') + '/';
165 url && ( url += "?no_hyperlinks=1" );
170 listMethods: function() {
172 for(var m in this) {
\r
173 if(typeof this[m] == "function") {
\r
183 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
184 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
185 relatedCollections: {"networkSlivers": "sliver"},
186 foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
187 model: this.sliver});
188 this.slivers = new this.sliverCollection();
190 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
191 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
192 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice"},
193 foreignCollections: ["services", "sites"],
195 this.slices = new this.sliceCollection();
197 this.sliceDeployment = XOSModel.extend({ urlRoot: SLICEDEPLOYMENT_API });
198 this.sliceDeploymentCollection = XOSCollection.extend({ urlRoot: SLICEDEPLOYMENT_API,
199 foreignCollections: ["slices", "deployments"],
201 this.sliceDeployments = new this.sliceDeploymentCollection();
203 this.slicePrivilege = XOSModel.extend({ urlRoot: SLICEPRIVILEGE_API });
204 this.slicePrivilegeCollection = XOSCollection.extend({ urlRoot: SLICEPRIVILEGE_API,
205 foreignCollections: ["slices", "users", "sliceRoles"],
207 this.slicePrivileges = new this.slicePrivilegeCollection();
209 this.sliceRole = XOSModel.extend({ urlRoot: SLICEROLE_API });
210 this.sliceRoleCollection = XOSCollection.extend({ urlRoot: SLICEROLE_API,
212 this.sliceRoles = new this.sliceRoleCollection();
214 this.node = XOSModel.extend({ urlRoot: NODE_API });
215 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
216 foreignCollections: ["sites", "deployments"],
218 this.nodes = new this.nodeCollection();
220 this.site = XOSModel.extend({ urlRoot: SITE_API });
221 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
223 this.sites = new this.siteCollection();
225 this.user = XOSModel.extend({ urlRoot: USER_API });
226 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
228 this.users = new this.userCollection();
230 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
231 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
232 model: this.deployment});
233 this.deployments = new this.deploymentCollection();
235 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
236 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
238 this.images = new this.imageCollection();
240 this.networkTemplate = XOSModel.extend({ urlRoot: NETWORKTEMPLATE_API });
241 this.networkTemplateCollection = XOSCollection.extend({ urlRoot: NETWORKTEMPLATE_API,
242 model: this.networkTemplate});
243 this.networkTemplates = new this.networkTemplateCollection();
245 this.network = XOSModel.extend({ urlRoot: NETWORK_API });
246 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
247 foreignCollections: ["slivers", "networkTemplates"],
248 model: this.network});
249 this.networks = new this.networkCollection();
251 this.networkSliver = XOSModel.extend({ urlRoot: NETWORKSLIVER_API });
252 this.networkSliverCollection = XOSCollection.extend({ urlRoot: NETWORKSLIVER_API,
253 model: this.networkSliver});
254 this.networkSlivers = new this.networkSliverCollection();
256 this.service = XOSModel.extend({ urlRoot: SERVICE_API });
257 this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
258 model: this.service});
259 this.services = new this.serviceCollection();
262 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API, relatedCollections: {'slivers': "slice"} });
263 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
264 model: this.slicePlus});
265 this.slicesPlus = new this.slicePlusCollection();
267 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
272 function getCookie(name) {
273 var cookieValue = null;
\r
274 if (document.cookie && document.cookie != '') {
\r
275 var cookies = document.cookie.split(';');
\r
276 for (var i = 0; i < cookies.length; i++) {
\r
277 var cookie = jQuery.trim(cookies[i]);
\r
278 // Does this cookie string begin with the name we want?
\r
279 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
280 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
285 return cookieValue;
\r
289 var _sync = Backbone.sync;
\r
290 Backbone.sync = function(method, model, options){
\r
291 options.beforeSend = function(xhr){
\r
292 var token = getCookie("csrftoken");
\r
293 xhr.setRequestHeader('X-CSRFToken', token);
\r
295 return _sync(method, model, options);
\r