YannBergonzat

Slides Fixer

Feb 27th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getIEVersion() {
  2.     var rv = -1;
  3.     if(navigator.appName == 'Microsoft Internet Explorer') {
  4.         var ua = navigator.userAgent;
  5.         var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  6.         if (re.exec(ua) != null)
  7.         rv = parseFloat( RegExp.$1 );
  8.     }
  9.    
  10.     return rv;
  11. }
  12.  
  13. function IESlidesFixer() {
  14.     if(getIEVersion() >= 9) {
  15.         var input = document.getElementsByTagName('input');
  16.        
  17.         for(var i=0;i<input.length;i++) {
  18.             if(input[i].getAttribute('type') == 'range') {
  19.                 var range = input[i];
  20.                 var c = range.parentNode;
  21.                 var body = document.getElementsByTagName('body')[0];
  22.                
  23.                 var hr = document.createElement('hr');
  24.                 var thumb = document.createElement('div');
  25.                
  26.                 var rangeMax = range.getAttribute('max') || 100;
  27.                 var rangeMin = range.getAttribute('min') || 0;
  28.                 var rangeStep = range.getAttribute('step') || 0.001;
  29.                 var rangeValue = range.value || parseFloat(rangeMin) + parseFloat((rangeMax - rangeMin) / 2);
  30.                 var rangePaddingLeft = range.style.paddingLeft || 0;
  31.                 var rangeMarginLeft = range.style.marginLeft || 0;
  32.                 var rangePaddingTop = range.style.paddingTop || 0;
  33.                 var rangeMarginTop = range.style.marginTop || 0;
  34.                
  35.                 if(rangeValue < rangeMin) rangeValue = min;
  36.                 else if(rangeValue > rangeMax) rangeValue = rangeMin + ~~((rangeMax - rangeMin) / rangeStep) * rangeStep;
  37.                
  38.                 range.value = rangeValue;
  39.                
  40.                 var bodyPaddingLeft = body.style.paddingLeft || 0;
  41.                 var bodyMarginLeft = body.style.marginLeft || 0;
  42.                 var bodyPaddingTop = body.style.paddingTop || 0;
  43.                 var bodyMarginTop = body.style.marginTop || 0;
  44.                
  45.                 hr.style.width= range.style.width || "130px";
  46.                 hr.style.display="inline-block";
  47.                 hr.style.position="relative";
  48.                 hr.style.marginBottom="5px";
  49.                 hr.style.textAlign="right";
  50.                
  51.                 var posX = range.offsetLeft + rangePaddingLeft + rangeMarginLeft + bodyPaddingLeft + bodyMarginLeft;
  52.                 var posY = range.offsetTop + rangePaddingTop + rangeMarginTop + bodyPaddingTop + bodyMarginTop;
  53.                
  54.                 thumb.className="thumb";
  55.                 thumb.style.position="absolute";
  56.                 thumb.style.width="5px";
  57.                 thumb.style.height="20px";
  58.                 thumb.style.background="#D6D6D6";
  59.                 thumb.style.border="1px solid #707070";
  60.                 thumb.style.borderRadius="3px 3px 5px 5px / 3px 3px 25px 25px";
  61.                 thumb.style.boxShadow="0 10px 2px #EEEEEE inset, 1px 1px 2px #AAAAAA";
  62.                 thumb.style.left = (((rangeValue - rangeMin) / (rangeMax - rangeMin)) * parseFloat(hr.style.width)) + posX + parseFloat(thumb.style.width) / 2 + 'px';
  63.                 thumb.style.top = posY + "px";
  64.                 thumb.setAttribute('data-clicked', 'false');
  65.                 thumb.setAttribute('data-posx', posX);
  66.                 thumb.setAttribute('data-posy', posY);
  67.                 thumb.setAttribute('data-rangepx', parseFloat(hr.style.width));
  68.                 thumb.setAttribute('data-max', rangeMax);
  69.                 thumb.setAttribute('data-min', rangeMin);
  70.                 thumb.setAttribute('data-step', rangeStep);
  71.                 thumb.setAttribute('data-rangeid', 'range'+i);
  72.                 thumb.setAttribute('data-focused', 'false');
  73.                
  74.                 thumb.onmouseover = function(e) {
  75.                     this.setAttribute('data-focused', 'true');
  76.                    
  77.                     if(this.getAttribute('data-clicked') == 'false') {
  78.                         this.style.background="#B6E2FB";
  79.                         this.style.border="1px solid #3C7FB1";
  80.                         this.style.boxShadow="0 10px 2px #DEF2FC inset, 1px 1px 2px #AAAAAA";
  81.                     }
  82.                    
  83.                     if(range.onmouseover()) range.onmouseover(e);
  84.                 };
  85.                
  86.                 thumb.onmouseout = function(e) {
  87.                     if(this.getAttribute('data-clicked') == 'false') {         
  88.                         this.style.background="#D6D6D6";
  89.                         this.style.border="1px solid #707070";
  90.                         this.style.boxShadow="0 10px 2px #EEEEEE inset, 1px 1px 2px #AAAAAA";
  91.                     }
  92.                    
  93.                     if(range.onmouseout()) range.onmouseout(e);
  94.                 };
  95.                
  96.                 thumb.onmousedown = function(e) {
  97.                     this.style.background="#63B3DE";
  98.                     this.style.border="1px solid #2C628B";
  99.                     this.style.boxShadow="0 10px 2px #A6D9F4 inset, 1px 1px 2px #AAAAAA";
  100.                    
  101.                     this.setAttribute('data-clicked', e.clientX);
  102.                    
  103.                     if(range.onmousedown()) range.onmousedown(e);
  104.                 };
  105.                
  106.                 thumb.onmouseup = function(e) {
  107.                     this.style.background="#B6E2FB";
  108.                     this.style.border="1px solid #3C7FB1";
  109.                     this.style.boxShadow="0 10px 2px #DEF2FC inset, 1px 1px 2px #AAAAAA";
  110.                    
  111.                     this.setAttribute('data-clicked', 'false');
  112.                    
  113.                     if(range.onmouseup()) range.onmouseup(e);
  114.                 };
  115.                
  116.                 thumb.onclick = function(e) {
  117.                     if(range.onclick()) range.onclick(e);
  118.                 };
  119.                
  120.                 thumb.onfocus = function(e) {
  121.                     this.setAttribute('data-focused', 'true');
  122.                                
  123.                     if(range.onfocus()) range.onfocus(e);
  124.                 };
  125.                
  126.                 thumb.onblur = function(e) {
  127.                     this.setAttribute('data-focused', 'false');
  128.                     this.style.background="#D6D6D6";
  129.                     this.style.border="1px solid #707070";
  130.                     this.style.boxShadow="0 10px 2px #EEEEEE inset, 1px 1px 2px #AAAAAA";
  131.                    
  132.                     this.setAttribute('data-clicked', 'false');
  133.                    
  134.                     if(range.onblur()) range.onblur(e);
  135.                 };
  136.                
  137.                 hr.onmousedown = function(e) {
  138.                     var thumb       = this.nextSibling;
  139.                     var range       = document.getElementById(thumb.getAttribute('data-rangeid'));
  140.                    
  141.                     thumb.style.background="#63B3DE";
  142.                     thumb.style.border="1px solid #2C628B";
  143.                     thumb.style.boxShadow="0 10px 2px #A6D9F4 inset, 1px 1px 2px #AAAAAA";
  144.                    
  145.                     thumb.setAttribute('data-clicked', e.clientX);
  146.                    
  147.                     var posX        = parseFloat(thumb.getAttribute('data-posx'));
  148.                     var w           = parseFloat(thumb.getAttribute('data-rangepx'));
  149.                     var max         = parseFloat(thumb.getAttribute('data-max'));
  150.                     var min         = parseFloat(thumb.getAttribute('data-min'));
  151.                     var step        = parseFloat(thumb.getAttribute('data-step'));
  152.                     var rangeVal    = max - min;
  153.                    
  154.                     var x = (e.clientX - posX) - parseFloat(thumb.style.width);
  155.                     var value = ((x / w) * max) + min;
  156.                    
  157.                     value = Math.round((value - min) / step) * step + min;
  158.                     if(value < min) {
  159.                         value = min;
  160.                     }
  161.                     else if(value > max) {
  162.                         value = min + ~~(rangeVal / step) * step;
  163.                     }
  164.                    
  165.                     thumb.style.left = (((value - min) / max) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
  166.                    
  167.                     thumb.setAttribute('data-clicked', e.clientX);
  168.                    
  169.                     range.value = value;
  170.                    
  171.                     if(range.onchange()) range.onchange(e);
  172.                     if(range.onmousedown()) range.onmousedown(e);
  173.                 }
  174.                
  175.                 range.setAttribute('type', 'hidden');
  176.                 range.setAttribute('id', ((range.getAttribute('id')) ? range.getAttribute('id') + ' ' : '') + 'range'+i);
  177.                
  178.                 c.insertBefore(hr, range);
  179.                 c.insertBefore(thumb, range);
  180.             }
  181.         }
  182.        
  183.         document.onmousemove = function(e) {
  184.             var thumbs = document.getElementsByClassName('thumb');
  185.            
  186.             for(var i=0;i<thumbs.length;i++) {
  187.                 if(thumbs[i].getAttribute('data-clicked') != 'false') {
  188.                     var thumb       = thumbs[i];
  189.                     var pos_init    = thumb.getAttribute('data-clicked');
  190.                     var posX        = parseFloat(thumb.getAttribute('data-posx'));
  191.                     var posY        = parseFloat(thumb.getAttribute('data-posy'));
  192.                     var w           = parseFloat(thumb.getAttribute('data-rangepx'));
  193.                     var max         = parseFloat(thumb.getAttribute('data-max'));
  194.                     var min         = parseFloat(thumb.getAttribute('data-min'));
  195.                     var step        = parseFloat(thumb.getAttribute('data-step'));
  196.                     var rangeVal    = max - min;
  197.                     var range       = document.getElementById(thumb.getAttribute('data-rangeid'));
  198.                    
  199.                     if(e.clientX > pos_init) {
  200.                         if(range.value >= max) return;
  201.                        
  202.                         var x = (e.clientX - posX) - parseFloat(thumb.style.width);
  203.                         var value = ((x / w) * (max - min)) + min;
  204.                        
  205.                         value = Math.round((value - min) / step) * step + min;
  206.                         if(value < min) {
  207.                             value = min;
  208.                         }
  209.                         else if(value > max) {
  210.                             value = min + ~~(rangeVal / step) * step;
  211.                         }
  212.                        
  213.                         thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
  214.                        
  215.                         thumb.setAttribute('data-clicked', e.clientX);
  216.                        
  217.                         range.value = value;
  218.                         if(range.onchange()) range.onchange(e);
  219.                     }
  220.                     else if(e.clientX < pos_init) {
  221.                         if(range.value <= min) return;
  222.                        
  223.                         var x = (e.clientX - posX) - parseFloat(thumb.style.width);
  224.                         var value = ((x / w) * (max - min)) + min;
  225.                        
  226.                         value = Math.round((value - min) / step) * step + min;
  227.                         if(value < min) {
  228.                             value = min;
  229.                         }
  230.                         else if(value > max) {
  231.                             value = min + ~~(rangeVal / step) * step;
  232.                         }
  233.                        
  234.                         thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
  235.                        
  236.                         thumb.setAttribute('data-clicked', e.clientX);
  237.                        
  238.                         range.value = value;
  239.                         if(range.onchange()) range.onchange(e);
  240.                     }
  241.                     else return;
  242.                 }
  243.             }
  244.         };
  245.        
  246.         document.onmouseup = function(e) {
  247.             var thumbs = document.getElementsByClassName('thumb');
  248.            
  249.             for(var i=0;i<thumbs.length;i++) {
  250.                 if(thumbs[i].getAttribute('data-clicked') != 'false') {
  251.                     thumbs[i].setAttribute('data-clicked', 'false');
  252.                     thumbs[i].onblur(e);
  253.                 }
  254.             }
  255.         };
  256.        
  257.         window.onkeydown = function(e) {
  258.             if(e.keyCode == 39) {
  259.                 var thumbs = document.getElementsByClassName('thumb');
  260.                
  261.                 for(var i=0;i<thumbs.length;i++) {
  262.                     if(thumbs[i].getAttribute('data-focused').toString() == 'true') {
  263.                         var thumb       = thumbs[i];
  264.                         var range       = document.getElementById(thumb.getAttribute('data-rangeid'));
  265.                        
  266.                         var posX        = parseFloat(thumb.getAttribute('data-posx'));
  267.                         var w           = parseFloat(thumb.getAttribute('data-rangepx'));
  268.                         var max         = parseFloat(thumb.getAttribute('data-max'));
  269.                         var min         = parseFloat(thumb.getAttribute('data-min'));
  270.                         var step        = parseFloat(thumb.getAttribute('data-step'));
  271.                         var rangeVal    = max - min;
  272.                        
  273.                         var value = (range.value != step) ? parseFloat(range.value) + parseFloat((step < 1) ? 1 : step)  : step * 2;
  274.                        
  275.                         if(value < min) {
  276.                             value = min;
  277.                         }
  278.                         else if(value > max) {
  279.                             value = min + ~~(rangeVal / step) * step;
  280.                         }
  281.                        
  282.                         thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
  283.                        
  284.                         thumb.setAttribute('data-clicked', e.clientX);
  285.                        
  286.                         range.value = value;
  287.                         if(range.onchange()) range.onchange(e);
  288.                     }
  289.                 }
  290.             }
  291.             else if(e.keyCode == 37) {
  292.                 var thumbs = document.getElementsByClassName('thumb');
  293.  
  294.                 for(var i=0;i<thumbs.length;i++) {
  295.                     if(thumbs[i].getAttribute('data-focused').toString() == 'true') {
  296.                         var thumb       = thumbs[i];
  297.                         var range       = document.getElementById(thumb.getAttribute('data-rangeid'));
  298.                        
  299.                         var posX        = parseFloat(thumb.getAttribute('data-posx'));
  300.                         var w           = parseFloat(thumb.getAttribute('data-rangepx'));
  301.                         var max         = parseFloat(thumb.getAttribute('data-max'));
  302.                         var min         = parseFloat(thumb.getAttribute('data-min'));
  303.                         var step        = parseFloat(thumb.getAttribute('data-step'));
  304.                         var rangeVal    = max - min;
  305.                        
  306.                         var value = (range.value != step) ? range.value - ((step < 1) ? 1 : step) : 0;
  307.                        
  308.                         if(value < min) {
  309.                             value = min;
  310.                         }
  311.                         else if(value > max) {
  312.                             value = min + ~~(rangeVal / step) * step;
  313.                         }
  314.                        
  315.                         thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
  316.                        
  317.                         thumb.setAttribute('data-clicked', e.clientX);
  318.                        
  319.                         range.value = value;
  320.                         if(range.onchange()) range.onchange(e);
  321.                     }
  322.                 }
  323.             }
  324.         };
  325.     }
  326. };
  327.  
  328. // Call IESlidesFixer() each time you add a slider using JavaScript !
  329. document.addEventListener('DOMContentLoaded', IESlidesFixer, false);
  330.  
  331. /*
  332. html5slider - a JS implementation of <input type=range> for Firefox 16 and up
  333. https://github.com/fryn/html5slider
  334.  
  335. Copyright (c) 2010-2012 Frank Yan, <http://frankyan.com>
  336.  
  337. Permission is hereby granted, free of charge, to any person obtaining a copy
  338. of this software and associated documentation files (the "Software"), to deal
  339. in the Software without restriction, including without limitation the rights
  340. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  341. copies of the Software, and to permit persons to whom the Software is
  342. furnished to do so, subject to the following conditions:
  343.  
  344. The above copyright notice and this permission notice shall be included in
  345. all copies or substantial portions of the Software.
  346.  
  347. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  348. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  349. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  350. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  351. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  352. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  353. THE SOFTWARE.
  354. */
  355.  
  356. (function() {
  357.  
  358. // test for native support
  359. var test = document.createElement('input');
  360. try {
  361.   test.type = 'range';
  362.   if (test.type == 'range')
  363.     return;
  364. } catch (e) {
  365.   return;
  366. }
  367.  
  368. // test for required property support
  369. test.style.background = 'linear-gradient(red, red)';
  370. if (!test.style.backgroundImage || !('MozAppearance' in test.style) ||
  371.     !document.mozSetImageElement || !this.MutationObserver)
  372.   return;
  373.  
  374. var scale;
  375. var isMac = navigator.platform == 'MacIntel';
  376. var thumb = {
  377.   radius: isMac ? 9 : 6,
  378.   width: isMac ? 22 : 12,
  379.   height: isMac ? 16 : 20
  380. };
  381. var track = 'linear-gradient(transparent ' + (isMac ?
  382.   '6px, #999 6px, #999 7px, #ccc 8px, #bbb 9px, #bbb 10px, transparent 10px' :
  383.   '9px, #999 9px, #bbb 10px, #fff 11px, transparent 11px') +
  384.   ', transparent)';
  385. var styles = {
  386.   'min-width': thumb.width + 'px',
  387.   'min-height': thumb.height + 'px',
  388.   'max-height': thumb.height + 'px',
  389.   padding: '0 0 ' + (isMac ? '2px' : '1px'),
  390.   border: 0,
  391.   'border-radius': 0,
  392.   cursor: 'default',
  393.   'text-indent': '-999999px' // -moz-user-select: none; breaks mouse capture
  394. };
  395. var options = {
  396.   attributes: true,
  397.   attributeFilter: ['min', 'max', 'step', 'value']
  398. };
  399. var forEach = Array.prototype.forEach;
  400. var onChange = document.createEvent('HTMLEvents');
  401. onChange.initEvent('change', true, false);
  402.  
  403. if (document.readyState == 'loading')
  404.   document.addEventListener('DOMContentLoaded', initialize, true);
  405. else
  406.   initialize();
  407.  
  408. function initialize() {
  409.   // create initial sliders
  410.   forEach.call(document.querySelectorAll('input[type=range]'), transform);
  411.   // create sliders on-the-fly
  412.   new MutationObserver(function(mutations) {
  413.     mutations.forEach(function(mutation) {
  414.       if (mutation.addedNodes)
  415.         forEach.call(mutation.addedNodes, function(node) {
  416.           check(node);
  417.           if (node.childElementCount)
  418.             forEach.call(node.querySelectorAll('input'), check);
  419.         });
  420.     });
  421.   }).observe(document, { childList: true, subtree: true });
  422. }
  423.  
  424. function check(input) {
  425.   if (input.localName == 'input' && input.type != 'range' &&
  426.       input.getAttribute('type') == 'range')
  427.     transform(input);
  428. }
  429.  
  430. function transform(slider) {
  431.  
  432.   var isValueSet, areAttrsSet, isChanged, isClick, prevValue, rawValue, prevX;
  433.   var min, max, step, range, value = slider.value;
  434.  
  435.   // lazily create shared slider affordance
  436.   if (!scale) {
  437.     scale = document.body.appendChild(document.createElement('hr'));
  438.     style(scale, {
  439.       '-moz-appearance': isMac ? 'scale-horizontal' : 'scalethumb-horizontal',
  440.       display: 'block',
  441.       visibility: 'visible',
  442.       opacity: 1,
  443.       position: 'fixed',
  444.       top: '-999999px'
  445.     });
  446.     document.mozSetImageElement('__sliderthumb__', scale);
  447.   }
  448.  
  449.   // reimplement value and type properties
  450.   var getValue = function() { return '' + value; };
  451.   var setValue = function setValue(val) {
  452.     value = '' + val;
  453.     isValueSet = true;
  454.     draw();
  455.     delete slider.value;
  456.     slider.value = value;
  457.     slider.__defineGetter__('value', getValue);
  458.     slider.__defineSetter__('value', setValue);
  459.   };
  460.   slider.__defineGetter__('value', getValue);
  461.   slider.__defineSetter__('value', setValue);
  462.   slider.__defineGetter__('type', function() { return 'range'; });
  463.  
  464.   // sync properties with attributes
  465.   ['min', 'max', 'step'].forEach(function(prop) {
  466.     if (slider.hasAttribute(prop))
  467.       areAttrsSet = true;
  468.     slider.__defineGetter__(prop, function() {
  469.       return this.hasAttribute(prop) ? this.getAttribute(prop) : '';
  470.     });
  471.     slider.__defineSetter__(prop, function(val) {
  472.       val === null ? this.removeAttribute(prop) : this.setAttribute(prop, val);
  473.     });
  474.   });
  475.  
  476.   // initialize slider
  477.   slider.readOnly = true;
  478.   style(slider, styles);
  479.   update();
  480.  
  481.   new MutationObserver(function(mutations) {
  482.     mutations.forEach(function(mutation) {
  483.       if (mutation.attributeName != 'value') {
  484.         update();
  485.         areAttrsSet = true;
  486.       }
  487.       // note that value attribute only sets initial value
  488.       else if (!isValueSet) {
  489.         value = slider.getAttribute('value');
  490.         draw();
  491.       }
  492.     });
  493.   }).observe(slider, options);
  494.  
  495.   slider.addEventListener('mousedown', onDragStart, true);
  496.   slider.addEventListener('keydown', onKeyDown, true);
  497.   slider.addEventListener('focus', onFocus, true);
  498.   slider.addEventListener('blur', onBlur, true);
  499.  
  500.   function onDragStart(e) {
  501.     isClick = true;
  502.     setTimeout(function() { isClick = false; }, 0);
  503.     if (e.button || !range)
  504.       return;
  505.     var width = parseFloat(getComputedStyle(this, 0).width);
  506.     var multiplier = (width - thumb.width) / range;
  507.     if (!multiplier)
  508.       return;
  509.     // distance between click and center of thumb
  510.     var dev = e.clientX - this.getBoundingClientRect().left - thumb.width / 2 -
  511.               (value - min) * multiplier;
  512.     // if click was not on thumb, move thumb to click location
  513.     if (Math.abs(dev) > thumb.radius) {
  514.       isChanged = true;
  515.       this.value -= -dev / multiplier;
  516.     }
  517.     rawValue = value;
  518.     prevX = e.clientX;
  519.     this.addEventListener('mousemove', onDrag, true);
  520.     this.addEventListener('mouseup', onDragEnd, true);
  521.   }
  522.  
  523.   function onDrag(e) {
  524.     var width = parseFloat(getComputedStyle(this, 0).width);
  525.     var multiplier = (width - thumb.width) / range;
  526.     if (!multiplier)
  527.       return;
  528.     rawValue += (e.clientX - prevX) / multiplier;
  529.     prevX = e.clientX;
  530.     isChanged = true;
  531.     this.value = rawValue;
  532.   }
  533.  
  534.   function onDragEnd() {
  535.     this.removeEventListener('mousemove', onDrag, true);
  536.     this.removeEventListener('mouseup', onDragEnd, true);
  537.   }
  538.  
  539.   function onKeyDown(e) {
  540.     if (e.keyCode > 36 && e.keyCode < 41) { // 37-40: left, up, right, down
  541.       onFocus.call(this);
  542.       isChanged = true;
  543.       this.value = value + (e.keyCode == 38 || e.keyCode == 39 ? step : -step);
  544.     }
  545.   }
  546.  
  547.   function onFocus() {
  548.     if (!isClick)
  549.       this.style.boxShadow = !isMac ? '0 0 0 2px #fb0' :
  550.         'inset 0 0 20px rgba(0,127,255,.1), 0 0 1px rgba(0,127,255,.4)';
  551.   }
  552.  
  553.   function onBlur() {
  554.     this.style.boxShadow = '';
  555.   }
  556.  
  557.   // determines whether value is valid number in attribute form
  558.   function isAttrNum(value) {
  559.     return !isNaN(value) && +value == parseFloat(value);
  560.   }
  561.  
  562.   // validates min, max, and step attributes and redraws
  563.   function update() {
  564.     min = isAttrNum(slider.min) ? +slider.min : 0;
  565.     max = isAttrNum(slider.max) ? +slider.max : 100;
  566.     if (max < min)
  567.       max = min > 100 ? min : 100;
  568.     step = isAttrNum(slider.step) && slider.step > 0 ? +slider.step : 1;
  569.     range = max - min;
  570.     draw(true);
  571.   }
  572.  
  573.   // recalculates value property
  574.   function calc() {
  575.     if (!isValueSet && !areAttrsSet)
  576.       value = slider.getAttribute('value');
  577.     if (!isAttrNum(value))
  578.       value = (min + max) / 2;;
  579.     // snap to step intervals (WebKit sometimes does not - bug?)
  580.     value = Math.round((value - min) / step) * step + min;
  581.     if (value < min)
  582.       value = min;
  583.     else if (value > max)
  584.       value = min + ~~(range / step) * step;
  585.   }
  586.  
  587.   // renders slider using CSS background ;)
  588.   function draw(attrsModified) {
  589.     calc();
  590.     if (isChanged && value != prevValue)
  591.       slider.dispatchEvent(onChange);
  592.     isChanged = false;
  593.     if (!attrsModified && value == prevValue)
  594.       return;
  595.     prevValue = value;
  596.     var position = range ? (value - min) / range * 100 : 0;
  597.     var bg = '-moz-element(#__sliderthumb__) ' + position + '% no-repeat, ';
  598.     style(slider, { background: bg + track });
  599.   }
  600.  
  601. }
  602.  
  603. function style(element, styles) {
  604.   for (var prop in styles)
  605.     element.style.setProperty(prop, styles[prop], 'important');
  606. }
  607.  
  608. })();
Advertisement
Add Comment
Please, Sign In to add comment