| 1 |
if (typeof ToggleButton !== "function") { |
| 2 |
/** |
| 3 |
* Create the Toggle Button |
| 4 |
* @param dom |
| 5 |
* @constructor |
| 6 |
*/ |
| 7 |
function ToggleButton(dom, id) { |
| 8 |
this.dom = dom; |
| 9 |
|
| 10 |
this.setId = function(id){ |
| 11 |
this.id = 'f12-toggle-id-'+id; |
| 12 |
this.id = this.id.replace(new RegExp('\\[', 'g'), ''); |
| 13 |
this.id = this.id.replace(new RegExp('\\]', 'g'), ''); |
| 14 |
} |
| 15 |
|
| 16 |
this.setId(id); |
| 17 |
|
| 18 |
this.init = function () { |
| 19 |
var name = this.dom.attr('name'); |
| 20 |
|
| 21 |
var before = this.dom.attr('data-before'); |
| 22 |
var after = this.dom.attr('data-after'); |
| 23 |
|
| 24 |
this.dom.wrap('<div class="f12-toggle"></div>'); |
| 25 |
this.dom.parent().append('<button type="button" id="' + this.id + '" class="btn btn-toggle" data-before="' + before + '" data-after="' + after + '" data-switch-target="' + name + '" aria-pressed="false"><span class="handle"></span></button>'); |
| 26 |
|
| 27 |
if (parseInt(this.dom.val()) === 1) { |
| 28 |
this.updateStatus(1); |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
this.updateStatus = function (value) { |
| 33 |
this.dom.val(value); |
| 34 |
this.dom.attr('value', value); |
| 35 |
|
| 36 |
// Change button status |
| 37 |
if (value === 1) { |
| 38 |
this.dom.parent().find('button').addClass('active'); |
| 39 |
this.dom.parent().find('button').attr('aria-pressed', 'true'); |
| 40 |
} else { |
| 41 |
this.dom.parent().find('button').removeClass('active'); |
| 42 |
this.dom.parent().find('button').attr('aria-pressed', 'false') |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
this.onClick = function (target) { |
| 47 |
var targetObject = 'input[name="' + target + '"]'; |
| 48 |
var value = parseInt(jQuery(document).find(targetObject).val()); |
| 49 |
|
| 50 |
if (value === 1) { |
| 51 |
value = 0; |
| 52 |
} else { |
| 53 |
value = 1; |
| 54 |
} |
| 55 |
|
| 56 |
this.updateStatus(value); |
| 57 |
} |
| 58 |
|
| 59 |
this.addEventHandler = function () { |
| 60 |
var c = this; |
| 61 |
jQuery(document).on('click', '#' + this.id, function () { |
| 62 |
c.onClick(jQuery(this).attr('data-switch-target')); |
| 63 |
}); |
| 64 |
|
| 65 |
// Handle clicks on labels within the toggle wrapper |
| 66 |
var $wrapper = this.dom.closest('.f12-checkbox-toggle'); |
| 67 |
if ($wrapper.length) { |
| 68 |
$wrapper.on('click', function (e) { |
| 69 |
// Skip if clicking the toggle button itself |
| 70 |
if (jQuery(e.target).closest('.btn-toggle').length) { |
| 71 |
return; |
| 72 |
} |
| 73 |
e.preventDefault(); |
| 74 |
c.onClick(c.dom.attr('name')); |
| 75 |
}); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
this.init(); |
| 80 |
this.addEventHandler(); |
| 81 |
} |
| 82 |
|
| 83 |
jQuery.fn.f12Toggle = function () { |
| 84 |
|
| 85 |
this.each(function () { |
| 86 |
var name = jQuery(this).attr('name'); |
| 87 |
new ToggleButton(jQuery(this), name); |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
jQuery(document).ready(function () { |
| 92 |
jQuery('input[type="hidden"].toggle').f12Toggle(); |
| 93 |
}); |
| 94 |
} |