<!--
   /*************************************************************************
    *                                                                       *
    * chummix_config.lzx                                                    *
    *                                                                       *
    *************************************************************************
    *                                                                       *
    * Provides implementation for a Widget Configuration Dialog (Chumby)    *
    * Uses OpenLazlo framework to generate flash (swf)                      *
    * Doesn't support displaying existing values (yet)                      *
    *                                                                       *
    * Copyright (c) 2010 Sven Werlen <admin@chummix.org>                    *
    * All rights reserved.                                                  *
    *                                                                       *
    * This script is free software; you can redistribute it and/or modify   *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    * The GNU General Public License can be found at                        *
    * http://www.gnu.org/copyleft/gpl.html.                                 *
    *                                                                       *
    * This script is distributed in the hope that it will be useful,        *
    * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
    * GNU General Public License for more details.                          *
    *                                                                       *
    * Author(s): Sven Werlen <admin@chummix.org>                            *
    *                                                                       *
    * Last modified: 2010-08-20                                             *
    *                                                                       *
    *************************************************************************/


    Data structure to be sent to Chumby server
    ==========================================

	<widget_parameters>
		<widget_parameter>
			<name>ParamName1</name>
			<value>ParamValue1</value>
		</widget_parameter>
		<widget_parameter>
			<name>ParamName2</name>
			<value>ParamValue2</value>
		</widget_parameter>
		...
	</widget_parameters>

    Important!
    ==========
    If you don't want parameter values to be copied when a user sends the application to another,
    make sure that its name starts with "_private"
	
-->
<canvas name="canvas" width="320" height="240">

	<!-- Library to post an XML to chumby server -->
	<include href="./xmlhttprequest.lzx"/>
	<XMLRequest name="remote" />
	
	<!-- Dataset (XML) that will be sent to server -->
	<dataset name="widget_instance" autorequest="false"></dataset>
	
	<!-- Application name -->
	<text fontsize="20" x="100" y="2" fontstyle="bolditalic" fgcolor="#3884bf">Chummix</text>
	
	<!-- Logo / Button to submit data -->
	<view name="actionbutton" x="250" y="10" resource="img/chummix.png">
		<handler name="onclick">
			// Obtain a datapointer
			var dp = canvas.datasets.widget_instance.getPointer();
			
			// Add root
			var root = 'widget_parameters';
			dp.addNode( root );
			dp.setXPath( "widget_instance:/" + root );
			
			// Add 3 parameters
			var param = 'widget_parameter';
			dp.addNode( param );
			dp.addNode( param );
			dp.addNode( param );
			
			// Param 1: username
			dp.setXPath( "widget_instance:/" + root + "/" + param + "[1]" );
			dp.addNode( "name", "_private_username" );
			dp.addNode( "value", canvas.main.username.input.getText() );
			
			// Param 2: password
			dp.setXPath( "widget_instance:/" + root + "/" + param + "[2]" );
			dp.addNode( "name", "_private_password" );
			dp.addNode( "value", canvas.main.password.input.getText() );
			
			// Param 3: private key
			dp.setXPath( "widget_instance:/" + root + "/" + param + "[3]" );
			dp.addNode( "name", "_private_key" );
			dp.addNode( "value", canvas.main.key.input.getText() );
			
			var dspointer = canvas.datasets.widget_instance.getPointer();
			//Debug.write( dspointer.serialize() );
			
			// Server URL is passed as parameter by chumby.com
			remote.open(_root._chumby_instance_url);
			remote.send(dspointer);
			
			// Hide the submit button to avoid double-submits
			actionbutton.setAttribute('visible',false);
			
			// Wait before closing the widget or the data will never reach the server :-(
			lz.Timer.addTimer( new LzDelegate( this, "dismiss" ), 3000 );
		</handler>
		
		<!-- 
		     Dismiss method
		     ==============
		     Closes the popup-window (Chumby preferences)
		 -->
		<method name="dismiss" args="ignore=null"> 
			// close configuration panel
			Debug.write("Closing");
			lz.Browser.loadURL("javascript:dismiss()");
		</method>
	</view>
	
	
	<!-- 
	     Main view
	     =========
	     Put your fields here!
	-->
	<view name="main" x="20" y="25">
		<simplelayout/>
		<text fontsize="20" fgcolor="#3884bf">Username</text>
		<field name="username"/>
		<text fontsize="20" fgcolor="#3884bf">Password</text>
		<field name="password"/>
		<text fontsize="20" fgcolor="#3884bf">Private key</text>
		<field name="key"/>
		<text fgcolor="#3884bf" x="20" fontstyle="italic">Press on logo (top right) to submit settings...</text>
	</view>
	
	
	<!-- 
	     Class field
	     ===========
	     Defines a simple input field and defines a style for it
	     Can be re-used in views
	-->
	<class name="field" extends="view" height="35" width="280" bgcolor="black">
		<inputtext name="input" bgcolor="#dde6ec" fontsize="20"
		      x="2" y="2"
		      width="${parent.width-4}"
		      height="${parent.height-4}"/>
	</class>
	
</canvas>
