This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
drupalcampbristol/web/modules/contrib/webform/js/webform.element.toggle.js
2017-03-16 15:29:07 +00:00

57 lines
1.6 KiB
JavaScript

/**
* @file
* Javascript behaviors for toggle integration.
*/
(function ($, Drupal) {
'use strict';
/**
* Initialize toggle element using Toggles.
*
* @type {Drupal~behavior}
*/
Drupal.behaviors.webformToggle = {
attach: function (context) {
$(context).find('.js-webform-toggle').once('webform-toggle').each(function () {
var $toggle = $(this);
var $wrapper = $toggle.parent();
var $checkbox = $wrapper.find('input[type="checkbox"]');
var $label = $wrapper.find('label');
$toggle.toggles({
checkbox: $checkbox,
on: $checkbox.is(':checked'),
clicker: $label,
text: {
on: $toggle.attr('data-toggle-text-on') || '',
off: $toggle.attr('data-toggle-text-off') || ''
}
});
// If checkbox is disabled then add the .disabled class to the toggle.
if ($checkbox.attr('disabled') || $checkbox.attr('readonly')) {
$toggle.addClass('disabled');
}
// Add .clearfix to the wrapper.
$wrapper.addClass('clearfix')
});
}
};
// Track the disabling of a toggle's checkbox using states.
$(document).on('state:disabled', function (event) {
$('.js-webform-toggle').each(function () {
var $toggle = $(this);
var $wrapper = $toggle.parent();
var $checkbox = $wrapper.find('input[type="checkbox"]');
var isDisabled = ($checkbox.attr('disabled') || $checkbox.attr('readonly'));
(isDisabled) ? $toggle.addClass('disabled') : $toggle.removeClass('disabled');
});
});
})(jQuery, Drupal);