function Input(name, value) { this.name = name; this.value = value; }; Input.prototype.bootstrapify = function(input, label) { input.addClass('form-control').addClass('form-control-sm'); return [ '
', '', '
', input[0].outerHTML, '
', '
' ].join(''); }; function TextInput() { Input.apply(this, arguments); }; TextInput.prototype = new Input(); TextInput.prototype.render = function() { return this.bootstrapify($('')); } function NumberInput() { Input.apply(this, arguments); }; NumberInput.prototype = new Input(); NumberInput.prototype.render = function() { return this.bootstrapify($('')); }; function ProfileInput() { Input.apply(this, arguments); }; ProfileInput.prototype = new Input(); ProfileInput.prototype.render = function() { return $('

Profiles

'); }; function SchedulerInput() { Input.apply(this, arguments); }; SchedulerInput.prototype = new Input(); SchedulerInput.prototype.render = function() { return $('

Scheduler

'); }; Input.mappings = { "name": TextInput, "type": TextInput, "ppm": NumberInput, "profiles": ProfileInput, "scheduler": SchedulerInput }; function SdrDevice(el) { this.el = el; this.data = JSON.parse(decodeURIComponent(el.data('config'))); this.inputs = {}; this.render(); var self = this; el.on('click', '.fieldselector .btn', function() { var key = el.find('.fieldselector select').val(); self.data[key] = false; self.render(); }); }; SdrDevice.prototype.render = function() { var self = this; self.el.empty(); $.each(this.data, function(key, value) { var inputClass = Input.mappings[key] || TextInput; var input = new inputClass(key, value); self.inputs[key] = input; self.el.append(input.render()); }); self.el.append(this.renderFieldSelector()); }; SdrDevice.prototype.renderFieldSelector = function() { var self = this; return '
' + '

Add new configuration options

' + '
' + '
' + '
' + '
Add to config
' + '
' + '
' + '

'; }; $.fn.sdrdevice = function() { return this.map(function(){ var el = $(this); if (!el.data('sdrdevice')) { el.data('sdrdevice', new SdrDevice(el)); } return el.data('sdrdevice'); }); }; $(function(){ $(".map-input").each(function(el) { var $el = $(this); var field_id = $el.attr("for"); var $lat = $('#' + field_id + '-lat'); var $lon = $('#' + field_id + '-lon'); $.getScript("https://maps.googleapis.com/maps/api/js?key=" + $el.data("key")).done(function(){ $el.css("height", "200px"); var lp = new locationPicker($el.get(0), { lat: parseFloat($lat.val()), lng: parseFloat($lon.val()) }, { zoom: 7 }); google.maps.event.addListener(lp.map, 'idle', function(event){ var pos = lp.getMarkerPosition(); $lat.val(pos.lat); $lon.val(pos.lng); }); }); }); $(".sdrdevice").sdrdevice(); });