From efac73f4bc3cb0f1e288d70157e14487a3f829bb Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Wed, 8 Feb 2017 17:28:23 +0100 Subject: [PATCH] mgCrud como modulo npm externo, vnFocus sobre botones --- client/client/src/index/index.html | 6 ++++++ client/core/src/directives/focus.js | 23 ++++++++++++++++------- client/core/src/directives/id.js | 11 ++++++++++- client/vendor/src/vendor.js | 4 ++-- package.json | 2 +- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/client/client/src/index/index.html b/client/client/src/index/index.html index 3cc5d33a2..140afb414 100644 --- a/client/client/src/index/index.html +++ b/client/client/src/index/index.html @@ -1,3 +1,9 @@ + +
diff --git a/client/core/src/directives/focus.js b/client/core/src/directives/focus.js index e4985f7c4..34b992642 100644 --- a/client/core/src/directives/focus.js +++ b/client/core/src/directives/focus.js @@ -1,16 +1,25 @@ import {module} from '../module'; -// Sets the focus and selects the text on the input. - +/** + * Sets the focus and selects the text on the input. + */ export function directive() { return { restrict: 'A', link: function($scope, $element, $attrs) { - $scope.$watch($attrs.vnFocus, function(value) { - if ($element[0].getElementsByTagName("INPUT")[0]) - $element[0].getElementsByTagName("INPUT")[0].focus(); - if ($element[0].getElementsByTagName("TEXTAREA")[0]) - $element[0].getElementsByTagName("TEXTAREA")[0].focus(); + $scope.$watch('', function() { + let input = $element[0]; + let selector = 'input, textarea, button, submit'; + + if(!input.matches(selector)) + input = input.querySelector(selector); + if(!input) + throw new Error(`vnFocus: Can't find a focusable element`); + + input.focus(); + + if(input.select) + input.select(); }); } }; diff --git a/client/core/src/directives/id.js b/client/core/src/directives/id.js index 2f9eb6c72..4b6b4d22d 100644 --- a/client/core/src/directives/id.js +++ b/client/core/src/directives/id.js @@ -8,8 +8,17 @@ import {kebabToCamel} from '../lib/string'; export function directive() { return { restrict: 'A', + require: [], link: function($scope, $element, $attrs) { - $scope[kebabToCamel($attrs.vnId)] = $element[0].$ctrl; + let id = kebabToCamel($attrs.vnId); + let controller = $element[0].$ctrl; + + if(!id) + throw new Error(`vnId: Attribute can't be null`); + if(!controller) + throw new Error(`vnId: Can't find controller for element '${id}'`); + + $scope[id] = controller; } }; }; diff --git a/client/vendor/src/vendor.js b/client/vendor/src/vendor.js index 25196f39a..09a92d155 100644 --- a/client/vendor/src/vendor.js +++ b/client/vendor/src/vendor.js @@ -1,8 +1,8 @@ export * from './angular'; -export * from './oc-lazy-load'; export * from './ui-router'; +export * from './mg-crud'; +export * from './oc-lazy-load'; export * from './angular-translate'; export * from './material-design-lite'; export * from './angular-paging'; export * from './validator'; -//export * from './mg-crud'; \ No newline at end of file diff --git a/package.json b/package.json index c0b7dce1b..8c2bdb97c 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "angular-translate-loader-partial": "^2.13.1", "angular-ui-router": "^1.0.0-beta.3", "material-design-lite": "^1.3.0", - "mg-crud": "^1.0.1", + "mg-crud": "^1.1.2", "oclazyload": "^0.6.3", "validator": "^6.2.1" },