Anchor directive functionality
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2020-10-19 07:35:24 +02:00
parent 6412f29685
commit cf8ed6f8d3
2 changed files with 69 additions and 10 deletions

View File

@ -7,26 +7,63 @@ import ngModule from '../module';
* @param {Object} $state
* @return {Object} The directive
*/
export function directive($parse, $state) {
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;
});
function changeState(event, state) {
const params = stringifyParams(state);
$state.go(state.url, params);
event.preventDefault();
event.stopPropagation();
}
function newTab(event, state) {
const params = stringifyParams(state);
const url = $state.href(state.url, params);
$window.open(url);
event.preventDefault();
event.stopPropagation();
}
function stringifyParams(state) {
const params = Object.assign({}, state.params);
for (let param in params)
params[param] = JSON.stringify(params[param]);
return params;
}
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
const state = $scope.$eval($attrs.vnAnchor);
// const element = $element[0];
$element.on('click', event => {
const params = [];
if (ctrlPressed)
newTab(event, state);
else
changeState(event, state);
});
for (let param in state.params)
console.log(param);
// $state.go(state.url, state.params);
event.preventDefault();
event.stopPropagation();
$element.on('mousedown', event => {
if (event.button == 1)
newTab(event, state);
});
}
};
}
directive.$inject = ['$parse', '$state'];
directive.$inject = ['$state', '$window'];
ngModule.directive('vnAnchor', directive);

View File

@ -0,0 +1,22 @@
fdescribe('Directive vnAnchor', () => {
let $scope;
let $element;
let compile;
beforeEach(ngModule('vnCore'));
compile = _element => {
inject(($compile, $rootScope) => {
$scope = $rootScope.$new();
$element = angular.element(_element);
$compile($element)($scope);
$scope.$digest();
});
};
it(`should throw an error when there's no id defined`, () => {
let html = `<vn-icon-button vn-id=""></vn-icon-button>`;
compile(html);
});
});