Version 0.9.1

Datei 'www/global/s_config.js' Zurück zur Übersicht
function getAllDevices() {
	var html, name,
		url = 'ajax/get_all_devices.php',
		data = {},
		ajax = {url: url, data: data, dataType: "json"};
	ajax.complete = function (d) {
		data = d.responseJSON;
		
		$(data.devices).each(function () {
			name = (typeof this.desc !== "undefined" && this.desc != "")? this.desc : this.id;
			html =	'<li class="device" device="'+ this.id +'">'+
						'<input type="hidden" class="device-id" value="'+ this.id +'">'+
						'<div class="device-desc"><span class="device-name">'+ name +'</span> (<span class="device-type">'+ this.type +'</span>)</div>'+
						'<div class="device-valueOn"><span>Value On</span><span><input param="valueOn" type="numer" value="'+ this.valueOn +'"></span></div>'+
						'<div class="device-valueOff"><span>Value Off</span><span><input param="valueOff" type="numer" value="'+ this.valueOff +'"></span></div>'+
					'</li>';
			$(html).appendTo('#devices');
		});
		
		if(data.devices.length > 0) {
			$('input', '#devices').change(function () { $(this).addClass('changed'); });
			$('<li><div style="text-align: center"><button id="save-config" onclick="writeAllNewValues()";>Speichern</button></div></li>').appendTo('#devices');
		}
	};
	
	$.ajax(ajax);
}

function writeNewValue(device, p, val) {
	var url = 'ajax/change_device.php',
		data = {device: device, p: p, val: val},
		ajax = {url: url, data: data, dataType: "json"};
		
	ajax.complete = function (d) {
		data = d.responseJSON;
		if(data.found == "true") {
			alert("Änderung gespeichert!");
		} else {
			alert("Ooops, something went wrong!");
		}
	}
	
	$.ajax(ajax);
}

function writeAllNewValues() {
	var device, p, val, devs = $('input.changed', '#devices');
	$(devs).each(function () {
		device = $(this).closest('.device').attr('device');
		p = $(this).attr('param');
		val = $(this).val();
		
		console.log(device,p,val);
		writeNewValue(device, p, val)
	});
	
	if(devs.length == 0) alert('Keine Änderungen vorhanden!');
}

$(function () {
	getAllDevices();
});