Implementation of additional shiny inputs (no documentation)
This commit is contained in:
83
inst/js/numericButtonInput.js
Normal file
83
inst/js/numericButtonInput.js
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
// switch input binding
|
||||
var numericButtonInputBinding = new Shiny.InputBinding();
|
||||
$.extend(numericButtonInputBinding, {
|
||||
find: function(scope) {
|
||||
return $(scope).find('.shiny-input-numericButton');
|
||||
},
|
||||
getId: function(el) {
|
||||
return $(el).attr('id');
|
||||
},
|
||||
getValue: function(el) {
|
||||
var value = $('#' + Shiny.$escape(el.id) + '_value').val();
|
||||
|
||||
if (/^\s*$/.test(value))
|
||||
// Return null if all whitespace
|
||||
value = null;
|
||||
else if (!isNaN(value))
|
||||
// If valid Javascript number string, coerce to number
|
||||
value = +value;
|
||||
else
|
||||
value = value;
|
||||
|
||||
return value;
|
||||
},
|
||||
setValue: function(el, value) {
|
||||
$('#' + Shiny.$escape(el.id) + '_value').val(value);
|
||||
},
|
||||
subscribe: function(el, callback) {
|
||||
$(el).on('change', function(event) {
|
||||
callback();
|
||||
});
|
||||
},
|
||||
unsubscribe: function(el) {
|
||||
$(el).off('change');
|
||||
},
|
||||
getState: function(el) {
|
||||
$numeric = $('#' + Shiny.$escape(el.id) + '_value');
|
||||
return {
|
||||
label: $(el).find('label[for="' + el.id + '_value"]').text(),
|
||||
button: $('#' + Shiny.$escape(el.id) + 'Button').val(),
|
||||
value: this.getValue(el),
|
||||
min: $numeric.attr('min'),
|
||||
max: $numeric.attr('max'),
|
||||
step: $numeric.attr('step')
|
||||
};
|
||||
},
|
||||
receiveMessage: function(el, data) {
|
||||
$numeric = $('#' + Shiny.$escape(el.id) + '_value');
|
||||
|
||||
if (data.hasOwnProperty("label")) {
|
||||
$(el).find('label[for="' + el.id + '_value"]').text(data.label);
|
||||
}
|
||||
|
||||
if (data.hasOwnProperty("min")) {
|
||||
$numeric.attr('min', data.min)
|
||||
}
|
||||
if (data.hasOwnProperty("max")) {
|
||||
$numeric.attr('max', data.max)
|
||||
}
|
||||
if (data.hasOwnProperty("step")) {
|
||||
$numeric.attr('step', data.step)
|
||||
}
|
||||
|
||||
if (data.hasOwnProperty("value")) {
|
||||
this.setValue(el, data.value);
|
||||
}
|
||||
|
||||
$(el).trigger("change");
|
||||
},
|
||||
initialize: function initialize(el) {
|
||||
$("#" + Shiny.$escape(el.id) + "_value").on('keyup', function(event) {
|
||||
$(el).trigger('change')
|
||||
});
|
||||
$("#" + Shiny.$escape(el.id) + "_value").on('input', function(event) {
|
||||
$(el).trigger('change')
|
||||
});
|
||||
$("#" + Shiny.$escape(el.id) + "_value").on('change', function(event) {
|
||||
$(el).trigger('change')
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Shiny.inputBindings.register(numericButtonInputBinding, "shinyExtra.numericButtonInput");
|
||||
Reference in New Issue
Block a user