Skip to content

Commit

Permalink
pkp/pkp-lib#8940 changes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
bozana committed Apr 25, 2023
1 parent d6f8f96 commit be0763c
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 223 deletions.
65 changes: 26 additions & 39 deletions plugins/pubIds/urn/URNPubIdPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
]
);
}

//
Expand Down
28 changes: 0 additions & 28 deletions plugins/pubIds/urn/js/FieldPubIdUrn.js

This file was deleted.

80 changes: 0 additions & 80 deletions plugins/pubIds/urn/js/FieldTextUrn.js

This file was deleted.

165 changes: 165 additions & 0 deletions plugins/pubIds/urn/js/FieldUrn.js
Original file line number Diff line number Diff line change
@@ -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('<div class="pkpFormField pkpFormField--text pkpFormField--urn" :class="classes">' +
' <form-field-label' +
' :controlId="controlId"' +
' :label="label"' +
' :localeLabel="localeLabel"' +
' :isRequired="isRequired"' +
' :requiredLabel="__(\'common.required\')"' +
' :multilingualLabel="multilingualLabel"' +
' />' +
' <div' +
' v-if="isPrimaryLocale && description"' +
' class="pkpFormField__description"' +
' v-html="description"' +
' :id="describedByDescriptionId"' +
' />' +
' <div class="pkpFormField__control" :class="controlClasses">' +
' <input' +
' class="pkpFormField__input pkpFormField--text__input pkpFormField--urn__input"' +
' ref="input"' +
' v-model="currentValue"' +
' :type="inputType"' +
' :id="controlId"' +
' :name="localizedName"' +
' :aria-describedby="describedByIds"' +
' :aria-invalid="!!errors.length"' +
' :required="isRequired"' +
' :style="inputStyles"' +
' />' +
' <button' +
' class="pkpButton pkpFormField--urn__button"' +
' @click.prevent="addCheckNumber"' +
' >' +
' {{ addCheckNumberLabel }}' +
' </button>' +
' <field-error' +
' v-if="errors.length"' +
' :id="describedByErrorId"' +
' :messages="errors"' +
' />' +
' </div>' +
' </div>' +
' </div>');

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;
}
},
});

0 comments on commit be0763c

Please sign in to comment.