1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 var blankClockAnim1Image;
7 var blankClockAnim2Image;
12 function updateEnabledStatus(alarm) {
13 var enabled = $('a' + alarm + '_on').checked;
14 $('a' + alarm + '_tt').disabled = !enabled;
15 $('a' + alarm + '_ampm').disabled = !enabled;
18 var tt = $('a' + alarm + '_tt').value;
19 var ampm = $('a' + alarm + '_ampm').selectedIndex;
25 $('a' + alarm + '_wrap').removeAttribute('aria-invalid');
27 $('a' + alarm + '_wrap').setAttribute('aria-invalid', 'true');
30 $('a' + alarm + '_wrap').classList.remove('disabled');
32 $('a' + alarm + '_wrap').classList.add('disabled');
36 function loadAllImages() {
38 var img = new Image();
39 img.onload = function() {
40 blankClockImage = img;
41 currentClockImage = blankClockImage;
44 img.src = 'blank-clock-150.png';
46 // These will finish loading before they're needed, no need
47 // for an onload handler.
48 blankClockAnim1Image = new Image();
49 blankClockAnim1Image.src = 'blank-clock-ring1-150.png';
50 blankClockAnim2Image = new Image();
51 blankClockAnim2Image.src = 'blank-clock-ring2-150.png';
54 function drawClock(hh, mm, ss) {
55 if (hh == undefined || mm == undefined) {
59 ss = d.getSeconds() + 0.001 * d.getMilliseconds();
62 if (!currentClockImage) {
67 var ctx = $('clock').getContext('2d');
68 ctx.drawImage(currentClockImage, 0, 0);
70 // Move the hour by the fraction of the minute
71 hh = (hh % 12) + (mm / 60);
73 // Move the minute by the fraction of the second
76 var hourAngle = Math.PI * hh / 6;
77 var hourX = Math.sin(hourAngle);
78 var hourY = -Math.cos(hourAngle);
79 var minAngle = Math.PI * mm / 30;
80 var minX = Math.sin(minAngle);
81 var minY = -Math.cos(minAngle);
82 var secAngle = Math.PI * ss / 30;
83 var secX = Math.sin(secAngle);
84 var secY = -Math.cos(secAngle);
90 ctx.strokeStyle = '#ffffff';
91 ctx.globalAlpha = 0.5;
93 ctx.moveTo(cx - 4 * hourX, cy - 4 * hourY);
94 ctx.lineTo(cx + 20 * hourX, cy + 20 * hourY);
97 ctx.moveTo(cx - 8 * minX, cy - 8 * minY);
98 ctx.lineTo(cx + 35 * minX, cy + 33 * minY);
102 ctx.strokeStyle = '#696969';
103 ctx.globalAlpha = 1.0;
105 ctx.moveTo(cx - 4 * hourX, cy - 4 * hourY);
106 ctx.lineTo(cx + 20 * hourX, cy + 20 * hourY);
109 ctx.moveTo(cx - 8 * minX, cy - 8 * minY);
110 ctx.lineTo(cx + 35 * minX, cy + 33 * minY);
114 ctx.strokeStyle = '#990000';
115 ctx.globalAlpha = 1.0;
117 ctx.moveTo(cx - 4 * secX, cy - 4 * secY);
118 ctx.lineTo(cx + 40 * secX, cy + 40 * secY);
122 function updateCurrentTime() {
123 var now = new Date();
124 var hh = now.getHours();
125 var mm = now.getMinutes();
126 var ss = now.getSeconds();
150 $('current_time').innerText = str;
153 // Override from common.js
154 window.stopAlarmAnimation = function() {
155 window.clearTimeout(animationTimer);
156 currentClockImage = blankClockImage;
161 // Override from common.js
162 window.displayAlarmAnimation = function() {
167 stopAlarmAnimation();
170 currentClockImage = (rings % 2 == 0)?
171 blankClockAnim1Image:
172 blankClockAnim2Image;
175 animationTimer = window.setTimeout(ring, 50);
180 function addOutlineStyleListeners() {
181 document.addEventListener('click', function(evt) {
182 document.body.classList.add('nooutline');
185 document.addEventListener('keydown', function(evt) {
186 document.body.classList.remove('nooutline');
193 port = chrome.runtime.connect();
194 port.onMessage.addListener(function(msg) {
195 if (msg.cmd == 'anim') {
196 displayAlarmAnimation();
202 addOutlineStyleListeners();
206 setInterval(drawClock, 100);
209 setInterval(updateCurrentTime, 250);
211 function updateTime(timeElement) {
212 if (!parseTime(timeElement.value)) {
216 timeElement.valueAsNumber =
217 timeElement.valueAsNumber % (12 * 60 * 60 * 1000);
218 if (timeElement.valueAsNumber < (1 * 60 * 60 * 1000))
219 timeElement.valueAsNumber += (12 * 60 * 60 * 1000);
223 $('clock').addEventListener('click', function(evt) {
224 if (isPlaying || isSpeaking || isAnimating) {
227 ringAlarmWithCurrentTime();
230 $('clock').addEventListener('keydown', function(evt) {
231 if (evt.keyCode == 13 || evt.keyCode == 32) {
232 if (isPlaying || isSpeaking || isAnimating) {
235 ringAlarmWithCurrentTime();
242 var a1_tt = localStorage['a1_tt'] || DEFAULT_A1_TT;
243 $('a1_tt').value = a1_tt;
244 $('a1_tt').addEventListener('input', function(evt) {
245 updateEnabledStatus(1);
246 if (!updateTime($('a1_tt'))) {
247 evt.stopPropagation();
250 localStorage['a1_tt'] = $('a1_tt').value;
251 updateEnabledStatus(1);
254 $('a1_tt').addEventListener('change', function(evt) {
255 if ($('a1_tt').value.length == 4 &&
256 parseTime('0' + $('a1_tt').value)) {
257 $('a1_tt').value = '0' + $('a1_tt').value;
259 if (!updateTime($('a1_tt'))) {
260 evt.stopPropagation();
263 localStorage['a1_tt'] = $('a1_tt').value;
264 updateEnabledStatus(1);
268 var a1_on = (localStorage['a1_on'] == 'true');
269 $('a1_on').checked = a1_on;
270 $('a1_on').addEventListener('change', function(evt) {
271 window.setTimeout(function() {
272 localStorage['a1_on'] = $('a1_on').checked;
273 updateEnabledStatus(1);
277 var a1_ampm = localStorage['a1_ampm'] || DEFAULT_A1_AMPM;
278 $('a1_ampm').selectedIndex = a1_ampm;
279 $('a1_ampm').addEventListener('change', function(evt) {
280 localStorage['a1_ampm'] = $('a1_ampm').selectedIndex;
283 updateEnabledStatus(1);
287 var a2_tt = localStorage['a2_tt'] || DEFAULT_A2_TT;
288 $('a2_tt').value = a2_tt;
289 $('a2_tt').addEventListener('input', function(evt) {
290 updateEnabledStatus(2);
291 if (!updateTime($('a2_tt'))) {
292 evt.stopPropagation();
295 localStorage['a2_tt'] = $('a2_tt').value;
296 updateEnabledStatus(2);
299 $('a2_tt').addEventListener('change', function(evt) {
300 if ($('a2_tt').value.length == 4 &&
301 parseTime('0' + $('a2_tt').value)) {
302 $('a2_tt').value = '0' + $('a2_tt').value;
304 if (!updateTime($('a2_tt'))) {
305 evt.stopPropagation();
308 localStorage['a2_tt'] = $('a2_tt').value;
309 updateEnabledStatus(2);
313 var a2_on = (localStorage['a2_on'] == 'true');
314 $('a2_on').checked = a2_on;
315 $('a2_on').addEventListener('change', function(evt) {
316 window.setTimeout(function() {
317 localStorage['a2_on'] = $('a2_on').checked;
318 updateEnabledStatus(2);
322 var a2_ampm = localStorage['a2_ampm'] || DEFAULT_A2_AMPM;
323 $('a2_ampm').selectedIndex = a2_ampm;
324 $('a2_ampm').addEventListener('change', function(evt) {
325 localStorage['a2_ampm'] = $('a2_ampm').selectedIndex;
328 updateEnabledStatus(2);
332 var phrase = localStorage['phrase'] || DEFAULT_PHRASE;
333 $('phrase').value = phrase;
334 $('phrase').addEventListener('change', function(evt) {
335 localStorage['phrase'] = $('phrase').value;
340 var rateElement = $('rate');
341 var volumeElement = $('volume');
342 var rate = localStorage['rate'] || DEFAULT_RATE;
343 var volume = localStorage['volume'] || DEFAULT_VOLUME;
344 rateElement.value = rate;
345 volumeElement.value = volume;
346 function listener(evt) {
347 rate = rateElement.value;
348 localStorage['rate'] = rate;
349 volume = volumeElement.value;
350 localStorage['volume'] = volume;
352 rateElement.addEventListener('keyup', listener, false);
353 volumeElement.addEventListener('keyup', listener, false);
354 rateElement.addEventListener('mouseup', listener, false);
355 volumeElement.addEventListener('mouseup', listener, false);
357 var sound = $('sound');
358 var currentSound = localStorage['sound'] || DEFAULT_SOUND;
359 for (var i = 0; i < sound.options.length; i++) {
360 if (sound.options[i].value == currentSound) {
361 sound.selectedIndex = i;
365 localStorage['sound'] = sound.options[sound.selectedIndex].value;
366 sound.addEventListener('change', function() {
367 localStorage['sound'] = sound.options[sound.selectedIndex].value;
370 var playSoundButton = $('playsound');
371 playSoundButton.addEventListener('click', function(evt) {
375 var playSpeechButton = $('playspeech');
376 playSpeechButton.addEventListener('click', function(evt) {
377 speakPhraseWithCurrentTime();
380 var voice = $('voice');
382 if (chrome && chrome.tts) {
383 chrome.tts.getVoices(function(va) {
385 for (var i = 0; i < voiceArray.length; i++) {
386 var opt = document.createElement('option');
387 var name = voiceArray[i].voiceName;
388 if (name == localStorage['voice']) {
389 opt.setAttribute('selected', '');
391 opt.setAttribute('value', name);
392 opt.innerText = voiceArray[i].voiceName;
393 voice.appendChild(opt);
397 voice.addEventListener('change', function() {
398 var i = voice.selectedIndex;
399 localStorage['voice'] = voiceArray[i].voiceName;
403 document.addEventListener('DOMContentLoaded', load);