Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/html/app.js
blob: 9b1b927fc435b840f9af626b66e277a2de856711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Enyo content used in a Sugar Activity
// Show bi-directionnal communication between Sugar and Enyo

enyo.kind({
	name: "App",
	classes: "onyx main-window",
	components: [
		// HTML5 logo and introduction text
		{kind: "Image", classes: "logo", src: "images/html5-logo.jpg"},	
		{classes: "block-top", components: [
			{content: "This is the HTML5 part of the activity.", classes:"hello-text"},
			{content: "All content is handle by JavaScript code.", classes:"hello-line"}
		]},
		
		// Welcome message using buddy setup
		{classes: "divider", content: "Environment"},
		{components: [
			{content: "Hello", classes: "hello-text"},
			{name: "buddyName", content: "NAME", classes: "hello-text hello-buddy"},
			{content: "!", classes: "hello-text"},
			{content: "(colors and name come from Sugar)", classes: "hello-advice"}
		]},
		
		// Buttons to send basic messages
		{classes: "divider", content: "Basic message"},
		{kind: "onyx.Button", content: "Send string to Python", ontap: "buttonStringClicked"},
		{kind: "onyx.Button", content: "Send object to Python", ontap: "buttonObjectClicked", classes: "spacer"},
		{content: "(send a simple message with a string or an object)", classes: "hello-advice"},
		
		// Counter affected by toolbar activity buttons
		{classes: "divider", content: "Toolbar"},		
		{components: [
			{content: "Page count:", classes: "hello-text"},
			{name: "pageCount", classes: "hello-text hello-buddy"},
			{content: "(use toolbar buttons to update page count)", classes: "hello-advice"}
		]},
		
		// Right part of the screen
		{classes: "right-part", components: [
			// Controls synchronized with matching Python controls
			{classes: "divider", content: "Synchronized controls"},
			{name: "checkbox", kind: "onyx.Checkbox", checked: true, onchange: "checkboxClicked"},
			{content: "Checkbox", classes: "hello-text spacer"},
			{components: [
				{kind: "onyx.InputDecorator", components: [
						{name: "sliderValue", kind: "onyx.Input", classes: "slider-text", onchange: "valueChanged"}
				]},
				{name: "sliderBar", kind: "onyx.Slider", classes: "slider-bar", onChange: "sliderChanged"}
			]},
			{content: "(click to update Python counterpart at the same time)", classes: "hello-advice"},

			// Canvas area
			{classes: "divider", content: "Canvas"},
			{kind: "Canvas", name: "canvas", classes: "canvas-style", attributes: {width: 200, height: 200}},		
			{content: "(use Python buttons to move the turtle)", classes: "hello-advice"},
			{kind: "Image", id: "liogo", src:"images/liogo.png", classes: "liogo-logo", onload: "initLiogo" }
		]}
	],
	
	// Constructor
	create: function() {
		this.inherited(arguments);
		
		// Init sugar interface
		this.sugar = new Sugar();

		// Connect to python events
		this.sugar.connect("helloFromPY", function(args) {
			// Displayed as JSON string to see structure
			alert("JavaScript received "+JSON.stringify(args));
		});
		this.sugar.connect("buddy", enyo.bind(this, "buddyChanged"));
		this.sugar.connect("home_clicked", enyo.bind(this, "upgradePageCount"));
		this.sugar.connect("back_clicked", enyo.bind(this, "upgradePageCount"));
		this.sugar.connect("forward_clicked", enyo.bind(this, "upgradePageCount"));
		this.sugar.connect("py_slider_changed", enyo.bind(this, "pySliderChanged"));
		this.sugar.connect("py_checkbox_changed", enyo.bind(this, "pyCheckboxChanged"));
		this.sugar.connect("liogo_forward_clicked", enyo.bind(this, "bForward"));
		this.sugar.connect("liogo_left_clicked", enyo.bind(this, "bLeft"));
		this.sugar.connect("liogo_right_clicked", enyo.bind(this, "bRight"));
		this.sugar.connect("liogo_clear_clicked", enyo.bind(this, "bClear"));
		
		// Send a message to python
		this.sugar.sendMessage("ready")

		// Init synchronized control
		this.$.sliderBar.setValue(50);
		this.$.sliderValue.setValue(50);
		this.$.pageCount.setContent(1);
		
		// Just to see that console function stay usable
		console.log("Unhandled console message");		
	},

	// Python sent buddy setup
	buddyChanged: function(args) {
		// Redraw buddy context
		this.$.buddyName.applyStyle("background-color", args.colors[1]);
		this.$.buddyName.applyStyle("color", args.colors[0]);
		this.$.buddyName.setContent(args.name);
	},
	
	// Create Liogo component when the turtle image is loaded
	initLiogo: function() {
		// Init mini Logo component
		this.liogo = new Liogo({canvas: this.$.canvas, image: document.getElementById("liogo") });
		this.liogo.draw();
	},
	
	// Send a simple string message to Python
	buttonStringClicked: function() {
		this.sugar.sendMessage("helloFromJS", "Hello Python !");
	},
	
	// Send a dummy JavaScript object to Python
	buttonObjectClicked: function() {
		var person = { name: "Lionel", version: 2.0, language: ["Python", "JavaScript"] };
		person.modified = new Date();
		this.sugar.sendMessage("helloFromJS", person);
	},
	
	// Checkbox clicked, signal the changed to the matching Python control
	checkboxClicked: function() {
		this.sugar.sendMessage("JScheckboxChanged", this.$.checkbox.getChecked());
	},

	// Handle Python message coming from toolbar
	upgradePageCount: function(args) {
		// Process toolbar button click
		var currentValue = parseInt(this.$.pageCount.getContent());
		switch(args)
		{
		case 1:
			currentValue++;
			break;
		case -1:
			currentValue--;
			break;
		case 0:
			currentValue = 1;
			break;
		}
		this.$.pageCount.setContent(currentValue);

		// Change toolbar button sensitivity depending of current page
		var back = "False";
		var forward = "False";
		if (currentValue == 1)
			back = "True"
		else if (currentValue == 10)
			forward = "True"
		this.sugar.sendMessage("disableBack", back);
		this.sugar.sendMessage("disableForward", forward);
	},
	
	// Slider changed, change the value and send a message to matching Python control
	sliderChanged: function(inSender, inEvent) {
		var newvalue = parseInt(this.$.sliderBar.getValue());
		this.$.sliderValue.setValue(newvalue);
		this.sugar.sendMessage("JSsliderChanged", newvalue);
	},

	// Slider value changed, change the slider and send a message to matching Python control	
	valueChanged: function(inSender, inEvent) {
		var newvalue = parseInt(this.$.sliderValue.getValue());
		this.$.sliderBar.setValue(newvalue);
		this.sugar.sendMessage("JSsliderChanged", newvalue);		
	},

	// Python slider value has changed, update the matching JavaScript control
	pySliderChanged: function(args) {	
		var newvalue = parseInt(args);
		this.$.sliderBar.setValue(newvalue);
		this.$.sliderValue.setValue(newvalue);
	},

	// Python checkbox value has changed, update the matching JavaScript control	
	pyCheckboxChanged: function(args) {	
		this.$.checkbox.setChecked(args);
	},
	
	// Python forward turtle button clicked, process it in the mini Logo component
	bForward: function(args) { 
		this.liogo.forward(args);
		this.liogo.draw();
	},
	
	// Python left turtle button clicked, process it in the mini Logo component	
	bLeft: function(args) { 
		this.liogo.left(args);
		this.liogo.draw();
	},
	
	// Python right turtle button clicked, process it in the mini Logo component	
	bRight: function(args) { 
		this.liogo.right(args);
		this.liogo.draw();
	},
	
	// Python clear turtle button clicked, process it in the mini Logo component	
	bClear: function() { 
		this.liogo.clear();
		this.liogo.draw();
	}
});