Commit c13c50ad authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

Issue #1761510: Doesn't work with CKEditor

parent ec814bb0
(function (Drupal, $, CKEDITOR) {
Drupal.behaviors.cvCKEDITOR = {
attach: function () {
$(document).bind('clientsideValidationAlterOptions', function (e, options, form_id) {
if (!Drupal.settings.clientsideValidation.forms[form_id].includeHidden) {
// Do not validate hidden fields. Fix ckeditor instances.
$('#' + form_id).find('textarea.ckeditor-processed').each(function () {
var $textarea = $(this);
var id = $textarea.attr('id');
if (CKEDITOR.instances.hasOwnProperty(id)) {
// The ckeditor instance has already been initialised, check if
// the instance is hidden.
if (CKEDITOR.instances[id].hasOwnProperty('container')) {
if (!$(CKEDITOR.instances[id].container.$).is(':hidden')) {
options.ignore = fixIgnore(options.ignore, id);
}
}
// The ckeditor instance has not yet been initialised. Check if
// the textarea itself is hidden.
else if (!$textarea.is(':hidden')) {
options.ignore = fixIgnore(options.ignore, id);
}
}
});
}
});
var fixIgnore = function (ignore, id) {
var ignores = ignore.split(',');
for (var i = 0; i < ignores.length; i++) {
ignores[i] += ':not(#' + id + ')';
}
return ignores.join(',');
};
var debounce = function (func, wait, immediate) {
var timeout;
return function () {
var context = this;
var args = arguments;
var later = function () {
timeout = null;
if (!immediate)
func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow)
func.apply(context, args);
};
};
var updateText = function (instance) {
return debounce(function () {
instance.updateElement();
}, 250);
};
CKEDITOR.on('instanceReady', function () {
for (var instance in CKEDITOR.instances) {
if (CKEDITOR.instances.hasOwnProperty(instance)) {
CKEDITOR.instances[instance].document.on("keyup", updateText(CKEDITOR.instances[instance]));
CKEDITOR.instances[instance].document.on("paste", updateText(CKEDITOR.instances[instance]));
CKEDITOR.instances[instance].document.on("keypress", updateText(CKEDITOR.instances[instance]));
CKEDITOR.instances[instance].document.on("blur", updateText(CKEDITOR.instances[instance]));
CKEDITOR.instances[instance].document.on("change", updateText(CKEDITOR.instances[instance]));
}
}
});
}
};
})(Drupal, jQuery, CKEDITOR);
......@@ -701,6 +701,13 @@
};
}
}
/**
* Let other modules alter the validation options for this form.
* @event clientsideValidationAlterOptions
* @name clientsideValidationAlterOptions
* @memberof Drupal.clientsideValidation
*/
$.event.trigger('clientsideValidationAlterOptions', [validate_options, f]);
self.validators[f] = $('#' + f).validate(validate_options);
// Disable HTML5 validation
......
......@@ -23,14 +23,25 @@ define('CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION', 7);
* Implements hook_js_alter().
*/
function clientside_validation_js_alter(&$javascript) {
$clientside_validation_settings = &drupal_static('clientside_validation_settings', array());
// Provide conversion table between translated month names, both full and abbreviated, and their number.
foreach (range(1, 12) as $month_number) {
$date = mktime(12, 0, 0, $month_number, 1, 1970);
$clientside_validation_settings['clientsideValidation']['general']['months'][t(date('F', $date))] = $month_number;
$clientside_validation_settings['clientsideValidation']['general']['months'][t(date('M', $date))] = $month_number;
$path = drupal_get_path('module', 'clientside_validation') ;
if (isset($javascript[$path. '/clientside_validation.js'])) {
$clientside_validation_settings = &drupal_static('clientside_validation_settings', array());
// Provide conversion table between translated month names, both full and abbreviated, and their number.
foreach (range(1, 12) as $month_number) {
$date = mktime(12, 0, 0, $month_number, 1, 1970);
$clientside_validation_settings['clientsideValidation']['general']['months'][t(date('F', $date))] = $month_number;
$clientside_validation_settings['clientsideValidation']['general']['months'][t(date('M', $date))] = $month_number;
}
$javascript['settings']['data'][] = $clientside_validation_settings;
$scripts = array_keys($javascript);
foreach ($scripts as $script) {
if (strpos($script, 'ckeditor') !== FALSE) {
$javascript[$path . '/clientside_validation.ckeditor.js'] = $javascript[$path. '/clientside_validation.js'];
$javascript[$path . '/clientside_validation.ckeditor.js']['weight'] -= 0.001;
$javascript[$path . '/clientside_validation.ckeditor.js']['data'] = $path . '/clientside_validation.ckeditor.js';
}
}
}
$javascript['settings']['data'][] = $clientside_validation_settings;
}
function clientside_validation_ajax_render_alter(&$commands) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment