-
Notifications
You must be signed in to change notification settings - Fork 44
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 #124 from hgiesenow/issue-123
Fixes #123
- Loading branch information
Showing
2 changed files
with
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const path = require('path'); | ||
|
||
module.exports = (eZConfig, eZConfigManager) => { | ||
eZConfigManager.add({ | ||
eZConfig, | ||
entryName: 'ezplatform-admin-ui-content-edit-parts-js', | ||
newItems: [path.resolve(__dirname, '../public/admin/js/fieldType/eztags.js')], | ||
}); | ||
}; |
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,74 @@ | ||
(function (global) { | ||
const SELECTOR_FIELD = '.ez-field-edit--eztags.ez-field-edit--required'; | ||
|
||
class EzTagsValidator extends global.eZ.BaseFieldValidator { | ||
/** | ||
* Validates the input | ||
* | ||
* @method validateInteger | ||
* @param {Event} event | ||
* @returns {Object} | ||
* @memberof EzIntegerValidator | ||
*/ | ||
validateEzTags(event) { | ||
const isEmpty = !event.target.value; | ||
const isError = isEmpty; | ||
const label = event.target.closest(SELECTOR_FIELD).querySelector('.ez-field-edit__label').innerHTML; | ||
const result = {isError}; | ||
if (isEmpty) { | ||
result.errorMessage = global.eZ.errors.emptyField.replace('{fieldName}', label); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
init() { | ||
global.eZ.BaseFieldValidator.prototype.init.call(this); | ||
this.eventsMap.forEach((eventConfig) => { | ||
// Workaround for missing change event on hidden input. | ||
// Watch hidden field for changes and re-validate it | ||
MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | ||
var trackChange = function (element) { | ||
var oldVal = ''; | ||
var observer = new MutationObserver(function (record, x) { | ||
var newVal = record[0].target.value; | ||
if (oldVal != newVal) { | ||
eventConfig.validateField({target: record[0].target}); | ||
oldVal = newVal; | ||
} | ||
}); | ||
observer.observe(element, { | ||
attributes: true | ||
}); | ||
} | ||
jQuery('.ez-field-edit--eztags input.tagids').each(function(index,element) { | ||
trackChange(element); | ||
}); | ||
}); | ||
|
||
} | ||
} | ||
|
||
const validator = new EzTagsValidator({ | ||
classInvalid: 'is-invalid', | ||
fieldSelector: SELECTOR_FIELD, | ||
eventsMap: [ | ||
{ | ||
selector: '.ez-field-edit--eztags input.tagids', | ||
eventName: 'change', | ||
callback: 'validateEzTags', | ||
errorNodeSelectors: ['.ez-field-edit__label-wrapper'], | ||
}, | ||
], | ||
}); | ||
|
||
if (jQuery(SELECTOR_FIELD).length) { | ||
validator.init(); | ||
} | ||
|
||
global.eZ.fieldTypeValidators = global.eZ.fieldTypeValidators ? | ||
[...global.eZ.fieldTypeValidators, validator] : | ||
[validator]; | ||
|
||
|
||
})(window); |