import $ from 'jquery' import XE from 'xe' // TODO:: mouseover, const Keys = { ENTER: 13, TAB: 9, BACKSPACE: 8, UP_ARROW: 38, DOWN_ARROW: 40, ESCAPE: 27 } class Permission { constructor ({ $wrapper, key, userSearchUrl, groupSearchUrl, permission, type, vgroupAll }) { this.$wrapper = $wrapper this.key = key this.userSearchUrl = userSearchUrl this.groupSearchUrl = groupSearchUrl this.permission = permission this.type = type this.vgroupAll = vgroupAll this.query = '' this.suggestion = [] this.placeholder = XE.Lang.trans('xe::explainIncludeUserOrGroup') this.selectedIndex = '' this.includeSelectedIndex = -1 this.excludeSelectedIndex = -1 this.MIN_QUERY_LENGTH = 2 } bindEvents () { var _this = this this.$wrapper.on('change', '.chkModeAble', function (e) { var $target = $(e.target) var checked = $target.is(':checked') if (checked) { _this.$wrapper.find('input:not(.chkModeAble)').prop('disabled', true) } else { _this.$wrapper.find('input:not(.chkModeAble)').prop('disabled', false) } }) this.$wrapper.on('keydown', '.inputUserGroup', function (e) { var query = e.target.value.trim() var $this = $(this) var keyCode = e.keyCode var $ul = $this.parent().find('.ReactTags__suggestions ul') var dataInput = $this.data('input') // include, exclude if (query.length >= _this.MIN_QUERY_LENGTH) { if ($ul.length > 0) { var index = parseInt($this.data('index'), 10) var focusedIndex = 0 switch (keyCode) { case Keys.UP_ARROW : if (index == 0) { focusedIndex = $ul.find('li').length - 1 } else { focusedIndex = (index - 1) } $this.data('index', focusedIndex) $ul.find('li').eq(focusedIndex).addClass('active').siblings().removeClass('active') break case Keys.DOWN_ARROW : if (index == $ul.find('li').length - 1) { focusedIndex = 0 } else { focusedIndex = index + 1 } $this.data('index', focusedIndex) $ul.find('li').eq(focusedIndex).addClass('active').siblings().removeClass('active') break case Keys.ENTER : case Keys.TAB : e.preventDefault() if ($ul.find('li.active').length > 0) { var tag = $ul.find('li.active').data('tag') var name = '' var pType = '' var prefix = '' // user if ($ul.data('target') === 'user') { // include if (dataInput === 'include') { name = _this.type + 'User' pType = 'user' prefix = '@' // exclude } else { name = _this.type + 'Except' pType = 'except' prefix = '@' } // group } else { name = _this.type + 'Group' pType = 'group' prefix = '%' } var pTypes = _this.permission[pType] var bSameWord = false if (pTypes.length > 0) { pTypes.forEach(function (type, i) { if (type.id === tag.id) { bSameWord = true } }) if (!bSameWord) { _this.permission[pType].push(tag) } } else { _this.permission[pType].push(tag) } var ids = _this.permission[pType].map(function (tag) { return tag.id }) if (!bSameWord) { $ul.closest('.ReactTags__tags').find('[name=' + name + ']').val(ids.join().trim()) $ul.closest('.ReactTags__tags').find('.' + pType + 'Wrap') .append(`${prefix + (tag.display_name || tag.name)}x`) } $ul.remove() $this.val('').data('index', -1).focus() } e.preventDefault() // prevent tab break case Keys.ESCAPE : _this[dataInput + 'SelectedIndex'] = 0 $ul.parent().empty() $this.focus() break } } } else { if (Keys.BACKSPACE === keyCode) { var $tag = $this.closest('.ReactTags__tags').find('.ReactTags__selected span') if (!query && $tag.length > 0) { _this.removeTag($tag.eq($tag.length - 1)) } } } }) this.$wrapper.find('.ReactTags__suggestions').on('mouseenter', 'li', function () { var $this = $(this) $this.addClass('active').siblings().removeClass('active') }) this.$wrapper.find('.ReactTags__suggestions').on('click', 'li', function () { var $this = $(this) var tag = $this.data('tag') var $ul = $this.closest('ul') var $input = $this.closest('.ReactTags__tagInput').find('input:text') var dataInput = $input.data('input') var name = '' var pType = '' var prefix = '' if ($ul.data('target') === 'user') { // include if (dataInput === 'include') { name = _this.type + 'User' pType = 'user' prefix = '@' // exclude } else { name = _this.type + 'Except' pType = 'except' prefix = '@' } // group } else { name = _this.type + 'Group' pType = 'group' prefix = '%' } var pTypes = _this.permission[pType] var bSameWord = false if (pTypes.length > 0) { pTypes.forEach(function (type, i) { if (type.id === tag.id) { bSameWord = true } }) if (!bSameWord) { _this.permission[pType].push(tag) } } else { _this.permission[pType].push(tag) } var ids = _this.permission[pType].map(function (tag) { return tag.id }) if (!bSameWord) { $ul.closest('.ReactTags__tags').find('[name=' + name + ']').val(ids.join().trim()) $ul.closest('.ReactTags__tags').find('.' + pType + 'Wrap') .append(`${prefix + (tag.display_name || tag.name)}x`) } $ul.remove() $input.val('').data('index', -1).focus() }) this.$wrapper.on('keyup', '.inputUserGroup', function (e) { var query = e.target.value.trim() var $this = $(this) var keyCode = e.keyCode if (query.length >= _this.MIN_QUERY_LENGTH) { if ([Keys.ENTER, Keys.TAB, Keys.UP_ARROW, Keys.DOWN_ARROW, Keys.ESCAPE, 37, 39].indexOf(keyCode) == -1) { var temp = '' temp += `