diff options
author | 2014-10-17 10:27:13 +0800 | |
---|---|---|
committer | 2014-10-17 10:28:26 +0800 | |
commit | a64c372a2845ab566f437625f790091e09190e90 (patch) | |
tree | 4599d05b3d3fc0aa9d2db0558ae0d60fee17f5bd /share/tools | |
parent | 1f91a2a6f539d300e4f7cf5a4ffa9b23d0c2fda8 (diff) |
web_config: add support for adding and editing abbreviations
Possible future enhancements include explanatory text and an image for
the 'save' action.
Work on #731.
Diffstat (limited to 'share/tools')
-rw-r--r-- | share/tools/web_config/fishconfig.css | 6 | ||||
-rw-r--r-- | share/tools/web_config/js/controllers.js | 37 | ||||
-rw-r--r-- | share/tools/web_config/partials/abbreviations.html | 16 | ||||
-rwxr-xr-x | share/tools/web_config/webconfig.py | 33 |
4 files changed, 87 insertions, 5 deletions
diff --git a/share/tools/web_config/fishconfig.css b/share/tools/web_config/fishconfig.css index b93b541a..2097eca6 100644 --- a/share/tools/web_config/fishconfig.css +++ b/share/tools/web_config/fishconfig.css @@ -231,6 +231,12 @@ body { border-bottom: #444 dotted 1px; } + .abbreviation_actions { + width: 5em; + text-align: right; + border-bottom: #444 dotted 1px; +} + /* The CSS we apply when a table row is filtered */ .data_table_row_filtered { display: none; diff --git a/share/tools/web_config/js/controllers.js b/share/tools/web_config/js/controllers.js index 65489cfd..5f4135c1 100644 --- a/share/tools/web_config/js/controllers.js +++ b/share/tools/web_config/js/controllers.js @@ -287,10 +287,45 @@ controllers.controller("bindingsController", function($scope, $http) { controllers.controller("abbreviationsController", function($scope, $http) { $scope.abbreviations = []; + $scope.addBlank = function() { + // Add blank entry if it is missing + hasBlank = {hasBlank: false} + angular.forEach($scope.abbreviations, function(value, key) { + if (value.phrase === "" && value.word === "") { + this.hasBlank = true; + } + }, hasBlank); + if (! hasBlank.hasBlank) { + $scope.abbreviations.push({phrase: "", word: "", editable: true}) + } + } $scope.fetchAbbreviations = function() { $http.get("abbreviations/").success(function(data, status, headers, config) { $scope.abbreviations = data; + $scope.addBlank(); })}; + $scope.editAbbreviation = function(abbreviation) { + abbreviation.editable = true; + } + + $scope.saveAbbreviation = function(abbreviation) { + if (abbreviation.word && abbreviation.phrase) { + $http.post("save_abbreviation/", abbreviation).success(function(data, status, headers, config) { + abbreviation.editable = false; + $scope.addBlank(); + }); + } + }; + + $scope.removeAbbreviation = function(abbreviation) { + if (abbreviation.word) { + $http.post("remove_abbreviation/", abbreviation).success(function(data, status, headers, config) { + $scope.abbreviations.splice($scope.abbreviations.indexOf(abbreviation), 1); + $scope.addBlank(); + }); + } + }; + $scope.fetchAbbreviations(); -});
\ No newline at end of file +}); diff --git a/share/tools/web_config/partials/abbreviations.html b/share/tools/web_config/partials/abbreviations.html index 563865a8..e56281ef 100644 --- a/share/tools/web_config/partials/abbreviations.html +++ b/share/tools/web_config/partials/abbreviations.html @@ -4,9 +4,19 @@ <table class="data_table"> <tbody> - <tr class="data_table_row" ng-repeat="abbreviation in abbreviations | filterAbbreviations:query"> - <td ng-class="{ data_table_cell: true}" style="text-align: right; padding-right: 30px;" ng-click="abbreviation._is_selected = !abbreviation._is_selected">{{ abbreviation.word }}</td> - <td ng-class="{ data_table_cell: true}" style="text-align: left; padding-right: 30px;" ng-click="abbreviation._is_selected = !abbreviation._is_selected">{{ abbreviation.phrase }}</td> + <tr class="data_table_row" ng-repeat="abbreviation in abbreviations | filterAbbreviations:query" ng-click="editAbbreviation(abbreviation)"> + <td ng-class="{ data_table_cell: true }" style="text-align: right; padding-right: 30px;"> + <span ng-hide="abbreviation.editable">{{ abbreviation.word }}</span> + <span ng-show="abbreviation.editable"><input ng-model="abbreviation.word"></span> + </td> + <td ng-class="{ data_table_cell: true }" style="text-align: left; padding-right: 30px;"> + <span ng-hide="abbreviation.editable">{{ abbreviation.phrase }}</span> + <span ng-show="abbreviation.editable"><input ng-model="abbreviation.phrase"></span> + </td> + <td ng-class="{ data_table_cell: true }" class="abbreviation_actions"> + <span ng-show="abbreviation.editable && abbreviation.word && abbreviation.phrase" ng-click="saveAbbreviation(abbreviation)">Save</span> + <a ng-show="abbreviation.word" ng-click="removeAbbreviation(abbreviation)"><img alt="Delete" src="delete.png"></a> + </td> </tr> </tbody> </table> diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index cefeaab9..31fa79b7 100755 --- a/share/tools/web_config/webconfig.py +++ b/share/tools/web_config/webconfig.py @@ -699,6 +699,20 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): result = [] return result + def do_remove_abbreviation(self, abbreviation): + out, err = run_fish_cmd('abbr -r %s' % abbreviation['word']) + if out or err: + return err + else: + return True + + def do_save_abbreviation(self, abbreviation): + out, err = run_fish_cmd('abbr -a \'%s=%s\'' % (abbreviation['word'], abbreviation['phrase'])) + if err: + return err + else: + return True + def secure_startswith(self, haystack, needle): if len(haystack) < len(needle): return False @@ -780,6 +794,10 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): length = int(self.headers['content-length']) url_str = self.rfile.read(length).decode('utf-8') postvars = cgi.parse_qs(url_str, keep_blank_values=1) + elif ctype == 'application/json': + length = int(self.headers['content-length']) + url_str = self.rfile.read(length).decode('utf-8') + postvars = json.loads(url_str) else: postvars = {} @@ -810,12 +828,25 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): output = ["OK"] else: output = ["Unable to set prompt"] + elif p == '/save_abbreviation/': + r = self.do_save_abbreviation(postvars) + if r == True: + output = ["OK"] + else: + output = [r] + elif p == '/remove_abbreviation/': + r = self.do_remove_abbreviation(postvars) + if r == True: + output = ["OK"] + else: + output = [r] else: return self.send_error(404) # Return valid output self.send_response(200) - self.send_header('Content-type','text/html') + self.send_header('Content-type','application/json') + self.end_headers() self.write_to_wfile('\n') # Output JSON |