salix/front/core/directives/anchor.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-10-16 08:40:45 +00:00
import ngModule from '../module';
2020-10-20 05:43:36 +00:00
export function stringifyParams(data) {
const params = Object.assign({}, data.params);
for (let param in params)
params[param] = JSON.stringify(params[param]);
return params;
}
export function changeState($state, event, data) {
const params = stringifyParams(data);
$state.go(data.state, params);
event.preventDefault();
event.stopPropagation();
}
2020-10-21 08:26:28 +00:00
export function openNewTab($state, $window, event, data) {
const params = stringifyParams(data);
const href = $state.href(data.state, params);
$window.open(href);
event.preventDefault();
event.stopPropagation();
}
2020-10-16 08:40:45 +00:00
/**
* Allows changing state for nested anchor
*
* @param {Object} $state
2020-10-21 08:26:28 +00:00
* @param {Object} $window
2020-10-16 08:40:45 +00:00
* @return {Object} The directive
*/
2020-10-19 05:35:24 +00:00
export function directive($state, $window) {
let ctrlPressed = false;
$window.addEventListener('keydown', event => {
if (event.key == 'Control')
ctrlPressed = true;
});
$window.addEventListener('keyup', event => {
if (event.key == 'Control')
ctrlPressed = false;
});
2020-10-16 08:40:45 +00:00
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
const data = $scope.$eval($attrs.vnAnchor);
2020-10-16 08:40:45 +00:00
$element.on('click', event => {
2020-10-19 05:35:24 +00:00
if (ctrlPressed)
2020-10-21 08:26:28 +00:00
openNewTab($state, $window, event, data);
2020-10-19 05:35:24 +00:00
else
2020-10-20 05:43:36 +00:00
changeState($state, event, data);
2020-10-19 05:35:24 +00:00
});
2020-10-16 08:40:45 +00:00
2020-10-19 05:35:24 +00:00
$element.on('mousedown', event => {
2020-10-21 08:26:28 +00:00
const mouseWheel = 1;
if (event.button == mouseWheel)
openNewTab($state, $window, event, data);
2020-10-16 08:40:45 +00:00
});
}
};
}
2020-10-19 05:35:24 +00:00
directive.$inject = ['$state', '$window'];
2020-10-16 08:40:45 +00:00
ngModule.directive('vnAnchor', directive);