Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.widget.Clock");
12
dojo.require("dojo.widget.*");
13
dojo.require("dojo.gfx.*");
14
dojo.require("dojo.uri.Uri");
15
dojo.require("dojo.lang.common");
16
dojo.require("dojo.lang.timing.Timer");
17
dojo.widget.defineWidget("dojo.widget.Clock", dojo.widget.HtmlWidget, function () {
18
	var self = this;
19
	this.timeZoneOffset = 0;
20
	this.label = "";
21
	this.date = new Date();
22
	this.handColor = "#788598";
23
	this.handStroke = "#6f7b8c";
24
	this.secondHandColor = [201, 4, 5, 0.8];
25
	this.topLabelColor = "#efefef";
26
	this.labelColor = "#fff";
27
	this.timer = new dojo.lang.timing.Timer(1000);
28
	this.center = {x:75, y:75};
29
	this.hands = {hour:null, minute:null, second:null};
30
	this.shadows = {hour:{shadow:null, shift:{dx:2, dy:2}}, minute:{shadow:null, shift:{dx:2, dy:3}}, second:{shadow:null, shift:{dx:4, dy:4}}};
31
	this.image = dojo.uri.moduleUri("dojo.widget", "templates/images/clock.png");
32
	this.surface = null;
33
	this.labelNode = null;
34
	this.topLabelNode = null;
35
	this.draw = function () {
36
		self.date = new Date();
37
		var h = (self.date.getHours() + self.timeZoneOffset) % 12;
38
		var m = self.date.getMinutes();
39
		var s = self.date.getSeconds();
40
		self.placeHour(h, m, s);
41
		self.placeMinute(m, s);
42
		self.placeSecond(s);
43
		self.topLabelNode.innerHTML = ((self.date.getHours() + self.timeZoneOffset) > 11) ? "PM" : "AM";
44
	};
45
	this.timer.onTick = self.draw;
46
}, {set:function (dt) {
47
	this.date = dt;
48
	if (!this.timer.isRunning) {
49
		this.draw();
50
	}
51
}, start:function () {
52
	this.timer.start();
53
}, stop:function () {
54
	this.timer.stop();
55
}, _initPoly:function (parent, points) {
56
	var path = parent.createPath();
57
	var first = true;
58
	dojo.lang.forEach(points, function (c) {
59
		if (first) {
60
			path.moveTo(c.x, c.y);
61
			first = false;
62
		} else {
63
			path.lineTo(c.x, c.y);
64
		}
65
	});
66
	return path;
67
}, _placeHand:function (shape, angle, shift) {
68
	var move = {dx:this.center.x + (shift ? shift.dx : 0), dy:this.center.y + (shift ? shift.dy : 0)};
69
	return shape.setTransform([move, dojo.gfx.matrix.rotateg(-angle)]);
70
}, placeHour:function (h, m, s) {
71
	var angle = 30 * (h + m / 60 + s / 3600);
72
	this._placeHand(this.hands.hour, angle);
73
	this._placeHand(this.shadows.hour.shadow, angle, this.shadows.hour.shift);
74
}, placeMinute:function (m, s) {
75
	var angle = 6 * (m + s / 60);
76
	this._placeHand(this.hands.minute, angle);
77
	this._placeHand(this.shadows.minute.shadow, angle, this.shadows.minute.shift);
78
}, placeSecond:function (s) {
79
	var angle = 6 * s;
80
	this._placeHand(this.hands.second, angle);
81
	this._placeHand(this.shadows.second.shadow, angle, this.shadows.second.shift);
82
}, init:function () {
83
	if (this.domNode.style.position != "absolute") {
84
		this.domNode.style.position = "relative";
85
	}
86
	while (this.domNode.childNodes.length > 0) {
87
		this.domNode.removeChild(this.domNode.childNodes[0]);
88
	}
89
	this.domNode.style.width = "150px";
90
	this.domNode.style.height = "150px";
91
	this.surface = dojo.gfx.createSurface(this.domNode, 150, 150);
92
	this.surface.createRect({width:150, height:150});
93
	this.surface.createImage({width:150, height:150, src:this.image + ""});
94
	var hP = [{x:-3, y:-4}, {x:3, y:-4}, {x:1, y:-27}, {x:-1, y:-27}, {x:-3, y:-4}];
95
	var mP = [{x:-3, y:-4}, {x:3, y:-4}, {x:1, y:-38}, {x:-1, y:-38}, {x:-3, y:-4}];
96
	var sP = [{x:-2, y:-2}, {x:2, y:-2}, {x:1, y:-45}, {x:-1, y:-45}, {x:-2, y:-2}];
97
	this.shadows.hour.shadow = this._initPoly(this.surface, hP).setFill([0, 0, 0, 0.1]);
98
	this.hands.hour = this._initPoly(this.surface, hP).setStroke({color:this.handStroke, width:1}).setFill({type:"linear", x1:0, y1:0, x2:0, y2:-27, colors:[{offset:0, color:"#fff"}, {offset:0.33, color:this.handColor}]});
99
	this.shadows.minute.shadow = this._initPoly(this.surface, mP).setFill([0, 0, 0, 0.1]);
100
	this.hands.minute = this._initPoly(this.surface, mP).setStroke({color:this.handStroke, width:1}).setFill({type:"linear", x1:0, y1:0, x2:0, y2:-38, colors:[{offset:0, color:"#fff"}, {offset:0.33, color:this.handColor}]});
101
	this.surface.createCircle({r:6}).setStroke({color:this.handStroke, width:2}).setFill("#fff").setTransform({dx:75, dy:75});
102
	this.shadows.second.shadow = this._initPoly(this.surface, sP).setFill([0, 0, 0, 0.1]);
103
	this.hands.second = this._initPoly(this.surface, sP).setFill(this.secondHandColor);
104
	this.surface.createCircle({r:4}).setFill(this.secondHandColor).setTransform({dx:75, dy:75});
105
	this.topLabelNode = document.createElement("div");
106
	with (this.topLabelNode.style) {
107
		position = "absolute";
108
		top = "3px";
109
		left = "0px";
110
		color = this.topLabelColor;
111
		textAlign = "center";
112
		width = "150px";
113
		fontFamily = "sans-serif";
114
		fontSize = "11px";
115
		textTransform = "uppercase";
116
		fontWeight = "bold";
117
	}
118
	this.topLabelNode.innerHTML = ((this.date.getHours() + this.timeZoneOffset) > 11) ? "PM" : "AM";
119
	this.domNode.appendChild(this.topLabelNode);
120
	this.labelNode = document.createElement("div");
121
	with (this.labelNode.style) {
122
		position = "absolute";
123
		top = "134px";
124
		left = "0px";
125
		color = this.labelColor;
126
		textAlign = "center";
127
		width = "150px";
128
		fontFamily = "sans-serif";
129
		fontSize = "10px";
130
		textTransform = "uppercase";
131
		fontWeight = "bold";
132
	}
133
	this.labelNode.innerHTML = this.label || " ";
134
	this.domNode.appendChild(this.labelNode);
135
	this.draw();
136
}, postCreate:function () {
137
	this.init();
138
	this.start();
139
}});
140