'use strict'; var cls = require('../lib/class') , d = require('../lib/dom') , defaultSettings = require('./default-setting') , EventManager = require('../lib/event-manager') , guid = require('../lib/guid') , h = require('../lib/helper'); var instances = {}; function Instance(element) { var i = this; i.settings = h.clone(defaultSettings); i.containerWidth = null; i.containerHeight = null; i.contentWidth = null; i.contentHeight = null; i.isRtl = d.css(element, 'direction') === "rtl"; i.isNegativeScroll = (function () { var originalScrollLeft = element.scrollLeft; var result = null; element.scrollLeft = -1; result = element.scrollLeft < 0; element.scrollLeft = originalScrollLeft; return result; })(); i.negativeScrollAdjustment = i.isNegativeScroll ? element.scrollWidth - element.clientWidth : 0; i.event = new EventManager(); i.ownerDocument = element.ownerDocument || document; function focus() { cls.add(element, 'ps-focus'); } function blur() { cls.remove(element, 'ps-focus'); } i.scrollbarXRail = d.appendTo(d.e('div', 'ps-scrollbar-x-rail'), element); i.scrollbarX = d.appendTo(d.e('div', 'ps-scrollbar-x'), i.scrollbarXRail); i.scrollbarX.setAttribute('tabindex', 0); i.event.bind(i.scrollbarX, 'focus', focus); i.event.bind(i.scrollbarX, 'blur', blur); i.scrollbarXActive = null; i.scrollbarXWidth = null; i.scrollbarXLeft = null; i.scrollbarXBottom = h.toInt(d.css(i.scrollbarXRail, 'bottom')); i.isScrollbarXUsingBottom = i.scrollbarXBottom === i.scrollbarXBottom; // !isNaN i.scrollbarXTop = i.isScrollbarXUsingBottom ? null : h.toInt(d.css(i.scrollbarXRail, 'top')); i.railBorderXWidth = h.toInt(d.css(i.scrollbarXRail, 'borderLeftWidth')) + h.toInt(d.css(i.scrollbarXRail, 'borderRightWidth')); // Set rail to display:block to calculate margins d.css(i.scrollbarXRail, 'display', 'block'); i.railXMarginWidth = h.toInt(d.css(i.scrollbarXRail, 'marginLeft')) + h.toInt(d.css(i.scrollbarXRail, 'marginRight')); d.css(i.scrollbarXRail, 'display', ''); i.railXWidth = null; i.railXRatio = null; i.scrollbarYRail = d.appendTo(d.e('div', 'ps-scrollbar-y-rail'), element); i.scrollbarY = d.appendTo(d.e('div', 'ps-scrollbar-y'), i.scrollbarYRail); i.scrollbarY.setAttribute('tabindex', 0); i.event.bind(i.scrollbarY, 'focus', focus); i.event.bind(i.scrollbarY, 'blur', blur); i.scrollbarYActive = null; i.scrollbarYHeight = null; i.scrollbarYTop = null; i.scrollbarYRight = h.toInt(d.css(i.scrollbarYRail, 'right')); i.isScrollbarYUsingRight = i.scrollbarYRight === i.scrollbarYRight; // !isNaN i.scrollbarYLeft = i.isScrollbarYUsingRight ? null : h.toInt(d.css(i.scrollbarYRail, 'left')); i.scrollbarYOuterWidth = i.isRtl ? h.outerWidth(i.scrollbarY) : null; i.railBorderYWidth = h.toInt(d.css(i.scrollbarYRail, 'borderTopWidth')) + h.toInt(d.css(i.scrollbarYRail, 'borderBottomWidth')); d.css(i.scrollbarYRail, 'display', 'block'); i.railYMarginHeight = h.toInt(d.css(i.scrollbarYRail, 'marginTop')) + h.toInt(d.css(i.scrollbarYRail, 'marginBottom')); d.css(i.scrollbarYRail, 'display', ''); i.railYHeight = null; i.railYRatio = null; } function getId(element) { if (typeof element.dataset === 'undefined') { return element.getAttribute('data-ps-id'); } else { return element.dataset.psId; } } function setId(element, id) { if (typeof element.dataset === 'undefined') { element.setAttribute('data-ps-id', id); } else { element.dataset.psId = id; } } function removeId(element) { if (typeof element.dataset === 'undefined') { element.removeAttribute('data-ps-id'); } else { delete element.dataset.psId; } } exports.add = function (element) { var newId = guid(); setId(element, newId); instances[newId] = new Instance(element); return instances[newId]; }; exports.remove = function (element) { delete instances[getId(element)]; removeId(element); }; exports.get = function (element) { return instances[getId(element)]; };