SYWEB-12: More formatting and tweaking of state event JSON.

Use a proper elastic directive to make the <textarea> resize dynamically.
Use an 'asjson' directive to turn an ngModel of a JSON object into a
formatted JSON string so it can be displayed on the textarea. Also, deep
copy the state events being displayed, else it actually alters the underlying
data structures when playing around with the JSON in the textarea!
This commit is contained in:
Kegan Dougal 2014-10-30 16:21:27 +00:00
parent 6f3f631fd1
commit 9de9661baa
5 changed files with 53 additions and 16 deletions

View File

@ -41,18 +41,44 @@ angular.module('matrixWebClient')
} }
}; };
}]) }])
.directive('elastic', [ // http://stackoverflow.com/questions/17772260/textarea-auto-height .directive('asjson', function() {
'$timeout',
function($timeout) {
return { return {
restrict: 'A', restrict: 'A',
link: function($scope, element) { require: 'ngModel',
var resize = function() { link: function (scope, element, attrs, ngModelCtrl) {
return element[0].style.height = "" + element[0].scrollHeight + "px"; function isValidJson(model) {
var flag = true;
try {
angular.fromJson(model);
} catch (err) {
flag = false;
}
return flag;
}; };
element.on("blur keyup change", resize);
$timeout(resize, 10); function string2JSON(text) {
try {
var j = angular.fromJson(text);
ngModelCtrl.$setValidity('json', true);
return j;
} catch (err) {
//returning undefined results in a parser error as of angular-1.3-rc.0, and will not go through $validators
//return undefined
ngModelCtrl.$setValidity('json', false);
return text;
} }
}; };
function JSON2String(object) {
return angular.toJson(object, true);
};
//$validators is an object, where key is the error
//ngModelCtrl.$validators.json = isValidJson;
//array pipelines
ngModelCtrl.$parsers.push(string2JSON);
ngModelCtrl.$formatters.push(JSON2String);
} }
]);; }
});

View File

@ -31,7 +31,8 @@ var matrixWebClient = angular.module('matrixWebClient', [
'eventStreamService', 'eventStreamService',
'eventHandlerService', 'eventHandlerService',
'infinite-scroll', 'infinite-scroll',
'ui.bootstrap' 'ui.bootstrap',
'monospaced.elastic'
]); ]);
matrixWebClient.config(['$routeProvider', '$provide', '$httpProvider', matrixWebClient.config(['$routeProvider', '$provide', '$httpProvider',

View File

@ -20,6 +20,7 @@
<script type='text/javascript' src="js/ui-bootstrap-tpls-0.11.2.js"></script> <script type='text/javascript' src="js/ui-bootstrap-tpls-0.11.2.js"></script>
<script type='text/javascript' src='js/ng-infinite-scroll-matrix.js'></script> <script type='text/javascript' src='js/ng-infinite-scroll-matrix.js'></script>
<script type='text/javascript' src='js/autofill-event.js'></script> <script type='text/javascript' src='js/autofill-event.js'></script>
<script type='text/javascript' src='js/elastic.js'></script>
<script src="app.js"></script> <script src="app.js"></script>
<script src="config.js"></script> <script src="config.js"></script>
<script src="app-controller.js"></script> <script src="app-controller.js"></script>

View File

@ -1018,6 +1018,13 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
}; };
$scope.openRoomInfo = function() { $scope.openRoomInfo = function() {
var stateFilter = $filter("stateEventsFilter");
var stateEvents = stateFilter($scope.events.rooms[$scope.room_id]);
// The modal dialog will 2-way bind this field, so we MUST make a deep
// copy of the state events else we will be *actually adjusing our view
// of the world* when fiddling with the JSON!! Apparently parse/stringify
// is faster than jQuery's extend when doing deep copies.
$scope.roomInfoStateEvents = JSON.parse(JSON.stringify(stateEvents));
var modalInstance = $modal.open({ var modalInstance = $modal.open({
templateUrl: 'roomInfoTemplate.html', templateUrl: 'roomInfoTemplate.html',
controller: 'RoomInfoController', controller: 'RoomInfoController',
@ -1040,7 +1047,9 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput'])
console.log("Displaying room info."); console.log("Displaying room info.");
$scope.submit = function(event) { $scope.submit = function(event) {
if (event.content) {
console.error("submit >>> " + JSON.stringify(event)); console.error("submit >>> " + JSON.stringify(event));
}
}; };
$scope.dismiss = $modalInstance.dismiss; $scope.dismiss = $modalInstance.dismiss;

View File

@ -16,7 +16,7 @@
<script type="text/ng-template" id="roomInfoTemplate.html"> <script type="text/ng-template" id="roomInfoTemplate.html">
<div class="modal-body"> <div class="modal-body">
<table class="room-info"> <table class="room-info">
<tr ng-repeat="(key, event) in events.rooms[room_id] | stateEventsFilter" class="room-info-event"> <tr ng-repeat="(key, event) in roomInfoStateEvents" class="room-info-event">
<td class="room-info-event-meta" width="30%"> <td class="room-info-event-meta" width="30%">
<span class="monospace">{{ key }}</span> <span class="monospace">{{ key }}</span>
<br/> <br/>
@ -29,7 +29,7 @@
</button> </button>
</td> </td>
<td class="room-info-event-content" width="70%"> <td class="room-info-event-content" width="70%">
<textarea class="room-info-textarea-content" elastic ng-model="event.content | json"></textarea> <textarea class="room-info-textarea-content" msd-elastic ng-model="event.content" asjson></textarea>
</td> </td>
</tr> </tr>
</table> </table>