summary refs log tree commit diff
path: root/webclient/app-directive.js
diff options
context:
space:
mode:
authorKegan Dougal <kegan@matrix.org>2014-10-30 16:21:27 +0000
committerKegan Dougal <kegan@matrix.org>2014-10-30 16:21:27 +0000
commit9de9661baaa232da170e45534a7d335bf96ef606 (patch)
tree82a709a3a048855a35f8a4e003cef9cfee074f1a /webclient/app-directive.js
parentSYWEB-12: More formatting. (diff)
downloadsynapse-9de9661baaa232da170e45534a7d335bf96ef606.tar.xz
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!
Diffstat (limited to 'webclient/app-directive.js')
-rw-r--r--webclient/app-directive.js50
1 files changed, 38 insertions, 12 deletions
diff --git a/webclient/app-directive.js b/webclient/app-directive.js
index d788475f46..c1ba0af3a9 100644
--- a/webclient/app-directive.js
+++ b/webclient/app-directive.js
@@ -41,18 +41,44 @@ angular.module('matrixWebClient')
         }
     };
 }])
-.directive('elastic', [ // http://stackoverflow.com/questions/17772260/textarea-auto-height
-    '$timeout',
-    function($timeout) {
-      return {
+.directive('asjson', function() {
+    return {
         restrict: 'A',
-        link: function($scope, element) {
-          var resize = function() {
-            return element[0].style.height = "" + element[0].scrollHeight + "px";
-          };
-          element.on("blur keyup change", resize);
-          $timeout(resize, 10);
+        require: 'ngModel',
+        link: function (scope, element, attrs, ngModelCtrl) {
+            function isValidJson(model) {
+                var flag = true;
+                try {
+                    angular.fromJson(model);
+                } catch (err) {
+                    flag = false;
+                }
+                return flag;
+            };
+
+            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);
         }
-      };
     }
-]);;
+});