From be0763cb48d778c88e478612894a26ed898890e6 Mon Sep 17 00:00:00 2001 From: Bozana Bokan Date: Tue, 25 Apr 2023 17:45:19 +0200 Subject: [PATCH] pkp/pkp-lib#8940 changes after code review --- plugins/pubIds/urn/URNPubIdPlugin.php | 65 ++++------ plugins/pubIds/urn/js/FieldPubIdUrn.js | 28 ----- plugins/pubIds/urn/js/FieldTextUrn.js | 80 ------------ plugins/pubIds/urn/js/FieldUrn.js | 165 +++++++++++++++++++++++++ plugins/pubIds/urn/js/checkNumber.js | 76 ------------ 5 files changed, 191 insertions(+), 223 deletions(-) delete mode 100644 plugins/pubIds/urn/js/FieldPubIdUrn.js delete mode 100644 plugins/pubIds/urn/js/FieldTextUrn.js create mode 100644 plugins/pubIds/urn/js/FieldUrn.js delete mode 100644 plugins/pubIds/urn/js/checkNumber.js diff --git a/plugins/pubIds/urn/URNPubIdPlugin.php b/plugins/pubIds/urn/URNPubIdPlugin.php index d0c7f8b5155..c348ea1c925 100755 --- a/plugins/pubIds/urn/URNPubIdPlugin.php +++ b/plugins/pubIds/urn/URNPubIdPlugin.php @@ -404,7 +404,7 @@ public function addPublicationFormFields($hookName, $form) } // Load the checkNumber.js file that is required for this field - $this->addJavaScript(Application::get()->getRequest(), TemplateManager::getManager(Application::get()->getRequest())); + //$this->addJavaScript(Application::get()->getRequest(), TemplateManager::getManager(Application::get()->getRequest())); // If a pattern exists, use a DOI-like field to generate the URN if ($pattern) { @@ -536,45 +536,32 @@ public function loadUrnFieldComponent($hookName, $args) return; } - $context = $templateMgr->getTemplateVars('currentContext'); - $suffixType = $this->getSetting($context->getId(), 'urnSuffix'); - if ($suffixType === 'default' || $suffixType === 'pattern') { - $templateMgr->addJavaScript( - 'field-pub-id-urn-component', - Application::get()->getRequest()->getBaseUrl() . '/' . $this->getPluginPath() . '/js/FieldPubIdUrn.js', - [ - 'contexts' => 'backend', - 'priority' => TemplateManager::STYLE_SEQUENCE_LAST, - ] - ); - } else { - $templateMgr->addJavaScript( - 'field-text-urn-component', - Application::get()->getRequest()->getBaseUrl() . '/' . $this->getPluginPath() . '/js/FieldTextUrn.js', - [ - 'contexts' => 'backend', - 'priority' => TemplateManager::STYLE_SEQUENCE_LAST, - ] - ); - $templateMgr->addStyleSheet( - 'field-text-urn-component', - ' - .pkpFormField--urn__input { - display: inline-block; - } + $templateMgr->addJavaScript( + 'field-urn', + Application::get()->getRequest()->getBaseUrl() . '/' . $this->getPluginPath() . '/js/FieldUrn.js', + [ + 'contexts' => 'backend', + 'priority' => TemplateManager::STYLE_SEQUENCE_LAST, + ] + ); + $templateMgr->addStyleSheet( + 'field-urn', + ' + .pkpFormField--urn__input { + display: inline-block; + } - .pkpFormField--urn__button { - margin-left: 0.25rem; - height: 2.5rem; // Match input height - } - ', - [ - 'contexts' => 'backend', - 'inline' => true, - 'priority' => TemplateManager::STYLE_SEQUENCE_LAST, - ] - ); - } + .pkpFormField--urn__button { + margin-left: 0.25rem; + height: 2.5rem; // Match input height + } + ', + [ + 'contexts' => 'backend', + 'inline' => true, + 'priority' => TemplateManager::STYLE_SEQUENCE_LAST, + ] + ); } // diff --git a/plugins/pubIds/urn/js/FieldPubIdUrn.js b/plugins/pubIds/urn/js/FieldPubIdUrn.js deleted file mode 100644 index e3059b7d1a2..00000000000 --- a/plugins/pubIds/urn/js/FieldPubIdUrn.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @defgroup plugins_pubIds_urn_js - */ -/** - * @file plugins/pubIds/urn/js/FieldPubIdUrn.js - * - * Copyright (c) 2014-2021 Simon Fraser University - * Copyright (c) 2003-2021 John Willinsky - * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. - * - * @brief A Vue.js component for a pub ID field that considers check number. - */ - -pkp.Vue.component('field-pub-id-urn', { - name: 'FieldPubIdUrn', - extends: pkp.Vue.component('field-pub-id'), - props: { - applyCheckNumber: Boolean, - }, - methods: { - generateId() { - let id = pkp.Vue.component('field-pub-id').options.methods['generateId'].apply(this); - return this.applyCheckNumber - ? id + $.pkp.plugins.generic.urn.getCheckNumber(id, this.prefix) - : id; - } - }, -}); \ No newline at end of file diff --git a/plugins/pubIds/urn/js/FieldTextUrn.js b/plugins/pubIds/urn/js/FieldTextUrn.js deleted file mode 100644 index 5133af05372..00000000000 --- a/plugins/pubIds/urn/js/FieldTextUrn.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * @defgroup plugins_pubIds_urn_js - */ -/** - * @file plugins/pubIds/urn/js/FieldTextUrn.js - * - * Copyright (c) 2014-2021 Simon Fraser University - * Copyright (c) 2003-2021 John Willinsky - * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. - * - * @brief A Vue.js component for a URN text form field, used for custom suffixes, and that adds a check number. - */ -var template = pkp.Vue.compile('
' + -' ' + -' ' + -'
' + -' ' + -' ' + -' {{ addCheckNumberLabel }}' + -' ' + -' ' + -'
' + -'
' + -' '); - -pkp.Vue.component('field-text-urn', { - name: 'FieldTextUrn', - extends: pkp.Vue.component('field-text'), - props: { - addCheckNumberLabel: { - type: String, - required: true - }, - urnPrefix: { - type: String, - required: true - } - }, - methods: { - /** - * Add a check number to the end of the URN - */ - addCheckNumber() { - this.currentValue += $.pkp.plugins.generic.urn.getCheckNumber(this.currentValue, this.urnPrefix); - } - }, - render: function(h) { - return template.render.call(this, h); - } -}); \ No newline at end of file diff --git a/plugins/pubIds/urn/js/FieldUrn.js b/plugins/pubIds/urn/js/FieldUrn.js new file mode 100644 index 00000000000..e0c1193b431 --- /dev/null +++ b/plugins/pubIds/urn/js/FieldUrn.js @@ -0,0 +1,165 @@ +/** + * @defgroup plugins_pubIds_urn_js + */ +/** + * @file plugins/pubIds/urn/js/FieldUrn.js + * + * Copyright (c) 2014-2021 Simon Fraser University + * Copyright (c) 2003-2021 John Willinsky + * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. + * + * @brief Function for determining and adding the check number for URNs, a Vue.js component for URN custom suffix input text field, and a Vue.js component for URN suffix using a pattern. + */ + +/** Function for determining and adding the check number for URNs */ +(function($) { + + /** + * Add method to the pkp namespace + */ + $.pkp.plugins.generic.urn = { + + /** + * Get the last, check number. + * Algorithm (s. http://www.persistent-identifier.de/?link=316): + * every URN character is replaced with a number + * according to the conversion table, + * every number is multiplied by + * it's position/index (beginning with 1), + * the numbers' sum is calculated, + * the sum is divided by the last number, + * the last number of the quotient + * before the decimal point is the check number. + * + * @param {string} urn + * @param {string} urnPrefix + */ + getCheckNumber: function(urn, urnPrefix) { + var newURN = '', + conversionTable = { + '9': '41', '8': '9', '7': '8', '6': '7', + '5': '6', '4': '5', '3': '4', '2': '3', + '1': '2', '0': '1', 'a': '18', 'b': '14', + 'c': '19', 'd': '15', 'e': '16', 'f': '21', + 'g': '22', 'h': '23', 'i': '24', 'j': '25', + 'k': '42', 'l': '26', 'm': '27', 'n': '13', + 'o': '28', 'p': '29', 'q': '31', 'r': '12', + 's': '32', 't': '33', 'u': '11', 'v': '34', + 'w': '35', 'x': '36', 'y': '37', 'z': '38', + '-': '39', ':': '17', '_': '43', '/': '45', + '.': '47', '+': '49' + }, + i, j, char, sum, lastNumber, quot, quotRound, quotString, newSuffix; + + suffix = urn.replace(urnPrefix, '').toLowerCase(); + for (i = 0; i < suffix.length; i++) { + char = suffix.charAt(i); + newURN += conversionTable[char]; + } + sum = 0; + for (j = 1; j <= newURN.length; j++) { + sum = sum + (newURN.charAt(j - 1) * j); + } + lastNumber = newURN.charAt(newURN.length - 1); + quot = sum / lastNumber; + quotRound = Math.floor(quot); + quotString = quotRound.toString(); + return parseInt(quotString.charAt(quotString.length - 1)); + } + }; + + // Apply the check number when the button is clicked + $('#checkNo').on('click', () => { + var urnPrefix = $('[id^="urnPrefix"]').val(), urnSuffix = $('[id^="urnSuffix"]').val(); + urn = urnPrefix + urnSuffix; + $('[id^="urnSuffix"]').val(urnSuffix + $.pkp.plugins.generic.urn.getCheckNumber(urn, urnPrefix)); + }); + +}(jQuery)); + +/** Vue.js component for URN custom suffix input text field, and possibility to add check number */ +var template = pkp.Vue.compile('
' + +' ' + +' ' + +'
' + +' ' + +' ' + +' {{ addCheckNumberLabel }}' + +' ' + +' ' + +'
' + +'
' + +' '); + +pkp.Vue.component('field-text-urn', { + name: 'FieldTextUrn', + extends: pkp.Vue.component('field-text'), + props: { + addCheckNumberLabel: { + type: String, + required: true + }, + urnPrefix: { + type: String, + required: true + } + }, + methods: { + /** + * Add a check number to the end of the URN + */ + addCheckNumber() { + this.currentValue += $.pkp.plugins.generic.urn.getCheckNumber(this.currentValue, this.urnPrefix); + } + }, + render: function(h) { + return template.render.call(this, h); + } +}); + +/** Vue.js component for URN suffix that uses a pattern, considering also the check number */ +pkp.Vue.component('field-pub-id-urn', { + name: 'FieldPubIdUrn', + extends: pkp.Vue.component('field-pub-id'), + props: { + applyCheckNumber: Boolean, + }, + methods: { + generateId() { + var id = pkp.Vue.component('field-pub-id').options.methods['generateId'].apply(this); + return this.applyCheckNumber + ? id + $.pkp.plugins.generic.urn.getCheckNumber(id, this.prefix) + : id; + } + }, +}); \ No newline at end of file diff --git a/plugins/pubIds/urn/js/checkNumber.js b/plugins/pubIds/urn/js/checkNumber.js deleted file mode 100644 index deef62d2c07..00000000000 --- a/plugins/pubIds/urn/js/checkNumber.js +++ /dev/null @@ -1,76 +0,0 @@ -/** - * @defgroup plugins_pubIds_urn_js - */ -/** - * @file plugins/pubIds/urn/js/checkNumber.js - * - * Copyright (c) 2014-2021 Simon Fraser University - * Copyright (c) 2003-2021 John Willinsky - * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. - * - * @brief Function for determining and adding the check number for URNs - */ -(function($) { - - /** - * Add method to the pkp namespace - */ - $.pkp.plugins.generic.urn = { - - /** - * Get the last, check number. - * Algorithm (s. http://www.persistent-identifier.de/?link=316): - * every URN character is replaced with a number - * according to the conversion table, - * every number is multiplied by - * it's position/index (beginning with 1), - * the numbers' sum is calculated, - * the sum is divided by the last number, - * the last number of the quotient - * before the decimal point is the check number. - * - * @param {string} urn - * @param {string} urnPrefix - */ - getCheckNumber: function(urn, urnPrefix) { - var newURN = '', - conversionTable = { - '9': '41', '8': '9', '7': '8', '6': '7', - '5': '6', '4': '5', '3': '4', '2': '3', - '1': '2', '0': '1', 'a': '18', 'b': '14', - 'c': '19', 'd': '15', 'e': '16', 'f': '21', - 'g': '22', 'h': '23', 'i': '24', 'j': '25', - 'k': '42', 'l': '26', 'm': '27', 'n': '13', - 'o': '28', 'p': '29', 'q': '31', 'r': '12', - 's': '32', 't': '33', 'u': '11', 'v': '34', - 'w': '35', 'x': '36', 'y': '37', 'z': '38', - '-': '39', ':': '17', '_': '43', '/': '45', - '.': '47', '+': '49' - }, - i, j, char, sum, lastNumber, quot, quotRound, quotString, newSuffix; - - suffix = urn.replace(urnPrefix, '').toLowerCase(); - for (i = 0; i < suffix.length; i++) { - char = suffix.charAt(i); - newURN += conversionTable[char]; - } - sum = 0; - for (j = 1; j <= newURN.length; j++) { - sum = sum + (newURN.charAt(j - 1) * j); - } - lastNumber = newURN.charAt(newURN.length - 1); - quot = sum / lastNumber; - quotRound = Math.floor(quot); - quotString = quotRound.toString(); - return parseInt(quotString.charAt(quotString.length - 1)); - } - }; - - // Apply the check number when the button is clicked - $('#checkNo').on('click', () => { - var urnPrefix = $('[id^="urnPrefix"]').val(), urnSuffix = $('[id^="urnSuffix"]').val(); - urn = urnPrefix + urnSuffix; - $('[id^="urnSuffix"]').val(urnSuffix + $.pkp.plugins.generic.urn.getCheckNumber(urn, urnPrefix)); - }); - -}(jQuery));