2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2017-02-07 13:34:26 +00:00
|
|
|
import {kebabToCamel} from '../lib/string';
|
|
|
|
|
|
|
|
/**
|
2018-02-20 09:00:19 +00:00
|
|
|
* Registers the element controller into the scope as a property whose name is
|
|
|
|
* the directive value transformed to lowerCamelCase.
|
2018-02-10 15:18:01 +00:00
|
|
|
*
|
|
|
|
* @return {Object} The directive
|
2017-02-07 13:34:26 +00:00
|
|
|
*/
|
|
|
|
export function directive() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function($scope, $element, $attrs) {
|
2017-02-08 16:28:23 +00:00
|
|
|
let id = kebabToCamel($attrs.vnId);
|
2019-11-10 10:08:44 +00:00
|
|
|
|
|
|
|
if (!id)
|
|
|
|
throw new Error(`vnId: Attribute can't be null`);
|
|
|
|
|
2019-09-30 09:30:54 +00:00
|
|
|
let $ctrl = $element[0].$ctrl
|
|
|
|
? $element[0].$ctrl
|
|
|
|
: $element.controller($element[0].tagName.toLowerCase());
|
2019-11-10 10:08:44 +00:00
|
|
|
let ctrl = $ctrl || $element[0];
|
2017-02-08 16:28:23 +00:00
|
|
|
|
2019-11-10 10:08:44 +00:00
|
|
|
$scope[id] = ctrl;
|
|
|
|
|
|
|
|
if (!$scope.hasOwnProperty('$ctrl')) {
|
|
|
|
while ($scope && !$scope.hasOwnProperty('$ctrl'))
|
|
|
|
$scope = Object.getPrototypeOf($scope);
|
2017-02-08 16:28:23 +00:00
|
|
|
|
2019-11-10 10:08:44 +00:00
|
|
|
if ($scope) $scope[id] = ctrl;
|
|
|
|
}
|
2017-02-07 13:34:26 +00:00
|
|
|
}
|
|
|
|
};
|
2017-05-16 10:37:48 +00:00
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.directive('vnId', directive);
|