SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/helpers-directive.js

357 lines
14 KiB
JavaScript

"use strict";
/**
* Created by viktor.shevchenko on 6/19/2014.
*/
(function () {
'use strict';
angular.module('myitsmApp')
.directive('preventClickEvent', function () {
return function (scope, iElement) {
function clickHandler(event) {
event.preventDefault();
event.stopPropagation();
}
function keyDownHandler(event) {
if (event.which === 13) {
event.preventDefault();
event.stopPropagation();
}
}
iElement.on('click', clickHandler);
iElement.on('keydown keypress', keyDownHandler);
scope.$on('$destroy', function () {
console.log("preventClickEvent: unbind events");
iElement.off('click', clickHandler);
iElement.off('keydown', keyDownHandler);
iElement.off('keypress', keyDownHandler);
});
};
})
.directive('stopPropagationEvent', function () {
return function (scope, iElement) {
function clickHandler(event) {
event.stopPropagation();
}
function keyDownHandler(event) {
if (event.which === 13) {
event.stopPropagation();
}
}
iElement.on('click', clickHandler);
iElement.on('keydown keypress', keyDownHandler);
scope.$on('$destroy', function () {
console.log("preventClickEvent: unbind events");
iElement.off('click', clickHandler);
iElement.off('keydown', keyDownHandler);
iElement.off('keypress', keyDownHandler);
});
};
})
.directive('preventModalClose', function () {
return function (scope, iElement) {
function keyDownHandler(event) {
if (event.which === 27) {
event.preventDefault();
event.stopPropagation();
}
}
iElement.on('keydown keypress', keyDownHandler);
scope.$on('$destroy', function () {
console.log("preventModalClose: unbind events");
iElement.off('keydown', keyDownHandler);
iElement.off('keypress', keyDownHandler);
});
};
})
.directive('modalPopupMenuClose', ['$timeout', function ($timeout) {
return function (scope, iElement) {
function keyDownHandler(event) {
if (event.which === 27) {
event.preventDefault();
event.stopPropagation();
$timeout(function () {
angular.element(iElement.children()[0]).trigger('click');
}, 0, false);
}
}
iElement.on('keydown keypress', keyDownHandler);
scope.$on('$destroy', function () {
console.log("modalPopupMenuClose: unbind events");
iElement.off('keydown', keyDownHandler);
iElement.off('keypress', keyDownHandler);
});
};
}])
.directive('preventConsoleFilterClose', ['events', function (events) {
return function (scope, iElement) {
function clickHandler(event) {
event.preventDefault();
event.stopPropagation();
scope.$broadcast(events.ASSET_CONSOLE_FILTER_CLICK);
}
function keyDownKeyPressHandler(event) {
if (event.which === 13) {
event.preventDefault();
event.stopPropagation();
scope.$broadcast(events.ASSET_CONSOLE_FILTER_CLICK);
}
else {
scope.$broadcast(events.ASSET_CONSOLE_FILTER_CLICK, event);
}
}
iElement.on('click', clickHandler);
iElement.on('keydown keypress', keyDownKeyPressHandler);
iElement.on("$destroy", function () {
console.log("preventConsoleFilterClose element $destroy event");
iElement.off('click', clickHandler);
iElement.off('keydown', keyDownKeyPressHandler);
iElement.off('keypress', keyDownKeyPressHandler);
});
scope.$on('$destroy', function () {
console.log("preventConsoleFilterClose scope $destroy event");
iElement.off('click', clickHandler);
iElement.off('keydown', keyDownKeyPressHandler);
iElement.off('keypress', keyDownKeyPressHandler);
});
};
}])
/**
* use vertical-screen-fit="nameSpace"
*/
.directive('verticalScreenFit', ['$window', function ($window) {
return function (scope, iElement, iAttrs) {
if (!iAttrs.verticalScreenFit) {
return;
}
var w = $($window), nameSpace = '.' + iAttrs.verticalScreenFit, additionalOffset = iAttrs.verticalScreenOffset || 30, calculateHeight = function () {
var elementOffset;
if (iElement.is(':visible')) {
elementOffset = iElement.offset().top;
}
else {
var firstVisibleParent = iElement.parents(':visible').first();
elementOffset = firstVisibleParent.offset().top + firstVisibleParent.outerHeight();
}
iElement.css('max-height', (w.outerHeight() - elementOffset - additionalOffset) + 'px');
};
calculateHeight();
w.on('resize' + nameSpace, calculateHeight);
iElement.on('$destroy', function () {
w.off('resize' + nameSpace, calculateHeight);
});
scope.$on('$destroy', function () {
w.off('resize' + nameSpace, calculateHeight);
});
};
}])
.directive('formAutofillFix', ['$timeout', function ($timeout) {
return function (scope, iElem, iAttrs) {
// Fixes Chrome bug: https://groups.google.com/forum/#!topic/angular/6NlucSskQjY
iElem.prop('method', 'POST');
// Fix autofill issues where Angular doesn't know about autofilled inputs
function onSubmitHandler(e) {
e.preventDefault();
iElem.find('input, textarea, select').trigger('input').trigger('change').trigger('keydown').trigger('blur'); //cross browser trick
scope.$apply(iAttrs.ngSubmit);
}
if (iAttrs.ngSubmit) {
$timeout(function () {
iElem.off('submit').on('submit', onSubmitHandler);
});
}
scope.$on('$destroy', function () {
iElem.off('submit', onSubmitHandler);
});
};
}])
/**
* Set focus on an element
* Please note, in order to make focus() work on a div
* you have to specify tabindex attr on that element
*/
.directive('autoFocus', ['$timeout', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element) {
$timeout(function () {
element[0].focus();
}, 0);
}
};
}])
/**
* Set focus on an element based on a condition - eg. when a modal closes
*/
.directive('setFocus', function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
scope.$watch(attr.setFocus, function (value) {
if (value === true) {
element[0].focus();
}
});
}
};
})
/**
* keep tab within modal
*/
.directive('modalContent', function () {
return {
restrict: 'C',
link: function (scope, element) {
var children, first, last, onFirstWithShift, onLastNoShift;
function keyUpHandler(event) {
if (event.which === 9) { // tab
if (onFirstWithShift || onLastNoShift) {
event.preventDefault();
}
}
}
function keyDownHandler(event) {
if (event.which === 9) { // tab
children = element.find(':tabbable');
first = children[0];
last = children[children.length - 1];
onFirstWithShift = event.target === first && event.shiftKey;
onLastNoShift = event.target === last && !event.shiftKey;
if (onFirstWithShift) {
event.preventDefault();
last.focus();
}
else if (onLastNoShift) {
event.preventDefault();
first.focus();
}
}
}
element.on('keyup', keyUpHandler);
element.on('keydown', keyDownHandler);
scope.$on("$destroy", function () {
console.log("modalContent: unbind events");
element.off('keyup', keyUpHandler);
element.off('keydown', keyDownHandler);
});
}
};
})
/**
* put focus back to input field after clear, this is not the best solution
* TODO:
* a better solution is to make the typeahead input a directive, similar to the selection directive
*/
.directive('focusInputOnClear', function () {
return {
restrict: 'A',
link: function (scope, element) {
var inputElement = element.parent().find('input');
function keyDownKeyPressHandler(event) {
if (event.which === 13) {
inputElement.focus();
}
}
function clickHandler() {
inputElement.focus();
}
element.on('keydown keypress', keyDownKeyPressHandler);
element.on('click', clickHandler);
scope.$on("$destroy", function () {
console.log("focusInputOnClear: unbind events");
element.off('click', clickHandler);
element.off('keydown', keyDownKeyPressHandler);
element.off('keypress', keyDownKeyPressHandler);
});
}
};
})
/**
* activate file input with [Enter] in IE
*/
.directive('ieActivateByEnter', ['$window', function ($window) {
return {
restrict: 'A',
link: function (scope, element) {
function keyDownHandler(event) {
if (event.which === 13) {
element.click();
}
}
if (($window.navigator.userAgent.indexOf('MSIE') > -1) || ($window.navigator.userAgent.indexOf('Trident\/7.0') > -1)) {
element.on('keydown', keyDownHandler);
}
scope.$on("$destroy", function () {
console.log("ieActivateByEnter: unbind events");
element.off('keydown', keyDownHandler);
});
}
};
}])
/**
* activate file input on button click
*/
.directive('uploadFile', [function () {
return {
restrict: 'A',
link: function (scope, element) {
function clickHandler(e) {
angular.element(e.target).siblings('#uploadAttachment').trigger('click');
}
element.bind('click', clickHandler);
scope.$on('$destroy', function () {
element.off('click', clickHandler);
});
}
};
}])
/**
* emit end-of-ng-repeat event
*/
.directive('emitLastElement', ['events', '$timeout', function (events, $timeout) {
return {
restrict: 'A',
link: function (scope) {
if (scope.$last) {
$timeout(function () {
scope.$emit(events.LAST_REPEATER_ELEMENT);
});
}
}
};
}])
.directive('iframeOnload', [function () {
return {
scope: {
callBack: '&iframeOnload'
},
link: function (scope, element, attrs) {
element.on('load', function () {
return scope.callBack();
});
}
};
}])
/**
* return appropriate priority class
*/
.directive('ticketPriorityDisplay', [function () {
return {
restrict: 'A',
scope: {
priorityValue: '@'
},
link: function (scope, element) {
var ootbPriorities = ['critical', 'high', 'medium', 'low'], priorityVal = 'custom';
scope.$watch('priorityValue', function (newValue, oldValue) {
priorityVal = _.includes(ootbPriorities, newValue.toLowerCase()) ? newValue.toLowerCase() : 'custom';
element.removeClass('ticket__priority-display-' + oldValue.toLowerCase());
element.addClass('ticket__priority-display-' + priorityVal);
if (priorityVal !== 'custom') {
element.removeClass('ticket__priority-display-custom');
}
});
}
};
}]);
}());