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.
This commit is contained in:
David Adam 2014-10-17 10:27:13 +08:00
parent 1f91a2a6f5
commit a64c372a28
4 changed files with 87 additions and 5 deletions

View file

@ -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;

View file

@ -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();
});

View file

@ -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>

View file

@ -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