Skip to content

Commit

Permalink
Merge pull request ezsystems#290 from StephaneDiot/EZP-24613_Edit_ISB…
Browse files Browse the repository at this point in the history
…N_fields

EZP-24613 As an editor, I want to be able to edit the ISBN fields
  • Loading branch information
StephaneDiot committed Aug 27, 2015
2 parents 9fb2a7a + 8742f04 commit 6757d97
Show file tree
Hide file tree
Showing 7 changed files with 538 additions and 0 deletions.
1 change: 1 addition & 0 deletions Resources/config/css.yml
Expand Up @@ -73,6 +73,7 @@ system:
- 'bundles/ezplatformui/css/views/fields/edit/relation.css'
- 'bundles/ezplatformui/css/views/fields/edit/relationlist.css'
- 'bundles/ezplatformui/css/views/fields/edit/richtext.css'
- 'bundles/ezplatformui/css/views/fields/edit/isbn.css'
- 'bundles/ezplatformui/css/modules/tabs.css'
- 'bundles/ezplatformui/css/modules/page-header.css'
- 'bundles/ezplatformui/css/modules/serverside-content.css'
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/yui.yml
Expand Up @@ -324,6 +324,7 @@ system:
- 'ez-integer-editview'
- 'ez-selection-editview'
- 'ez-relation-editview'
- 'ez-isbn-editview'
- 'ez-relationlist-editview'
- 'event-tap'
- 'contenteditformview-ez-template'
Expand Down Expand Up @@ -361,6 +362,12 @@ system:
dateandtimeview-ez-template:
type: 'template'
path: %ez_platformui.public_dir%/templates/fields/view/dateandtime.hbt
ez-isbn-editview:
requires: ['ez-fieldeditview', 'isbneditview-ez-template']
path: %ez_platformui.public_dir%/js/views/fields/ez-isbn-editview.js
isbneditview-ez-template:
type: 'template'
path: %ez_platformui.public_dir%/templates/fields/edit/isbn.hbt
ez-time-editview:
requires: ['ez-fieldeditview', 'event-valuechange', 'timeeditview-ez-template', 'datatype-date-format', 'datatype-date']
path: %ez_platformui.public_dir%/js/views/fields/ez-time-editview.js
Expand Down
14 changes: 14 additions & 0 deletions Resources/public/css/views/fields/edit/isbn.css
@@ -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%;
}
157 changes: 157 additions & 0 deletions Resources/public/js/views/fields/ez-isbn-editview.js
@@ -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
);
});
20 changes: 20 additions & 0 deletions Resources/public/templates/fields/edit/isbn.hbt
@@ -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">&nbsp;</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>

0 comments on commit 6757d97

Please sign in to comment.