This repository has been archived by the owner on Dec 31, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
roundrect.js
1501 lines (1306 loc) · 40.2 KB
/
roundrect.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (undefined) {
// That’s not IE! Getouttahere.
if (!window.attachEvent) {
return;
}
/**
* RoundRect. Makes funny looking square boxes into funny looking round
* boxes in your funny looking Microsoft browser.
*
* (Not DD_roundies.)
*
* Original DD_roundies © 2008 Drew Diller <[email protected]>
* RoundRect © 2010 Colin Snover <http://zetafleet.com>
*
* Released under MIT license.
*/
/**
* The nodeName for the element that is wrapped around the VML, as well as
* the name of the globally exposed RoundRect method.
* Defaults to RoundRect. You can change it to something else if you don’t
* like it (for example, if you are a dork and think CS_undies is a better
* name).
* @type {string}
*/
var ns = 'RoundRect',
/**
* The namespace prefix that VML is bound to.
* @type {string}
*/
xmlns = 'rr',
/**
* You can change these if you want to use a specific prefix in your
* CSS instead of using the unprefixed version of border-radius, though
* mostly they are here because these strings are stupidly long.
* @type {string}
*/
br = 'border-radius',
/**
* @type {string}
*/
btl = 'border-top-left-radius',
/**
* @type {string}
*/
btr = 'border-top-right-radius',
/**
* @type {string}
*/
bbr = 'border-bottom-right-radius',
/**
* @type {string}
*/
bbl = 'border-bottom-left-radius',
/**
* @type {string}
*/
expando = ns + new Date().getTime(),
/**
* @type {number}
*/
uuid = 0,
/**
* Internal collection of all RoundRect objects. Used to ensure all
* RoundRects are properly cleaned up so that IE does not leak memory
* all over the ground.
* @type {Object.<string, RoundRect>}
*/
collection = {},
/**
* @type {boolean}
*/
ie8 = document.documentMode === 8,
/**
* @type {boolean}
*/
isDOMReady = false,
/**
* @type {Array.<function(this:Document)>}
*/
readyList = [],
/**
* A map of images that are used as background-images. This is needed
* in order to get the correct size of the original image in order to
* clip it for repeat-x and repeat-y.
* @type {Object.<string, (Image|Object)>}
*/
imageMap = {},
/**
* @type {RegExp}
*/
isPixelString = /^-?\d+(?:px)?$/i,
/**
* @type {RegExp}
*/
isNumericString = /^-?\d/,
/**
* @type {string}
*/
hoverClass = ns + '-hover';
/**
* Proxy function.
* @param {Object} obj Object to bind as ‘this’
* @param {Function} fn Function to call
* @return {Function}
*/
function proxy(obj, fn) {
return function () {
return fn.apply(obj || this, arguments);
};
}
/**
* Registers a function to be executed on DOM ready.
* @param {Function} fn
*/
function ready(fn) {
if (isDOMReady) {
fn.call(document);
return;
}
readyList.push(fn);
}
/**
* Gets a pixel value for any CSS value string.
* @param {Element} e
* @param {string} prop
*/
function getPixelValue(e, prop) {
var left = e.style.left,
rsLeft = e.runtimeStyle.left,
value = e.currentStyle[prop];
if (isPixelString.test(value)) {
return parseInt(e.currentStyle[prop], 10);
}
if (isNumericString.test(value)) {
// Put in the new values to get a computed value out
e.runtimeStyle.left = e.currentStyle.left;
e.style.left = value;
value = e.style.pixelLeft;
// Revert the changed values
e.style.left = left;
e.runtimeStyle.left = rsLeft;
return value;
}
return 0;
}
/**
* Determines whether or not an element is a button input.
* @param {Element} element
* @return {boolean}
*/
function isButton(element) {
var tagName, type;
if (!element || !element.nodeName || !element.type) {
return false;
}
tagName = element.nodeName.toUpperCase();
type = element.type.toLowerCase();
return (tagName === 'BUTTON' || (tagName === 'INPUT'
&& (type === 'image' || type === 'button' || type === 'submit' || type === 'reset')));
}
/**
* Determines whether or not an element is a text input.
* @param {Element} element
* @return {boolean}
*/
function isTextField(element) {
var tagName, type;
if (!element || !element.nodeName) {
return false;
}
tagName = element.nodeName.toUpperCase();
type = tagName === 'INPUT' ? element.type.toLowerCase() : null;
return ((tagName === 'INPUT' && (element.type === 'text' || element.type === 'password'))
|| tagName === 'TEXTAREA');
}
/**
* Enables VML on the current page.
*/
(function enableVml() {
var css, rule;
// IE will throw confused errors if document.namespaces
// is not ready for our sweet sweet lovin’
try {
if (ie8) {
document.namespaces.add(xmlns, 'urn:schemas-microsoft-com:vml', '#default#VML');
}
else {
document.namespaces.add(xmlns, 'urn:schemas-microsoft-com:vml');
}
}
catch (e) {
setTimeout(enableVml, 10);
return;
}
// Technically not part of enabling VML, but it is important to
// prevent all sorts of havoc
try {
document.execCommand('BackgroundImageCache', false, true);
}
catch (e) {}
// luckily, IE does not care that styles are going into the body
css = document.createElement('style');
document.body.appendChild(css);
rule = 'behavior:url(#default#VML);display:inline-block';
css.styleSheet.addRule(xmlns + '\\:shape', rule);
css.styleSheet.addRule(xmlns + '\\:group', rule);
css.styleSheet.addRule(xmlns + '\\:fill', rule);
}());
/**
* Executes onReady once the DOM has loaded.
*/
function onReady() {
var fn;
if (isDOMReady) {
document.detachEvent('onreadystatechange', onReady);
return;
}
if (document.readyState === 'complete') {
document.detachEvent('onreadystatechange', onReady);
if (!document.body) {
setTimeout(onReady, 13);
return;
}
isDOMReady = true;
while ((fn = readyList.shift())) {
fn.call(document);
}
}
}
document.attachEvent('onreadystatechange', onReady);
/**
* Poll for early document ready state.
*/
(function scrollCheck() {
if (isDOMReady) {
return;
}
try {
document.documentElement.doScroll('left');
}
catch (e) {
setTimeout(scrollCheck);
return;
}
onReady();
}());
/**
* Only you can prevent horrible memory leaks in IE—because Microsoft
* doesn’t. (j/k guys i am sure ie9 will be leak-free.)
*/
window.attachEvent('onunload', function () {
for (var i in collection) {
if (collection.hasOwnProperty(i)) {
try {
collection[i].destroy();
}
catch (e) {}
}
}
collection = null;
});
/**
* Creates a new RoundRect object.
* @class RoundRect
* @constructor
* @param {Element} element
* @param {boolean} dynamic
*/
function RoundRect(element, dynamic) {
if (element[expando] && collection[element[expando]]) {
throw new Error('Can’t round already rounded rectangles (use RoundRect.create)');
}
collection[element[expando] = (++uuid)] = this;
this.element = element;
this.onPropertyChangeProxy = proxy(this, function () {
var self = this,
property = window.event.propertyName;
setTimeout(function () {
self.onPropertyChange.call(self, property);
});
});
this.onStateChangeProxy = proxy(this, function () {
var self = this,
eventType = window.event.type;
setTimeout(function () {
self.onStateChange.call(self, eventType);
});
});
this.onVmlStateChangeProxy = proxy(this, function () {
// IE seems to sometimes ditch properties from the event object
// if we do not create references to them here before passing to
// the statechange function
var eventType = window.event.type;
this.onVmlStateChange(eventType);
});
this.events = {
element: {},
container: {}
};
ready(proxy(this, function () {
this.render();
if (dynamic) {
this.start();
}
}));
}
/**
* @type {string}
*/
RoundRect.expando = expando;
/**
* A hash map of nodeNames that will fail if we try to round them. What a
* bummer.
* @type {Object.<string, boolean>}
*/
RoundRect.disallowed = { BODY: true, TABLE: true, TR: true, TD: true, SELECT: !ie8, OPTION: true };
/**
* Creates a new RoundRect object, or returns the one that already exists
* in the collection for the given element. This is the preferred method of
* RoundRect instantiation.
* @param {Element} element
* @param {boolean} dynamic
* @return {RoundRect}
*/
RoundRect.create = function (element, dynamic) {
var id = element[expando], obj;
if (id && (obj = collection[id])) {
if (dynamic !== undefined && obj.dynamic !== dynamic) {
if (obj.dynamic) {
obj.start();
}
else {
obj.stop();
}
}
return obj;
}
return new RoundRect(element, dynamic);
};
/**
* Manually destroy references of all elements not currently in the DOM in
* order to allow IE to garbage collect and free memory. If you need to
* call this, you are failing to destroy RoundRect objects, which is bad!
* Call the destroy method on objects you remove instead, whenever
* possible.
*/
RoundRect.gc = function () {
for (var i in collection) {
if (collection.hasOwnProperty(i)) {
if (!collection[i].element.parentNode) {
collection[i].destroy();
}
}
}
};
/**
* Because sometimes faking hover events is necessary, we need to pull
* rules from stylesheets that contain :hover pseudo-elements and add some
* new rules to generate pseudo-classes.
*/
RoundRect.processStyleSheets = function () {
var i, j, k, l, sheet;
/**
* :hover -> ns + -hover
* @param {Object} sheet CSSStyleSheet, IE style.
*/
function processStyleSheet(sheet) {
var i, rule;
for (i = sheet.rules.length - 1; i >= 0; --i) {
rule = sheet.rules[i];
// Remove rules that were added previously, in case
// processStyleSheets is being executed to refresh them
if (rule.selectorText.indexOf('.' + ns + '-hover') !== -1) {
sheet.removeRule(i);
continue;
}
if (rule.selectorText.indexOf(':hover') !== -1) {
sheet.addRule(rule.selectorText.replace(/:hover/g, '.' + ns + '-hover'), rule.style.cssText, i + 1);
}
}
}
for (i = 0, j = document.styleSheets.length; i < j; ++i) {
try {
sheet = document.styleSheets[i];
if (sheet.imports) {
for (k = 0, l = sheet.imports.length; k < l; ++k) {
try {
processStyleSheet(sheet.imports[k]);
}
// ignore ‘Permission Denied’ errors
// with as little collateral damage as possible
catch (e) {}
}
}
processStyleSheet(sheet);
}
// ignore ‘Permission Denied’ errors
catch (e) {}
}
};
/**
* Search the DOM for any elements that should have rounded rectangles
* (based on CSS rules) and apply them.
* @param {boolean=} dynamic Whether to watch for changed styles. Defaults
* to TRUE.
* @param {boolean=} watchAll If TRUE, even elements that don’t have
* border-radius right now will be watched for changes. dynamic must also
* be TRUE, or this will do nothing.
*/
RoundRect.run = function (dynamic, watchAll) {
if (dynamic === undefined) {
dynamic = true;
}
if (dynamic) {
RoundRect.processStyleSheets();
}
ready(function () {
var elements = document.getElementsByTagName('*'),
i, e, sucks, cs, tagName, nsUpper = ns.toUpperCase();
// NodeLists are live; when elements are added, the length changes,
// so don’t you dare try to optimize this loop unless you want to
// break stuff
for (i = 0; i < elements.length; ++i) {
e = elements[i];
cs = e.currentStyle;
tagName = e.nodeName.toUpperCase();
// Skip non-Element, RoundRect, VML, and disallowed elements
if (e.nodeType !== 1 || tagName === nsUpper || RoundRect.disallowed[tagName] || e.scopeName === xmlns) {
continue;
}
if ((cs[br] || cs[btl] || cs[btr] || cs[bbr] || cs[bbl]) !== undefined || (dynamic && watchAll)) {
RoundRect.create(e, dynamic);
}
}
});
};
RoundRect.prototype = {
/**
* The element referenced by this object.
* @type {Element}
* @private
*/
element: undefined,
/**
* A VML container for the VML content. What could be better?!
* @type {?}
* @private
*/
container: undefined,
/**
* Cached values for the element’s width, height, top, and left offset.
* @type {Object.<string, *>}
* @private
*/
dimensions: undefined,
/**
* Caches values for the four border-radius values, starting from the
* top-left.
* @type {?Array.<number>}
*/
radii: null,
/**
* Cached values for the top, right, bottom, and left border widths.
* @type {Object.<string, *>}
* @private
*/
borderWidths: undefined,
/**
* VML elements used to draw the border and background.
* @type {Object.<string, Element>}
*/
vml: undefined,
/**
* Whether or not the element responds to dynamic property updates.
* @type {boolean}
*/
dynamic: false,
/**
* The URL of the background image of the element.
* @type {?string}
*/
backgroundImage: null,
/**
* @type {Object.<string, string>}
* @private
*/
originalStyles: undefined,
/**
* References to event handlers for this element and its container.
* Required in order to prevent memory leaks.
* Defined in the constructor.
* @type {Object.<string, Object.<string, Array.<Function>>>}
* @private
*/
events: undefined,
/**
* Adds events to DOM elements in a manner such that they can be safely
* removed for garbage collection, since IE is incapable of doing this
* on its own.
* @param {string} elementType Either ‘element’ or ‘container’,
* depending upon which we are adding an event to.
* @param {string} eventType The type of event, excluding ‘on’.
* @param {Function} fn The event handler.
*/
addEvent: function (elementType, eventType, fn) {
if (!this.events[elementType][eventType]) {
this.events[elementType][eventType] = [ fn ];
}
else {
this.events[elementType][eventType].push(fn);
}
this[elementType].attachEvent('on' + eventType, fn);
},
/**
* Removes events from DOM elements in a manner such that they can be
* safely garbage collected, since IE is incapable of doing this on its
* own.
* @param {string=} element Either ‘element’ or ‘container’. If
* undefined, all events will be removed.
* @param {string=} event The type of event, excluding ‘on’. If
* undefined, all events for the specified elementType will be removed.
* @param {Function=} fn The function to unbind. If undefined, all
* events for the specified eventType will be removed.
*/
removeEvent: function (element, event, fn) {
var elementTypes, eventTypes, elementEvents,
elementType, eventType, i, j;
if (element !== undefined) {
elementTypes = {};
elementTypes[element] = 1;
}
else {
elementTypes = this.events;
}
for (elementType in elementTypes) {
if (elementTypes.hasOwnProperty(elementType)) {
if (event !== undefined) {
eventTypes = {};
eventTypes[event] = 1;
}
else {
eventTypes = this.events[elementType];
}
for (eventType in eventTypes) {
if (eventTypes.hasOwnProperty(eventType)) {
elementEvents = this.events[elementType][eventType];
for (i = 0, j = elementEvents.length; i < j; ++i) {
if (elementEvents[i] === fn) {
this[elementType].detachEvent('on' + eventType, elementEvents.splice(i, 1)[0]);
return;
}
else if (fn === undefined) {
this[elementType].detachEvent('on' + eventType, elementEvents[i]);
}
}
if (fn === undefined) {
delete this.events[elementType][eventType];
}
}
}
}
}
},
/**
* Breaks references to the DOM to allow Microsoft’s crap GC to GC.
* (I am not entirely sure how many of this is actually necessary,
* since 1. who cares about IE6, 2. sIEve is actually incredibly
* unreliable at determining leaks, and 3. leaks are fixed in IE8
* and will get collected when navigating to another page in IE7.
* Expert advice is appreciated.)
* @param {boolean=} restoreStyles Whether or not to restore inline
* styles from when the element was first run through RoundRect.
*/
destroy: function (restoreStyles) {
var id = this.element[expando], i;
this.removeEvent();
this.element.removeAttribute(expando);
if (this.vml) {
for (i in this.vml) {
if (this.vml.hasOwnProperty(i)) {
this.vml[i].filler = null;
this.vml[i] = null;
}
}
}
if (this.container) {
if (this.container && this.container.parentNode) {
this.container.parentNode.removeChild(this.container);
}
}
if (restoreStyles && this.originalStyles) {
for (i in this.originalStyles) {
if (this.originalStyles.hasOwnProperty(i)) {
this.element.style[i] = this.originalStyles[i];
}
}
this.element.parentNode.style.width = '';
}
this.container = null;
this.element = null;
delete collection[id];
},
/**
* Start watching for dynamic property changes and events.
*/
start: function () {
if (!this.dynamic) {
this.modifyEvents(true);
this.dynamic = true;
}
},
/**
* Stop watching for dynamic property changes and events.
*/
stop: function () {
if (this.dynamic) {
this.modifyEvents(false);
this.dynamic = false;
}
},
/**
* Modifies the event listeners on the element.
* @param {boolean} append
* @private
*/
modifyEvents: function (append) {
var e = 'element',
c = 'container',
scp = this.onStateChangeProxy,
vcp = this.onVmlStateChangeProxy,
method = append ? 'addEvent' : 'removeEvent';
this[method](e, 'propertychange', this.onPropertyChangeProxy);
// events that may have corresponding changes within stylesheets
this[method](e, 'mouseenter', scp);
this[method](e, 'mouseleave', scp);
this[method](e, 'focus', scp);
this[method](e, 'blur', scp);
// onresize fires whenever the original element is resized
this[method](e, 'resize', scp);
// move fires whenever the original element changes positions
this[method](e, 'move', scp);
this[method](c, 'mouseover', vcp);
this[method](c, 'mouseout', vcp);
this[method](c, 'click', vcp);
},
/**
* Add a class to the element referenced by this RoundRect object.
* @type {string} className
*/
addClass: function (className) {
var oldClassName = ' ' + this.element.className + ' ';
if (oldClassName.indexOf(' ' + className + ' ') === -1) {
this.element.className += ' ' + className;
}
},
/**
* Remove a class from the element referenced by this RoundRect object.
* @type {string} className
*/
removeClass: function (className) {
var oldClassName = ' ' + this.element.className + ' ';
if (oldClassName.indexOf(' ' + className + ' ') !== -1) {
this.element.className = oldClassName.replace(' ' + ns + '-hover ', ' ').replace(/^\s+|\s+$/g, '');
}
},
/**
* Proxy for onVmlStateChange, binds ‘this’. Defined in the
* constructor.
* @type {Function}
* @private
*/
onVmlStateChangeProxy: undefined,
/**
* Attaches a -hover class to the element when its VML container is
* hovered over, since the mouse passes right through any areas of the
* rounded element that aren’t taken up by child elements.
* @param {string} eventType
*/
onVmlStateChange: function (eventType) {
if (eventType === 'click' && isTextField(this.element)) {
// With RoundRect applied, text inputs can only be
// clicked on where text has already been written. This
// partially works around this issue. It is not perfect:
// clicking empty lines in textareas, for instance, puts the
// carat in the wrong place, but it works in most common cases
// and is much better than the default behaviour.
var range = this.element.createTextRange();
range.moveStart('textedit');
range.select();
}
else if (eventType === 'click' && isButton(this.element)) {
// Much like text fields, clicking on the VML part of a button
// will not trigger the button click
this.element.click();
}
else if (eventType === 'click'
&& document.activeElement !== this.element
&& document.activeElement !== document.body) {
document.activeElement.blur();
}
else if (eventType === 'mouseover') {
this.addClass(hoverClass);
}
else if (eventType === 'mouseout') {
this.removeClass(hoverClass);
}
},
/**
* Proxy for onPropertyChange, binds ‘this’ and implements a timeout
* when necessary. Defined in the constructor.
* @type {Function}
* @private
*/
onPropertyChangeProxy: undefined,
/**
* Adjusts the VML in response to changes to the DOM to the original
* element.
* @param {string} property The name of the property that changed.
* @private
*/
onPropertyChange: function (property) {
var es = this.element.style,
cs = this.container.style;
switch (property) {
case 'style.display':
cs.display = (es.display === 'none') ? 'none' : 'block';
// fall through
case 'style':
case 'className':
case 'style.cssText':
this.dimensions = this.calculateDimensions();
// fall through
case 'style.border':
case 'style.borderTop':
case 'style.borderRight':
case 'style.borderBottom':
case 'style.borderLeft':
case 'style.borderTopWidth':
case 'style.borderRightWidth':
case 'style.borderBottomWidth':
case 'style.borderLeftWidth':
case 'style.borderWidth':
this.borderWidths = this.calculateBorderWidths();
// fall through
case 'style.border-radius':
case 'style.border-top-left-radius':
case 'style.border-top-right-radius':
case 'style.border-bottom-right-radius':
case 'style.border-bottom-left-radius':
this.radii = this.calculateRadii();
// fall through
case 'style.padding':
case 'style.background':
case 'style.backgroundImage':
case 'style.backgroundColor':
case 'style.backgroundPosition':
case 'style.backgroundRepeat':
this.applyVML();
break;
case 'style.borderColor':
this.vmlStrokeColor();
break;
case 'style.visibility':
cs.visibility = es.visibility;
break;
case 'style.filter':
this.vmlOpacity();
break;
case 'style.zIndex':
cs.zIndex = es.zIndex;
break;
}
},
/**
* Proxy for onStateChange, binds ‘this’ and implements a timeout.
* Defined in the constructor.
* @type {Function}
* @private
*/
onStateChangeProxy: undefined,
/**
* Reapplies VML styles in response to state change event, such as a
* mouseover.
* @param {string} eventType
* @private
*/
onStateChange: function (eventType) {
if (eventType === 'resize' || eventType === 'move') {
var oldDimensions = this.dimensions;
this.dimensions = this.calculateDimensions();
if (this.dimensions.width !== oldDimensions.width
|| this.dimensions.height !== oldDimensions.height
|| this.dimensions.top !== oldDimensions.top
|| this.dimensions.left !== oldDimensions.left) {
this.vmlOffsets();
this.vmlPath();
}
}
else {
// Buttons always fail to change their hover states properly;
// though maybe it is just because it is really slow?
// TODO: Borders width/colour doesn’t seem to update properly
// even with this change for some reason.
if (isButton(this.element)) {
if (eventType === 'mouseenter') {
this.addClass(hoverClass);
}
else if (eventType === 'mouseleave') {
this.removeClass(hoverClass);
}
}
this.element.runtimeStyle.cssText = '';
this.dimensions = this.calculateDimensions();
this.borderWidths = this.calculateBorderWidths();
this.radii = this.calculateRadii();
this.applyVML();
}
},
/**
* Calculates the appropriate radii for all corners of an element.
* @return {?Array.<number>}
* @private
*/
calculateRadii: function () {
var e = this.element,
cs = e.currentStyle,
defaultRadius = cs[br] || '0 0 0 0',
radii,
i;
if ((cs[br] || cs[btl] || cs[btr] || cs[bbr] || cs[bbl]) === undefined) {
// No border radius set
return null;
}
// The first split gets rid of any vertical radii, which are not
// supported. We also assume in a really naïve manner that we are
// always dealing with pixels. Pixels pixels pixels pixels pixels.
radii = defaultRadius.split(/\s+\//)[0].replace(/[^0-9\s]/g, '').split(/\s+/);
radii[0] = (cs[btl] || '').replace(/[^0-9]/g, '') || radii[0];
radii[1] = (cs[btr] || '').replace(/[^0-9]/g, '') || radii[1];
radii[2] = (cs[bbr] || '').replace(/[^0-9]/g, '') || radii[2];
radii[3] = (cs[bbl] || '').replace(/[^0-9]/g, '') || radii[3];
// Normalize as per the css3 spec so we always have four radii
for (i = 0; i < 4; ++i) {
radii[i] = radii[i] === undefined
? (+radii[Math.max((i - 2), 0)])
: (+radii[i]);
}
// Make sure we aren’t drawing zero-radiuses because
// someone decided to try to be clever and set everything to 0
// in CSS
if (radii[0] + radii[1] + radii[2] + radii[3] === 0) {
return null;
}
return radii;
},
/**
* Calculates the width, height, top, and left offset of the element.
* @return {Object.<string, number>} Object with four keys: width,
* height, top, left.
* @private
*/
calculateDimensions: function () {
return {
width: this.element.offsetWidth,
height: this.element.offsetHeight,
left: this.element.offsetLeft,
top: this.element.offsetTop
};