OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be found | |
3 // in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview This file contains the JavaScript required for the WebGTT | |
7 * application, specifically, the implementation of the NaClModule class. This | |
8 * implementation provides functions for manipulating a NaCl module, and | |
9 * adding/managing event handlers for a NaCl module. | |
10 * | |
11 * @author ragad@google.com (Raga Gopalakrishnan) | |
12 */ | |
13 | |
14 /** | |
15 * This constant holds the color palette used to color the vertices. | |
16 * @type {object} | |
17 * @const | |
18 */ | |
19 var COLOR_PALETTE = [ | |
20 'red', | |
21 'blue', | |
22 'green', | |
23 'yellow', | |
24 'orange', | |
25 'magenta', | |
26 'chocolate', | |
27 'black', | |
28 'greenyellow', | |
29 'grey', | |
30 'pink', | |
31 'yellowgreen' | |
32 ]; | |
33 | |
34 /** | |
35 * This class is used to represent a NaCl module. | |
36 * | |
37 * @param {object} naclModule Handle to the DOM object representing the NaCl | |
38 * module. | |
39 * @param {object} graph1 Reference to the graph associated with the NaCl | |
40 * module. | |
41 * @constructor | |
42 */ | |
43 NaClModule = function (naclModule, graph1) { | |
44 this.naclModule = naclModule; | |
45 this.graph1 = graph1; | |
46 coloringButton1.setText(BUTTON_TEXT); | |
47 if (canvas1.graph1.listOfVertices.length > 0) { | |
48 coloringButton1.setDisabled(false); | |
49 } | |
50 }; | |
51 | |
52 /** | |
53 * This function invokes the postMessage function on the NaCl module. | |
54 * | |
55 * @param {string} message The argument for postMessage. | |
56 */ | |
57 NaClModule.prototype.postMessage = function (message) { | |
58 this.naclModule.postMessage(message); | |
59 }; | |
60 | |
61 /** | |
62 * This function handles the 'message' event on the NaCl module. | |
63 * | |
64 * This handler is fired when the NaCl module posts a message to the browser. | |
65 * Here, we color the graph according to the coloring that is received from the | |
66 * NaCl module. | |
67 * | |
68 * @param {object} messageEvent The Event object containing information about | |
69 * the 'message' event. | |
70 */ | |
71 NaClModule.prototype.handleMessage = function (messageEvent) { | |
72 var tempString = ''; | |
73 var vertexIndex = 0; | |
74 | |
75 for (var i = 0 ; i < messageEvent.data.length ; i++) { | |
76 if(messageEvent.data[i] != ',') { | |
77 tempString += messageEvent.data[i]; | |
78 continue; | |
79 } | |
80 this.graph1.listOfVertices[vertexIndex].setColor(COLOR_PALETTE[parseInt( | |
81 tempString)]); | |
82 tempString = ''; | |
83 vertexIndex ++; | |
84 } | |
85 this.graph1.listOfVertices[vertexIndex].setColor(COLOR_PALETTE[parseInt( | |
86 tempString)]); | |
87 | |
88 canvas1.redrawCanvas(); | |
89 alert('A coloring is displayed!'); | |
90 | |
91 for (var i = 0 ; i < ((messageEvent.data.length)+1)/2 ; i++) { | |
92 this.graph1.listOfVertices[i].setColor('black'); | |
93 } | |
94 | |
95 canvas1.redrawCanvas(); | |
96 | |
97 canvas1.setEditMode(true); | |
98 coloringButton1.setDisabled(false); | |
99 coloringButton1.setText(BUTTON_TEXT); | |
100 }; | |
OLD | NEW |