remove debugging code
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
1 SLIVER_API = "/plstackapi/slivers/";
2 SLICE_API = "/plstackapi/slices/";
3 NODE_API = "/plstackapi/nodes/";
4 SITE_API = "/plstackapi/sites/";
5 USER_API = "/plstackapi/users/";
6 DEPLOYMENT_API = "/plstackapi/deployments";
7
8 SLICEPLUS_API = "/xoslib/slicesplus/";
9
10 XOSModel = Backbone.Model.extend({
11     /* from backbone-tastypie.js */
12     //idAttribute: 'resource_uri',
13
14     /* from backbone-tastypie.js */
15     url: function() {
16                 var url = this.attributes.resource_uri;
17
18                 if (!url) {
19                     url = this.urlRoot + this.id;
20                 }
21
22                 if (!url) {
23                     // XXX I'm not sure this does anything useful
24                     url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
25                     url = url || this.urlRoot;
26                 }
27
28                 // remove any existing query parameters
29                 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
30
31                 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
32
33                 url && ( url += "?no_hyperlinks=1" );
34
35                 return url;
36         },
37
38         listMethods: function() {
39             var res = [];\r
40             for(var m in this) {\r
41                 if(typeof this[m] == "function") {\r
42                     res.push(m)\r
43                 }\r
44             }\r
45             return res;\r
46         }
47 });
48
49 XOSCollection = Backbone.Collection.extend({
50     objects: function() {
51                 return this.models.map(function(element) { return element.attributes; });
52              },
53
54     initialize: function(){
55       this.sortVar = 'name';\r
56       this.sortOrder = 'asc';\r
57     },\r
58 \r
59     simpleComparator: function( model ){\r
60       parts=this.sortVar.split(".");\r
61       result = model.get(parts[0]);\r
62       for (index=1; index<parts.length; ++index) {\r
63           result=result[parts[index]];\r
64       }\r
65       return result;\r
66     },\r
67 \r
68     comparator: function (left, right) {\r
69         var l = this.simpleComparator(left);\r
70         var r = this.simpleComparator(right);\r
71 \r
72         if (l === void 0) return -1;\r
73         if (r === void 0) return 1;\r
74 \r
75         if (this.sortOrder=="desc") {\r
76             return l < r ? 1 : l > r ? -1 : 0;\r
77         } else {\r
78             return l < r ? -1 : l > r ? 1 : 0;\r
79         }\r
80     },\r
81 \r
82     startPolling: function() {\r
83         if (!this._polling) {
84             collection=this;
85             setInterval(function() { collection.fetch(); }, 10000);
86             this._polling=true;
87             this.fetch();
88         }
89     },
90
91     maybeFetch: function(options){
92             // Helper function to fetch only if this collection has not been fetched before.
93         if(this._fetched){
94                 // If this has already been fetched, call the success, if it exists
95             options.success && options.success();
96             console.log("alreadyFetched");
97             return;
98         }
99
100             // when the original success function completes mark this collection as fetched
101         var self = this,
102         successWrapper = function(success){
103             return function(){
104                 self._fetched = true;
105                 success && success.apply(this, arguments);
106             };
107         };
108         options.success = successWrapper(options.success);
109         console.log("call fetch");
110         this.fetch(options);
111     },
112
113     getOrFetch: function(id, options){
114             // Helper function to use this collection as a cache for models on the server
115         var model = this.get(id);
116
117         if(model){
118             options.success && options.success(model);
119             return;
120         }
121
122         model = new this.model({
123             resource_uri: id
124         });
125
126         model.fetch(options);
127     },
128
129     /* from backbone-tastypie.js */
130     url: function( models ) {
131                 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
132                 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
133
134                 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
135                 // (set to 'resource_uri') contains the model's id.
136                 if ( models && models.length ) {
137                         var ids = _.map( models, function( model ) {
138                                         var parts = _.compact( model.id.split('/') );
139                                         return parts[ parts.length - 1 ];
140                                 });
141                         url += 'set/' + ids.join(';') + '/';
142                 }
143
144                 url && ( url += "?no_hyperlinks=1" );
145
146                 return url;
147         },
148
149         listMethods: function() {
150             var res = [];\r
151             for(var m in this) {\r
152                 if(typeof this[m] == "function") {\r
153                     res.push(m)\r
154                 }\r
155             }\r
156             return res;\r
157         }
158 });
159
160 function xoslib() {
161     // basic REST
162     this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
163     this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
164                                                    model: this.sliver});
165     this.slivers = new this.sliverCollection();
166
167     this.slice = XOSModel.extend({ urlRoot: SLICE_API });
168     this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
169                                                    model: this.slice});
170     this.slices = new this.sliceCollection();
171
172     this.node = XOSModel.extend({ urlRoot: NODE_API });
173     this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
174                                                    model: this.node});
175     this.nodes = new this.nodeCollection();
176
177     this.site = XOSModel.extend({ urlRoot: SITE_API });
178     this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
179                                                    model: this.site});
180     this.sites = new this.siteCollection();
181
182     this.user = XOSModel.extend({ urlRoot: USER_API });
183     this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
184                                                    model: this.user});
185     this.users = new this.userCollection();
186
187     this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
188     this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
189                                                        model: this.deployment});
190     this.deployments = new this.deploymentCollection();
191
192     // enhanced REST
193     this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
194     this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
195                                                       model: this.slicePlus});
196     this.slicesPlus = new this.slicePlusCollection();
197
198     this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
199 };
200
201 xos = new xoslib();
202