-
Notifications
You must be signed in to change notification settings - Fork 0
/
binClock.js
244 lines (213 loc) · 6.49 KB
/
binClock.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
function BinaryClock( cont )
{
// Initialize variables, that will be used
// by public methods, with default values
var sOrientation = 'btt';
var bShowSeconds = true;
var bSecondsInTheMiddle = false;
var iRankBits = 4;
// Other vars
var asToggleClasses = [ 'on' , 'off' ]; // Classes for 1 and 0
var asHoursMinutes = [ 'h' , 'm' ]; // Hours, Minutes
var sSeconds = 's'; // Seconds - in case they are enabled
var asDecimalEntities = [ 'd' , 'e' ]; // For bits order that represents each digit
var iLoop = 0; // To keep setInterval ID
// Settings for container
var
iHorizontalSquaresCount
,iVerticalSquaresCount
,sOrientationAxis
,iXKoef
,asHMS
,asDE;
// Clock container and the container to append to
var oBody = null;
var oClockContainer = null;
// Constructor :)
var construct = function( cont )
{
oBody = document.createElement( 'div' );
oBody.className = 'binary-clock-cont';
appendClock( cont );
buildClockContainer( );
};
var appendClock = function( cont )
{
if( typeof cont != 'undefined' )
{
if( document.querySelector( cont ) ? document.querySelector( cont ).nodeType == 1 : false )
document.querySelector( cont ).appendChild( oBody );
else if( document.getElementById( cont ) ? document.getElementById( cont ).nodeType == 1 : false )
document.getElementById( cont ).appendChild( oBody );
}
return this;
};
var setTitle = function( arr )
{
oBody.setAttribute( 'title' , arr.join( ':' ) );
};
// Setup variables for the container, before build it
var setupBuildClockContainerParameters = function( )
{
sOrientationAxis = sOrientation.match( /btt|ttb/ ) ? 'x' : 'y';
iXKoef = sOrientation.match( /btt|rtl/ ) ? ( ( iRankBits - 1 ) * -1 ) : 0;
asHMS = asHoursMinutes.slice( 0 ); // Copy for local use
asDE = asDecimalEntities.slice( 0 ); // ... same here
if( bShowSeconds )
asHMS.splice( bSecondsInTheMiddle ? 1 : 2 , 0 , sSeconds );
if( iRankBits != 4 )
asDE.splice( 1 , 1 );
if( sOrientationAxis == 'x' )
{
iHorizontalSquaresCount = asHMS.length * asDE.length;
iVerticalSquaresCount = iRankBits;
}
else // if sOrientationAxis == 'y'
{
iVerticalSquaresCount = asHMS.length * asDE.length;
iHorizontalSquaresCount = iRankBits;
}
};
// Setup the HTML for the clock container
var buildClockContainer = function( )
{
setupBuildClockContainerParameters( );
oBody.innerHTML = '';
for( var i = 0; iVerticalSquaresCount * iHorizontalSquaresCount > i; ++i )
{
var oTempElement = document.createElement( 'span' );
oBody.appendChild( oTempElement );
var sHandleTimeType = asHMS[ sOrientationAxis == 'x' ? parseInt( i / asDE.length ) % asHMS.length : parseInt( i / ( iRankBits * asDE.length ) ) ];
var sHandleRankType = asDE[ parseInt( i / ( sOrientationAxis == 'x' ? 1 : iHorizontalSquaresCount ) ) % asDE.length ];
var sIDLetters = sHandleTimeType + sHandleRankType;
var sIDDigit = Math.pow( 2 , Math.abs( ( sOrientationAxis == 'x' ? parseInt( i / iHorizontalSquaresCount ) : i % iHorizontalSquaresCount ) + iXKoef ) )
oTempElement.setAttribute( 'id' , sIDLetters + sIDDigit.toString( 10 ) );
oTempElement.className = sIDLetters + ' ' + asToggleClasses[ 0 ] + ( i % iHorizontalSquaresCount ? '' : ' lf' ) + ' item-' + sHandleTimeType;
}
};
// Redraw the clock
var rebuildClock = function( )
{
stopClock( );
buildClockContainer( );
runClock( );
};
// Refresh clock "arrows"
var refreshClockTime = function( )
{
var oDate = new Date( );
var iHours = '0' + oDate.getHours( ).toString( );
var iMinutes = '0' + oDate.getMinutes( ).toString( );
var iSeconds = '0' + oDate.getSeconds( ).toString( );
if( !parseInt( iSeconds ) ) // Update them per minute
{
setClockDigits( 'h' , iHours );
setClockDigits( 'm' , iMinutes );
}
setTitle( [ iHours.substr( -2 ), iMinutes.substr( -2 ), iSeconds.substr( -2 ) ] );
if( bShowSeconds )
setClockDigits( 's' , '0' + iSeconds.toString( ) );
};
// Draw each "arrow"
var setClockDigits = function( l , d )
{
try
{
d = iRankBits == 4 ? d.substr( -2 ).split( '' ) : [ d ];
for( var i = 0; asDE.length > i; ++i )
for( var j = 0; iRankBits > j; ++j )
{
var oN = document.getElementById( l + String.fromCharCode( 'd'.charCodeAt( 0 ) + i ) + Math.pow( 2 , j ) );
var bN = ( d[ i ] & Math.pow( 2 , j ) ) ? true : false;
oN.className = oN.className.replace( asToggleClasses[ bN * 1 ] , asToggleClasses[ !bN * 1 ] );
}
}
catch( e )
{
if( e.name == 'TypeError' )
console.log( 'Whoops! I would like to share with you that, with a big probability, you didn\'t provide me with a valid container to appear in! Try again, huh? [' + e.message + ']' );
}
};
// Run the "mechanism"
var runClock = function( )
{
// Setup
setTimeout( function( ) {
var oDate = new Date( );
setClockDigits( 'h' , '0' + oDate.getHours( ).toString( ) );
setClockDigits( 'm' , '0' + oDate.getMinutes( ).toString( ) );
if ( bShowSeconds ) setClockDigits( 's' , '0' + oDate.getSeconds( ).toString( ) );
} , 0 );
// Loop
iLoop = setInterval( refreshClockTime , 1000 );
return returnObject;
};
// Stop clock
var stopClock = function( )
{
clearInterval( iLoop );
return returnObject;
};
// "Public" members
var returnObject =
{
body: oBody,
run: runClock,
stop: stopClock,
appendTo: appendClock,
showSeconds: function( )
{
bShowSeconds = true;
rebuildClock( );
return this;
},
hideSeconds: function( )
{
bShowSeconds = false;
rebuildClock( );
return this;
},
setOrientation: function( orient )
{
if( [ 'ttb' , 'btt' , 'ltr' , 'rtl' , 'vertical' , 'horizontal' ].indexOf( orient ) != -1 )
{
if ( orient == 'vertical' ) sOrientation = 'btt';
else if ( orient == 'horizontal' ) sOrientation = 'rtl';
else sOrientation = orient;
rebuildClock( );
}
return this;
},
setSecondsPosition: function( pos )
{
if( pos.match( /center|middle/ ) )
bSecondsInTheMiddle = true;
else if( pos.match( /end|side/ ) )
bSecondsInTheMiddle = false;
else
return this;
rebuildClock( );
return this;
},
setType: function( typ )
{
if( typ.match( /wide|long|big/ ) )
iRankBits = 6;
else if( typ.match( /tight|short|small/ ) )
iRankBits = 4;
else
return this;
rebuildClock( );
return this;
},
setTheme: function( thm )
{
oBody.className = 'binary-clock-cont';
oBody.classList.add( thm );
return this;
}
};
// "Emulate" constructor
construct.call( this , cont );
return returnObject;
}