Kouhei Sutou
null+****@clear*****
Mon Oct 27 16:59:57 JST 2014
Kouhei Sutou 2014-10-27 16:59:57 +0900 (Mon, 27 Oct 2014) New Revision: 20395862a3607b77eb0c6d52b1f8c861cbe88dfb https://github.com/groonga/groonga-admin/commit/20395862a3607b77eb0c6d52b1f8c861cbe88dfb Message: Create Groonga client and use it Added files: app/scripts/groonga-client.js Modified files: .jshintrc app/index.html app/scripts/controllers/table-search-controller.js app/scripts/controllers/top-controller.js app/scripts/groonga-response.js app/scripts/groonga-response/select.js app/scripts/groonga-response/table-list.js Modified: .jshintrc (+1 -0) =================================================================== --- .jshintrc 2014-10-27 16:08:47 +0900 (34c0116) +++ .jshintrc 2014-10-27 16:59:57 +0900 (76034f5) @@ -20,6 +20,7 @@ "smarttabs": true, "globals": { "angular": false, + "GroongaClient": false, "GroongaResponse": false } } Modified: app/index.html (+1 -0) =================================================================== --- app/index.html 2014-10-27 16:08:47 +0900 (aeaa2cd) +++ app/index.html 2014-10-27 16:59:57 +0900 (6981a5d) @@ -73,6 +73,7 @@ <script src="scripts/groonga-response.js"></script> <script src="scripts/groonga-response/select.js"></script> <script src="scripts/groonga-response/table-list.js"></script> + <script src="scripts/groonga-client.js"></script> <script src="scripts/app.js"></script> <script src="scripts/controllers/top-controller.js"></script> <script src="scripts/controllers/table-search-controller.js"></script> Modified: app/scripts/controllers/table-search-controller.js (+21 -42) =================================================================== --- app/scripts/controllers/table-search-controller.js 2014-10-27 16:08:47 +0900 (115f3d8) +++ app/scripts/controllers/table-search-controller.js 2014-10-27 16:59:57 +0900 (247e5f4) @@ -9,25 +9,6 @@ */ angular.module('groongaAdminApp') .controller('TableSearchController', function ($scope, $routeParams, $location, $http) { - function escapeCommandValue(value) { - return value.replace(/(["\\])/g, function(match) { - return '\\' + match[1]; - }); - } - - function buildCommandLine(name, parameters) { - var components = [name]; - angular.forEach(parameters, function(value, key) { - if (key === 'callback') { - return; - } - - components.push('--' + key); - components.push('"' + escapeCommandValue(value) + '"'); - }); - return components.join(' '); - } - $scope.table = $routeParams.table; $scope.style = 'table'; $scope.rawData = []; @@ -57,29 +38,27 @@ angular.module('groongaAdminApp') } parameters[key] = value; }); - $http.jsonp('/d/select.json', {params: parameters}) - .success(function(data) { - $scope.rawData = data; - - $scope.commandLine = buildCommandLine('select', parameters); - - var response = new GroongaResponse.Select(data); - $scope.elapsedTimeInMilliseconds = response.elapsedTime() * 1000; - if (!response.isSuccess()) { - $scope.message = - 'Failed to call "select" command: ' + response.errorMessage(); - $scope.nTotalRecords = 0; - return; - } - $scope.nTotalRecords = response.nTotalRecords(); - $scope.columns = response.columns(); - $scope.records = response.records().map(function(record) { - return record.map(function(value, index) { - return { - value: value, - column: $scope.columns[index] - }; - }); + var client = new GroongaClient($http); + var request = client.execute('select', parameters); + request.success(function(response) { + $scope.rawData = response.rawData(); + $scope.commandLine = request.commandLine(); + $scope.elapsedTimeInMilliseconds = response.elapsedTime() * 1000; + if (!response.isSuccess()) { + $scope.message = + 'Failed to call "select" command: ' + response.errorMessage(); + $scope.nTotalRecords = 0; + return; + } + $scope.nTotalRecords = response.nTotalRecords(); + $scope.columns = response.columns(); + $scope.records = response.records().map(function(record) { + return record.map(function(value, index) { + return { + value: value, + column: $scope.columns[index] + }; }); }); + }); }); Modified: app/scripts/controllers/top-controller.js (+3 -4) =================================================================== --- app/scripts/controllers/top-controller.js 2014-10-27 16:08:47 +0900 (ff3b6e8) +++ app/scripts/controllers/top-controller.js 2014-10-27 16:59:57 +0900 (e81dc95) @@ -10,10 +10,9 @@ angular.module('groongaAdminApp') .controller('TopController', function ($scope, $http) { $scope.tables = []; - $http.jsonp('/d/table_list', {params: {callback: 'JSON_CALLBACK'}}) - .success(function(data) { - var response = new GroongaResponse.TableList(data); + var client = new GroongaClient($http); + var request = client.execute('table_list', {}); + request.success(function(response) { $scope.tables = response.tables(); - console.log($scope.tables); }); }); Added: app/scripts/groonga-client.js (+65 -0) 100644 =================================================================== --- /dev/null +++ app/scripts/groonga-client.js 2014-10-27 16:59:57 +0900 (0f710dd) @@ -0,0 +1,65 @@ +'use strict'; + +(function() { + function GroongaClient($http) { + this._$http = $http; + this._pathPrefix = '/d/'; + } + window.GroongaClient = GroongaClient; + + GroongaClient.prototype.execute = function(name, parameters) { + var params = { + callback: 'JSON_CALLBACK' + }; + for (var key in parameters) { + params[key] = parameters[key]; + } + var rawRequest = this._$http.jsonp(this._pathPrefix + name + '.json', + {params: params}); + var request = new GroongaClient.Request(rawRequest, name, params); + return request; + }; + + GroongaClient.Request = function(rawRequest, name, parameters) { + this._rawRequest = rawRequest; + this._name = name; + this._parameters = parameters; + }; + + GroongaClient.Request.prototype.success = function(callback) { + var name = this._name; + this._rawRequest.success(function(data, status, headers, config) { + var ResponseConstructor = GroongaClient.Response.find(name); + var response = new ResponseConstructor(data); + callback(response, status, headers, config); + }); + return this; + }; + + GroongaClient.Request.prototype.error = function(callback) { + this._rawRequest.error(callback); + return this; + }; + + GroongaClient.Request.prototype.commandLine = function() { + function escapeCommandValue(value) { + return value.replace(/(["\\])/g, function(match) { + return '\\' + match[1]; + }); + } + + var components = [this._name]; + for (var key in this._parameters) { + if (key === 'callback') { + continue; + } + + var value = this._parameters[key]; + components.push('--' + key); + components.push('"' + escapeCommandValue(value) + '"'); + } + return components.join(' '); + }; + + GroongaClient.Response = GroongaResponse; +})(); Modified: app/scripts/groonga-response.js (+11 -0) =================================================================== --- app/scripts/groonga-response.js 2014-10-27 16:08:47 +0900 (8638b2b) +++ app/scripts/groonga-response.js 2014-10-27 16:59:57 +0900 (5ac1475) @@ -2,3 +2,14 @@ window.GroongaResponse = { }; + +window.GroongaResponse.find = function(name) { + var constructorName = name.replace(/(^.|_.)/g, function(matched) { + if (matched.length === 1) { + return matched.toUpperCase(); + } else { + return matched[1].toUpperCase(); + } + }); + return window.GroongaResponse[constructorName]; +}; Modified: app/scripts/groonga-response/select.js (+4 -0) =================================================================== --- app/scripts/groonga-response/select.js 2014-10-27 16:08:47 +0900 (fa51f6f) +++ app/scripts/groonga-response/select.js 2014-10-27 16:59:57 +0900 (cac09f2) @@ -6,6 +6,10 @@ } GroongaResponse.Select = Select; + Select.prototype.rawData = function() { + return this._rawData; + }; + Select.prototype.header = function() { return this._rawData[0]; }; Modified: app/scripts/groonga-response/table-list.js (+4 -0) =================================================================== --- app/scripts/groonga-response/table-list.js 2014-10-27 16:08:47 +0900 (b08e592) +++ app/scripts/groonga-response/table-list.js 2014-10-27 16:59:57 +0900 (37e01a6) @@ -6,6 +6,10 @@ } GroongaResponse.TableList = TableList; + TableList.prototype.rawData = function() { + return this._rawData; + }; + TableList.prototype.header = function() { return this._rawData[0]; }; -------------- next part -------------- HTML����������������������������...Descargar