Created
September 14, 2015 18:48
-
-
Save voznik/e3827a80509005bd5d2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @ngdoc overview | |
# @name ui-commons.hook | |
# @description | |
# a directive to hook enter/leave events to select children without watchers or broadcasting | |
@directive = angular.module('ui-commons.hook', []) | |
@directive.controller 'UiHookController', ['$scope', ($scope) -> | |
hooks = [] | |
@addHook = (fn) -> | |
hooks.push fn | |
@toggle = (enter) -> | |
if hooks.length > 0 | |
angular.forEach hooks, (hook) -> | |
hook(enter) | |
return | |
] | |
# @ngdoc directive | |
# @name ui-commons.hook:uiHook | |
# @description | |
# uiHook is to be set on the parent element on which events are triggered | |
# @param {string=} uiHook category of event, available: [drag] | |
@directive.constant 'uiHookConfig', | |
drag: | |
enter: 'dragstart dragenter dragover' | |
leave: 'dragend drop dragleave' | |
@directive.directive 'uiHook', ['uiHookConfig', (uiHookConfig) -> | |
restrict : 'A' | |
controller: 'UiHookController' | |
link: (scope, element, attrs, ctrl) -> | |
hookConfig = uiHookConfig[attrs.uiHook] | |
if hookConfig | |
element.bind hookConfig.enter, (e) -> | |
ctrl.toggle(true) | |
element.bind hookConfig.leave, (e) -> | |
ctrl.toggle(false) | |
ctrl.toggle(false) | |
] | |
# @ngdoc directive | |
# @name ui-commons.hook:uiHookClass | |
# @description | |
# will set truthy or falsy classes according to events on the hooked parent | |
# @requires uiHook | |
# @param {object=} uiHookClass in the form of {true: 'truthy classes', false: 'falsy classes'} | |
@directive.directive 'uiHookClass', ['$parse', ($parse) -> | |
restrict : 'A' | |
require: '^uiHook' | |
link: (scope, element, attrs, uiHook) -> | |
hookConfig = $parse(attrs.uiHookClass)() | |
uiHook.addHook (toggle) -> | |
element.addClass(if toggle then hookConfig.true else hookConfig.false) | |
element.removeClass(if toggle then hookConfig.false else hookConfig.true) | |
] | |
# @ngdoc directive | |
# @name ui-commons.hook:uiHookClass | |
# @description | |
# will set to true or remove an attribute according to events on the hooked parent | |
# @requires uiHook | |
# @param {string=} uiHookAttr the name of the attribute | |
@directive.directive 'uiHookAttr', [() -> | |
restrict : 'A' | |
require: '^uiHook' | |
link: (scope, element, attrs, uiHook) -> | |
uiHook.addHook (toggle) -> | |
element.attr attrs.uiHookAttr, toggle | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment