From a64c372a2845ab566f437625f790091e09190e90 Mon Sep 17 00:00:00 2001 From: David Adam Date: Fri, 17 Oct 2014 10:27:13 +0800 Subject: [PATCH] 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. --- share/tools/web_config/fishconfig.css | 6 +++ share/tools/web_config/js/controllers.js | 37 ++++++++++++++++++- .../web_config/partials/abbreviations.html | 16 ++++++-- 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 b93b541a5..2097eca62 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 65489cfd4..5f4135c1c 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 563865a8e..e56281ef6 100644 --- a/share/tools/web_config/partials/abbreviations.html +++ b/share/tools/web_config/partials/abbreviations.html @@ -4,9 +4,19 @@ - - - + + + +
{{ abbreviation.word }}{{ abbreviation.phrase }}
+ {{ abbreviation.word }} + + + {{ abbreviation.phrase }} + + + Save + Delete +
diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index cefeaab95..31fa79b77 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