X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fpermissions_by_term%2Fjs%2Fsrc%2Fclient%2Fdom-client.prototype.js;fp=web%2Fmodules%2Fcontrib%2Fpermissions_by_term%2Fjs%2Fsrc%2Fclient%2Fdom-client.prototype.js;h=26d6bdcfd919f638b9c13937fcfbdd1c522da76b;hp=0000000000000000000000000000000000000000;hb=059867c3f96750652c80f39e44c442a58c2549ee;hpb=f8fc16ae6b862bef59baaad5d051dd37b7ff11b2 diff --git a/web/modules/contrib/permissions_by_term/js/src/client/dom-client.prototype.js b/web/modules/contrib/permissions_by_term/js/src/client/dom-client.prototype.js new file mode 100644 index 000000000..26d6bdcfd --- /dev/null +++ b/web/modules/contrib/permissions_by_term/js/src/client/dom-client.prototype.js @@ -0,0 +1,142 @@ +const _ = require('lodash'); + +/** + * @param PermissionsOutput permissionsOutput + * @param document + */ +let DomClient = function(document, permissionsOutput, drupal) { + this.document = document; + this.drupal = drupal; + this.permissionsOutput = permissionsOutput; +} + +DomClient.prototype.renderPermissionsInfo = function() { + + let allowedUsersHtml = '' + this.drupal.t('Allowed users:') + ' '; + + if (!_.isEmpty(this.permissionsOutput.getUsernames())) { + allowedUsersHtml += this.permissionsOutput.getUsernames().join(', '); + } else { + allowedUsersHtml += '' + this.drupal.t('No user restrictions.') + ''; + } + + let allowedRolesHtml = '' + this.drupal.t('Allowed roles:') + ' '; + + if (!_.isEmpty(this.permissionsOutput.getRoles())) { + allowedRolesHtml += this.permissionsOutput.getRoles().join(', '); + } else { + allowedRolesHtml += '' + this.drupal.t('No role restrictions.') + '';; + } + + let generalInfoText = this.drupal.t("This widget shows information about taxonomy term related permissions. It's being updated, as soon you make any related changes in the form."); + + let newTermInfo = this.document.createElement('div'); + newTermInfo.innerHTML = '
' + generalInfoText + '

' + allowedUsersHtml + '
' + allowedRolesHtml + '
'; + this.document.querySelector('#edit-permissions-by-term-info .form-type-item').replaceWith(newTermInfo); + +} + +DomClient.prototype._computeTidsByAutocomplete = function(fieldWrapperCSSClass) { + let selectedTids = [] + + let autocompleteInputs = this.document.querySelectorAll(fieldWrapperCSSClass + ' input.form-autocomplete'); + + for (let autocompleteInput of autocompleteInputs) { + + if (autocompleteInput.value !== undefined && _.includes(autocompleteInput.value, '(') && _.includes(autocompleteInput.value, ')')) { + + let tidsInBrackets = autocompleteInput.value.match(/\(\d+\)/g); + + if (tidsInBrackets !== undefined && tidsInBrackets !== null && tidsInBrackets.constructor === Array) { + + for (let i = 0; i < tidsInBrackets.length; ++i) { + let selectedTid = parseInt(tidsInBrackets[i].replace('(', '').replace(')', '')); + if (!_.includes(selectedTids, selectedTid)) { + selectedTids.push(String(selectedTid)); + } + } + + } + + } + + } + + return selectedTids; +} + +DomClient.prototype._computeTidsBySelect = function(fieldWrapperCSSClass) { + let tids = [], + inputTypes = ['select', 'input']; + + for (let inputTypesIndex = 0; inputTypesIndex <= inputTypes.length; inputTypesIndex++) { + let value = this.document.querySelector(fieldWrapperCSSClass + ' select').value; + + if (typeof value === "string") { + tids.push(value); + } else { + throw "Value must be type of string."; + } + + } + + return tids; +} + +DomClient.prototype._computeTidsByCheckbox = function(formElementCssClass) { + let tids = []; + + for (let checkbox of this.document.querySelectorAll(formElementCssClass + ' input[type="checkbox"]')) { + if (checkbox.checked === true) { + tids.push(checkbox.value); + } + } + + return tids; +} + +DomClient.prototype.computeTids = function(formElementCssClass) { + let tids = []; + + let lookup = { + checkbox: '_computeTidsByCheckbox', + text: '_computeTidsByAutocomplete', + select: '_computeTidsBySelect', + }; + + let inputType = this._getInputType(formElementCssClass); + + tids.push(this[lookup[inputType]](formElementCssClass)); + + return tids; +} + +DomClient.prototype._getInputType = function(formElementCssClass) { + let formElement = null; + + if (!_.isEmpty(this.document.querySelector(formElementCssClass + ' select'))) { + formElement = 'select'; + } + + if (!_.isEmpty(this.document.querySelector(formElementCssClass + ' input'))) { + formElement = 'input'; + } + + if (formElement === 'input') { + if (_.get(this.document.querySelector(formElementCssClass + ' input.form-autocomplete'), 'type') && this.document.querySelector(formElementCssClass + ' input.form-autocomplete').type === "text") { + return 'text'; + } + if (this.document.querySelector(formElementCssClass + ' input').type === "checkbox") { + return 'checkbox'; + } + if (this.document.querySelector(formElementCssClass + ' input').type === "radio") { + return 'radio'; + } + } + if (!_.isEmpty(formElement) && this.document.querySelector(formElementCssClass + ' select').tagName === "SELECT") { + return 'select'; + } + +} + +export default DomClient; \ No newline at end of file