summary refs log tree commit diff
path: root/webclient/app-directive.js
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-10-31 09:49:43 +0000
committerErik Johnston <erik@matrix.org>2014-10-31 09:49:43 +0000
commitd9a9e9eb300a856a52255f77f40b66d18801153d (patch)
tree5731bcf730053af0f688d49e79eaa4f9bfcb47bc /webclient/app-directive.js
parentFix bug in redaction auth. (diff)
parentSYWEB-12: You'll be needing this. (diff)
downloadsynapse-d9a9e9eb300a856a52255f77f40b66d18801153d.tar.xz
Merge branch 'develop' of github.com:matrix-org/synapse into federation_authorization
Diffstat (limited to 'webclient/app-directive.js')
-rw-r--r--webclient/app-directive.js43
1 files changed, 42 insertions, 1 deletions
diff --git a/webclient/app-directive.js b/webclient/app-directive.js
index 75283598ab..c1ba0af3a9 100644
--- a/webclient/app-directive.js
+++ b/webclient/app-directive.js
@@ -40,4 +40,45 @@ angular.module('matrixWebClient')
             }            
         }
     };
-}]);
\ No newline at end of file
+}])
+.directive('asjson', function() {
+    return {
+        restrict: 'A',
+        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);
+        }
+    }
+});