2014-10-16 12:25:28 -04:00
|
|
|
'use strict';
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
var ngModule = angular.module('woDirectives', []);
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
ngModule.directive('woTouch', function($parse) {
|
|
|
|
var className = 'wo-touch-active';
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
return function(scope, elm, attrs) {
|
|
|
|
var handler = $parse(attrs.woTouch);
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
elm.on('touchstart', function() {
|
|
|
|
elm.addClass(className);
|
|
|
|
});
|
|
|
|
elm.on('touchleave touchcancel touchmove touchend', function() {
|
|
|
|
elm.removeClass(className);
|
|
|
|
});
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
elm.on('click', function(event) {
|
|
|
|
elm.removeClass(className);
|
|
|
|
scope.$apply(function() {
|
|
|
|
handler(scope, {
|
|
|
|
$event: event
|
2014-09-23 09:41:37 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-16 12:25:28 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
ngModule.directive('woTooltip', function($document, $timeout) {
|
|
|
|
return function(scope, elm, attrs) {
|
|
|
|
var selector = attrs.woTooltip;
|
|
|
|
var tooltip = $document.find(selector);
|
|
|
|
|
|
|
|
elm.on('mouseover', function() {
|
|
|
|
// Compute tooltip position
|
|
|
|
var offsetElm = elm.offset();
|
|
|
|
var offsetTooltipParent = tooltip.offsetParent().offset();
|
|
|
|
|
|
|
|
// Set tooltip position
|
|
|
|
tooltip[0].style.top = (offsetElm.top - offsetTooltipParent.top +
|
|
|
|
elm[0].offsetHeight / 2 - tooltip[0].offsetHeight / 2) + 'px';
|
|
|
|
tooltip[0].style.left = (offsetElm.left - offsetTooltipParent.left +
|
|
|
|
elm[0].offsetWidth) + 'px';
|
|
|
|
|
|
|
|
// Wait till browser repaint
|
|
|
|
$timeout(function() {
|
|
|
|
tooltip.addClass('tooltip--show');
|
2014-09-23 09:41:37 -04:00
|
|
|
});
|
2014-10-16 12:25:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
elm.on('mouseout', function() {
|
|
|
|
tooltip.removeClass('tooltip--show');
|
|
|
|
tooltip[0].style.top = '-9999px';
|
|
|
|
tooltip[0].style.left = '-9999px';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
ngModule.directive('woDropdown', function($document, $timeout) {
|
|
|
|
return function(scope, elm, attrs) {
|
|
|
|
var selector = attrs.woDropdown;
|
|
|
|
var position = (attrs.woDropdownPosition || '').split(' ');
|
|
|
|
var dropdown = $document.find(selector);
|
|
|
|
|
|
|
|
function appear() {
|
|
|
|
// Compute dropdown position
|
|
|
|
var offsetElm = elm.offset();
|
|
|
|
var offsetDropdownParent = dropdown.offsetParent().offset();
|
|
|
|
|
|
|
|
// Set dropdown position
|
|
|
|
switch(position[1]) {
|
|
|
|
case 'up':
|
|
|
|
dropdown[0].style.top = (offsetElm.top - offsetDropdownParent.top -
|
|
|
|
dropdown[0].offsetHeight - 3) + 'px';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dropdown[0].style.top = (offsetElm.top - offsetDropdownParent.top +
|
|
|
|
elm[0].offsetHeight + 3) + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(position[0]) {
|
|
|
|
case 'center':
|
|
|
|
dropdown[0].style.left = (offsetElm.left - offsetDropdownParent.left +
|
|
|
|
elm[0].offsetWidth / 2 - dropdown[0].offsetWidth / 2) + 'px';
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
dropdown[0].style.left = (offsetElm.left - offsetDropdownParent.left +
|
|
|
|
elm[0].offsetWidth - dropdown[0].offsetWidth) + 'px';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dropdown[0].style.left = (offsetElm.left - offsetDropdownParent.left) + 'px';
|
|
|
|
}
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
// Wait till browser repaint
|
|
|
|
$timeout(function() {
|
|
|
|
dropdown.addClass('dropdown--show');
|
2014-09-23 09:41:37 -04:00
|
|
|
});
|
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
// class on element to change style if drowdown is visible
|
|
|
|
elm.addClass('wo-dropdown-active');
|
|
|
|
}
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
function disappear() {
|
|
|
|
dropdown.removeClass('dropdown--show');
|
|
|
|
dropdown[0].style.top = '-9999px';
|
|
|
|
dropdown[0].style.left = '-9999px';
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
elm.removeClass('wo-dropdown-active');
|
|
|
|
}
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
function toggle() {
|
|
|
|
if(dropdown.hasClass('dropdown--show')) {
|
|
|
|
disappear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appear();
|
2014-09-23 09:41:37 -04:00
|
|
|
}
|
2014-10-16 12:25:28 -04:00
|
|
|
}
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-11-29 04:33:48 -05:00
|
|
|
elm.on('click', function(e) {
|
2014-11-10 07:51:20 -05:00
|
|
|
e.preventDefault();
|
|
|
|
toggle();
|
|
|
|
});
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-11-06 11:34:59 -05:00
|
|
|
// close if user clicks button in dropdown list
|
2014-11-29 04:33:48 -05:00
|
|
|
dropdown.on('click.woDropdown', 'button', disappear);
|
2014-11-06 11:34:59 -05:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
// close if user clicks outside of dropdown and elm
|
|
|
|
$document.on('touchstart.woDropdown click.woDropdown', function(e) {
|
|
|
|
var t = angular.element(e.target);
|
|
|
|
if(dropdown.hasClass('dropdown--show') &&
|
|
|
|
!t.closest(dropdown).length &&
|
|
|
|
!t.closest(elm).length) {
|
|
|
|
disappear();
|
|
|
|
}
|
|
|
|
});
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
// remove event listener on document
|
|
|
|
scope.$on('$destroy', function() {
|
2014-11-29 04:33:48 -05:00
|
|
|
dropdown.off('click.woDropdown');
|
2014-10-16 12:25:28 -04:00
|
|
|
$document.off('touchstart.woDropdown click.woDropdown');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
2014-09-23 09:41:37 -04:00
|
|
|
|
2014-11-10 08:14:34 -05:00
|
|
|
ngModule.directive('woFocusMe', function($timeout, $parse) {
|
|
|
|
return function(scope, element, attrs) {
|
|
|
|
var model = $parse(attrs.woFocusMe);
|
|
|
|
scope.$watch(model, function(value) {
|
|
|
|
if (value === true) {
|
|
|
|
$timeout(function() {
|
|
|
|
var el = element[0];
|
|
|
|
el.focus();
|
|
|
|
// set cursor to start of textarea
|
|
|
|
if (el.type === 'textarea') {
|
|
|
|
el.selectionStart = 0;
|
|
|
|
el.selectionEnd = 0;
|
|
|
|
el.scrollTop = 0;
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-11-20 09:25:32 -05:00
|
|
|
ngModule.directive('woClickFileInput', function() {
|
|
|
|
return function(scope, elm, attrs) {
|
|
|
|
var fileInput = document.querySelector(attrs.woClickFileInput);
|
|
|
|
elm.on('click touchstart', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
fileInput.click();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-10-16 12:25:28 -04:00
|
|
|
module.exports = ngModule;
|