0b0cec98a8697681121c09fdc7ca0e43d02a3368
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 (function( $, app ) {
17
18         var services = app.ns("services");
19         var ux = app.ns("ux");
20
21         function parse_version( v ) {
22                 return v.match(/^(\d+)\.(\d+)\.(\d+)/).slice(1,4).map( function(d) { return parseInt(d || 0, 10); } );
23         }
24
25         services.Cluster = ux.Class.extend({
26                 defaults: {
27                         base_uri: null
28                 },
29                 init: function() {
30                         this.base_uri = this.config.base_uri;
31                 },
32                 setVersion: function( v ) {
33                         this.version = v;
34                         this._version_parts = parse_version( v );
35                 },
36                 versionAtLeast: function( v ) {
37                         var testVersion = parse_version( v );
38                         for( var i = 0; i < 3; i++ ) {
39                                 if( testVersion[i] !== this._version_parts[i] ) {
40                                         return testVersion[i] < this._version_parts[i];
41                                 }
42                         }
43                         return true;
44                 },
45                 request: function( params ) {
46                         return $.ajax( $.extend({
47                                 url: this.base_uri + params.path,
48                                 dataType: "json",
49                                 error: function(xhr, type, message) {
50                                         if("console" in window) {
51                                                 console.log({ "XHR Error": type, "message": message });
52                                         }
53                                 }
54                         },  params) );
55                 },
56                 "get": function(path, success) { return this.request( { type: "GET", path: path, success: success } ); },
57                 "post": function(path, data, success) { return this.request( { type: "POST", path: path, data: data, success: success } ); },
58                 "put": function(path, data, success) { return this.request( { type: "PUT", path: path, data: data, success: success } ); },
59                 "delete": function(path, data, success) { return this.request( { type: "DELETE", path: path, data: data, success: success } ); }
60         });
61
62 })( this.jQuery, this.app );