forked from ezsystems/PlatformUIBundle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ezsystems#290 from StephaneDiot/EZP-24613_Edit_ISB…
…N_fields EZP-24613 As an editor, I want to be able to edit the ISBN fields
- Loading branch information
Showing
7 changed files
with
538 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (C) eZ Systems AS. All rights reserved. | ||
* For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
|
||
.ez-isbn-input-ui { | ||
width: 100%; | ||
display: inline-block; | ||
vertical-align: bottom; | ||
} | ||
|
||
.ez-isbn-input-ui input { | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright (C) eZ Systems AS. All rights reserved. | ||
* For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
YUI.add('ez-isbn-editview', function (Y) { | ||
"use strict"; | ||
/** | ||
* Provides the field edit view for the ISBN field | ||
* | ||
* @module ez-isbn-editview | ||
*/ | ||
Y.namespace('eZ'); | ||
|
||
var FIELDTYPE_IDENTIFIER = 'ezisbn'; | ||
|
||
/** | ||
* ISBN edit view | ||
* | ||
* @namespace eZ | ||
* @class ISBNEditView | ||
* @constructor | ||
* @extends eZ.FieldEditView | ||
*/ | ||
Y.eZ.ISBNEditView = Y.Base.create('isbnEditView', Y.eZ.FieldEditView, [], { | ||
|
||
events: { | ||
'.ez-isbn-input-ui input': { | ||
'blur': 'validate', | ||
'valuechange': 'validate', | ||
}, | ||
}, | ||
|
||
/** | ||
* Validates the current input of the ISBN field | ||
* | ||
* @method validate | ||
*/ | ||
validate: function () { | ||
this.set('errorStatus', false); | ||
if ( this.get('fieldDefinition').isRequired && this._isFieldEmpty() ){ | ||
this.set('errorStatus', 'This field is required'); | ||
} else if ( !this._isFieldEmpty() ) { | ||
if ( this.get('fieldDefinition').fieldSettings.isISBN13 ) { | ||
this._checkISBN13(); | ||
} else { | ||
this._checkISBN10(); | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Get the raw field value (without dashes) | ||
* | ||
* @method _getRawFieldValue | ||
* @protected | ||
* @return {String} | ||
*/ | ||
_getRawFieldValue: function () { | ||
return this._getFieldValue().replace(/-/g, ""); | ||
}, | ||
|
||
/** | ||
* Checks whether the field is empty | ||
* | ||
* @method _isFieldEmpty | ||
* @protected | ||
* @return {Boolean} | ||
*/ | ||
_isFieldEmpty: function () { | ||
return (this._getRawFieldValue().length === 0); | ||
}, | ||
|
||
/** | ||
* Defines the variables to imported in the field edit template for ISBN | ||
* | ||
* @protected | ||
* @method _variables | ||
* @return {Object} containing isRequired | ||
*/ | ||
_variables: function () { | ||
var def = this.get('fieldDefinition'); | ||
|
||
return { | ||
"isRequired": def.isRequired | ||
}; | ||
}, | ||
|
||
/** | ||
* Checks if the ISBN 13 is valid | ||
* | ||
* @protected | ||
* @method _checkISBN13 | ||
*/ | ||
_checkISBN13: function () { | ||
var rawInputString = this._getRawFieldValue(), | ||
checksum13 = 0, | ||
weight13 = 1, | ||
checkDigit; | ||
|
||
if ( /^(97[89][0-9]{10})$/.test(rawInputString)) { | ||
Y.Array.each(rawInputString, function (char) { | ||
checksum13 = checksum13 + weight13 * char; | ||
weight13 = (weight13 + 2) % 4; | ||
}); | ||
} else { | ||
this.set('errorStatus', "This is not a correct ISBN13 pattern"); | ||
} | ||
if (this.isValid()) { | ||
if ((checksum13 % 10) !== 0) { | ||
checkDigit = (10 - ((checksum13 - ((weight13 + 2) % 4) * rawInputString[12]) %10)) %10; | ||
this.set('errorStatus', "Bad checksum, last digit of ISBN 13 should be " + checkDigit ); | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Checks if the ISBN 10 is valid | ||
* | ||
* @protected | ||
* @method _checkISBN10 | ||
*/ | ||
_checkISBN10: function () { | ||
var rawInputString = this._getRawFieldValue(), | ||
sumResult = 0; | ||
|
||
if (/^([0-9]{9}[0-9X])$/.test(rawInputString)) { | ||
Y.Array.each(rawInputString, function (char, index) { | ||
if ((index === 9) && (char === "X")) { | ||
sumResult = sumResult + 100; | ||
} else { | ||
sumResult = sumResult + (index + 1) * char; | ||
} | ||
}); | ||
} else { | ||
this.set('errorStatus', 'invalid ISBN 10'); | ||
} | ||
if (this.get('errorStatus') === false && sumResult % 11 !== 0) { | ||
this.set('errorStatus', "invalid ISBN 10 (sum is not a multiple of 11"); | ||
} | ||
}, | ||
|
||
/** | ||
* Returns the currently filled ISBN | ||
* | ||
* @protected | ||
* @method _getFieldValue | ||
* @return {String} | ||
*/ | ||
_getFieldValue: function () { | ||
return this.get('container').one('.ez-isbn-input-ui input').get('value'); | ||
}, | ||
}); | ||
|
||
Y.eZ.FieldEditView.registerFieldEditView( | ||
FIELDTYPE_IDENTIFIER, Y.eZ.ISBNEditView | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<div class="pure-g ez-editfield-row"> | ||
<div class="pure-u ez-editfield-infos"> | ||
{{> ez_fieldinfo_tooltip }} | ||
<label for="ez-field-{{ content.contentId }}-{{ fieldDefinition.identifier }}"> | ||
<p class="ez-fielddefinition-name"> | ||
{{ fieldDefinition.names.[eng-GB] }}{{#if isRequired}}*{{/if}}: | ||
</p> | ||
<p class="ez-editfield-error-message"> </p> | ||
</label> | ||
</div> | ||
<div class="pure-u ez-editfield-input-area ez-default-error-style"> | ||
<div class="ez-editfield-input"><div class="ez-isbn-input-ui"> | ||
<input type="text" | ||
value="{{ field.fieldValue }}" | ||
class="ez-validated-input" | ||
id="ez-field-{{ content.contentId }}-{{ fieldDefinition.identifier }}" | ||
{{#if isRequired}}required{{/if}} | ||
></div></div> | ||
</div> | ||
</div> |
Oops, something went wrong.