@spalger/kibana
Version:
Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic
49 lines (42 loc) • 1.17 kB
JavaScript
define(function (require) {
var _ = require('lodash');
var $ = require('jquery');
var module = require('ui/modules').get('kibana');
module.directive('validateJson', function ($compile) {
return {
restrict: 'A',
require: 'ngModel',
scope: {
'ngModel': '=',
'queryInput': '=?',
},
link: function ($scope, $elem, attr, ngModel) {
$scope.$watch('ngModel', validator);
function validator(newValue, oldValue) {
if (!newValue || newValue.length === 0) {
setValid();
return;
}
// We actually need a proper object in all JSON inputs
newValue = (newValue || '').trim();
if (newValue[0] === '{' || '[') {
try {
JSON.parse(newValue);
setValid();
} catch (e) {
setInvalid();
}
} else {
setInvalid();
}
}
function setValid() {
ngModel.$setValidity('jsonInput', true);
}
function setInvalid() {
ngModel.$setValidity('jsonInput', false);
}
}
};
});
});