Files
AVSDevR.ShinyInputs/inst/js/togglableTextInput.js

101 lines
3.1 KiB
JavaScript

// switch input binding
var togglableTextInputBinding = new Shiny.InputBinding();
$.extend(togglableTextInputBinding, {
find: function(scope) {
return $(scope).find('.shiny-input-togglableText');
},
getId: function(el) {
return $(el).attr('id');
},
getValue: function(el) {
return {
checked: $('#' + Shiny.$escape(el.id) + 'Check').prop('checked'),
text: $('#' + Shiny.$escape(el.id) + 'Text').val()
};
},
setValue: function(el, value) {
var $checkEl =$('#' + Shiny.$escape(el.id) + 'Check')
var $textEl = $('#' + Shiny.$escape(el.id) + 'Text');
$checkEl.prop('checked', value.checked);
Shiny.setInputValue(Shiny.$escape(el.id) + 'Check', value.checked);
if ($('#' + Shiny.$escape(el.id)).attr('aria-enable-on-check') == true) {
if (value.checked) {
$textEl.val(value.text);
Shiny.setInputValue(Shiny.$escape(el.id) + 'Text', value.text);
$textEl.prop('disabled', false);
} else {
$textEl.val(null);
Shiny.setInputValue(Shiny.$escape(el.id) + 'Text', null);
$textEl.prop('disabled', true);
}
} else {
if (value.checked) {
$textEl.val(null);
Shiny.setInputValue(Shiny.$escape(el.id) + 'Text', null);
$textEl.prop('disabled', true);
} else {
$textEl.val(value.text);
Shiny.setInputValue(Shiny.$escape(el.id) + 'Text', value.text);
$textEl.prop('disabled', false);
}
}
if ($checkEl.prop('disabled')) {
$textEl.prop('disabled', true);
}
},
subscribe: function(el, callback) {
$(el).on('change', function(event) {
callback();
});
},
unsubscribe: function(el) {
$(el).off('change');
},
getState: function(el) {
return {
value: this.getValue(el)
};
},
receiveMessage: function(el, data) {
if (data.hasOwnProperty("value")) {
this.setValue(el, data.value);
}
if (data.hasOwnProperty("label")) {
$(el).find('label[for="' + el.id + 'Text"]').text(data.label);
}
if (data.hasOwnProperty("placeholder")) {
$("#" + el.id + "Text")[0].placeholder = data.placeholder;
}
// if (data.hasOwnProperty("disabled"))
// $(el).bootstrapSwitch("disabled", data.disabled, data.disabled);
$(el).trigger("change");
},
initialize: function initialize(el) {
$("#" + Shiny.$escape(el.id) + "Check").on('change', function(event) {
$(el).trigger('change');
if ($('#' + Shiny.$escape(el.id)).attr('aria-enable-on-check') == true) {
$('#' + Shiny.$escape(el.id) + 'Text').prop('disabled', !$(this).prop('checked'));
} else {
$('#' + Shiny.$escape(el.id) + 'Text').prop('disabled', $(this).prop('checked'));
}
if ($(this).prop('disabled')) {
$('#' + Shiny.$escape(el.id) + 'Text').prop('disabled', true);
}
});
$("#" + Shiny.$escape(el.id) + "Text").on('keyup', function(event) {
if (event.keyCode == 13) {
$(el).trigger('change')
}
});
}
});
Shiny.inputBindings.register(togglableTextInputBinding, "shinyExtra.toggleableTextInput");