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;
33 url = this.urlRoot + this.id;
35 // this happens when creating a new model.
41 // XXX I'm not sure this does anything useful
42 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
43 url = url || this.urlRoot;
46 // remove any existing query parameters
47 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
49 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
51 url && ( url += "?no_hyperlinks=1" );
56 listMethods: function() {
58 for(var m in this) {
\r
59 if(typeof this[m] == "function") {
\r
67 XOSCollection = Backbone.Collection.extend({
69 return this.models.map(function(element) { return element.attributes; });
72 initialize: function(){
73 this.isLoaded = false;
74 this.sortVar = 'name';
\r
75 this.sortOrder = 'asc';
\r
76 this.on( "sort", this.sorted );
\r
79 relatedCollections: [],
\r
80 foreignCollections: [],
\r
82 sorted: function() {
\r
83 this.isLoaded = true;
\r
86 simpleComparator: function( model ){
\r
87 parts=this.sortVar.split(".");
\r
88 result = model.get(parts[0]);
\r
89 for (index=1; index<parts.length; ++index) {
\r
90 result=result[parts[index]];
\r
95 comparator: function (left, right) {
\r
96 var l = this.simpleComparator(left);
\r
97 var r = this.simpleComparator(right);
\r
99 if (l === void 0) return -1;
\r
100 if (r === void 0) return 1;
\r
102 if (this.sortOrder=="desc") {
\r
103 return l < r ? 1 : l > r ? -1 : 0;
\r
105 return l < r ? -1 : l > r ? 1 : 0;
\r
109 startPolling: function() {
\r
110 if (!this._polling) {
\r
112 setInterval(function() { collection.fetch(); }, 10000);
118 maybeFetch: function(options){
119 // Helper function to fetch only if this collection has not been fetched before.
121 // If this has already been fetched, call the success, if it exists
122 options.success && options.success();
123 console.log("alreadyFetched");
127 // when the original success function completes mark this collection as fetched
129 successWrapper = function(success){
131 self._fetched = true;
132 success && success.apply(this, arguments);
135 options.success = successWrapper(options.success);
136 console.log("call fetch");
140 getOrFetch: function(id, options){
141 // Helper function to use this collection as a cache for models on the server
142 var model = this.get(id);
145 options.success && options.success(model);
149 model = new this.model({
153 model.fetch(options);
156 filterBy: function(fieldName, value) {
157 filtered = this.filter(function(obj) {
158 return obj.get(fieldName) == value;
160 return new this.constructor(filtered);
163 /* from backbone-tastypie.js */
164 url: function( models ) {
165 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
166 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
168 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
169 // (set to 'resource_uri') contains the model's id.
170 if ( models && models.length ) {
171 var ids = _.map( models, function( model ) {
172 var parts = _.compact( model.id.split('/') );
173 return parts[ parts.length - 1 ];
175 url += 'set/' + ids.join(';') + '/';
178 url && ( url += "?no_hyperlinks=1" );
183 listMethods: function() {
185 for(var m in this) {
\r
186 if(typeof this[m] == "function") {
\r
194 function define_model(lib, attrs) {
195 modelName = attrs.modelName;
196 modelClassName = modelName;
197 collectionClassName = modelName + "Collection";
198 collectionName = modelName + "s";
205 if ($.inArray(key, ["urlRoot", "modelName"])>=0) {
206 modelAttrs[key] = value;
208 if ($.inArray(key, ["urlRoot", "modelName", "relatedCollections", "foreignCollections"])>=0) {
209 collectionAttrs[key] = value;
213 if (xosdefaults && xosdefaults[modelName]) {
214 modelAttrs["defaults"] = xosdefaults[modelName];
217 lib[modelName] = XOSModel.extend(modelAttrs);
219 collectionAttrs["model"] = lib[modelName];
221 lib[collectionClassName] = XOSCollection.extend(collectionAttrs);
222 lib[collectionName] = new lib[collectionClassName]();
224 lib.allCollectionNames.push(collectionName);
228 this.allCollectionNames = [];
230 define_model(this, {urlRoot: SLIVER_API,
231 relatedCollections: {"networkSlivers": "sliver"},
232 foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
233 modelName: "sliver"});
235 define_model(this, {urlRoot: SLICE_API,
236 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"},
237 foreignCollections: ["services", "sites"],
238 modelName: "slice"});
240 define_model(this, {urlRoot: SLICEDEPLOYMENT_API,
241 foreignCollections: ["slices", "deployments"],
242 modelName: "sliceDeployment"});
244 define_model(this, {urlRoot: SLICEPRIVILEGE_API,
245 foreignCollections: ["slices", "users", "sliceRoles"],
246 modelName: "slicePrivilege"});
248 define_model(this, {urlRoot: SLICEROLE_API,
249 modelName: "sliceRole"});
251 define_model(this, {urlRoot: NODE_API,
252 foreignCollections: ["sites", "deployments"],
255 define_model(this, {urlRoot: SITE_API,
256 relatedCollections: {"users": "site", "slices": "site", "nodes": "site"},
259 define_model(this, {urlRoot: USER_API,
260 relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"},
261 foreignCollections: ["sites"],
264 define_model(this, {urlRoot: USERDEPLOYMENT_API,
265 foreignCollections: ["users","deployments"],
266 modelName: "userDeployment"});
268 define_model(this, { urlRoot: DEPLOYMENT_API,
269 relatedCollections: {"nodes": "deployment", "slivers": "deploymentNetwork", "networkDeployments": "deployment", "userDeployments": "deployment"},
270 modelName: "deployment"});
272 define_model(this, {urlRoot: IMAGE_API,
274 modelName: "image"});
276 define_model(this, {urlRoot: NETWORKTEMPLATE_API,
277 modelName: "networkTemplate"});
279 define_model(this, {urlRoot: NETWORK_API,
280 relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"},
281 foreignCollections: ["slices", "networkTemplates"],
282 modelName: "network"});
284 define_model(this, {urlRoot: NETWORKSLIVER_API,
285 modelName: "networkSliver"});
287 define_model(this, {urlRoot: NETWORKDEPLOYMENT_API,
288 modelName: "networkDeployment"});
290 define_model(this, {urlRoot: SERVICE_API,
291 modelName: "service"});
294 define_model(this, {urlRoot: SLICEPLUS_API,
295 relatedCollections: {'slivers': "slice"},
296 modelName: "slicePlus"});
298 this.listObjects = function() { return this.allCollectionNames; };
303 function getCookie(name) {
304 var cookieValue = null;
\r
305 if (document.cookie && document.cookie != '') {
\r
306 var cookies = document.cookie.split(';');
\r
307 for (var i = 0; i < cookies.length; i++) {
\r
308 var cookie = jQuery.trim(cookies[i]);
\r
309 // Does this cookie string begin with the name we want?
\r
310 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
311 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
316 return cookieValue;
\r
320 var _sync = Backbone.sync;
\r
321 Backbone.sync = function(method, model, options){
\r
322 options.beforeSend = function(xhr){
\r
323 var token = getCookie("csrftoken");
\r
324 xhr.setRequestHeader('X-CSRFToken', token);
\r
326 return _sync(method, model, options);
\r