vendor/assets/javascripts/webshims/shims/forms-picker.js in webshims-rails-1.11.3.1 vs vendor/assets/javascripts/webshims/shims/forms-picker.js in webshims-rails-1.11.6

- old
+ new

@@ -1,5 +1,266 @@ +(function($){ + +(function () { +if($.event.special.mousewheel){return;} + var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'], + toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ? + ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'], + slice = Array.prototype.slice, + nullLowestDeltaTimeout, lowestDelta; + + if ( $.event.fixHooks ) { + for ( var i = toFix.length; i; ) { + $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks; + } + } + + $.event.special.mousewheel = { + version: '3.1.6', + + setup: function() { + if ( this.addEventListener ) { + for ( var i = toBind.length; i; ) { + this.addEventListener( toBind[--i], handler, false ); + } + } else { + this.onmousewheel = handler; + } + }, + + teardown: function() { + if ( this.removeEventListener ) { + for ( var i = toBind.length; i; ) { + this.removeEventListener( toBind[--i], handler, false ); + } + } else { + this.onmousewheel = null; + } + } + }; + + $.fn.extend({ + mousewheel: function(fn) { + return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel'); + }, + + unmousewheel: function(fn) { + return this.unbind('mousewheel', fn); + } + }); + + + function handler(event) { + var orgEvent = event || window.event, + args = slice.call(arguments, 1), + delta = 0, + deltaX = 0, + deltaY = 0, + absDelta = 0; + event = $.event.fix(orgEvent); + event.type = 'mousewheel'; + + // Old school scrollwheel delta + if ( 'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; } + if ( 'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; } + if ( 'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; } + if ( 'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; } + + // Firefox < 17 horizontal scrolling related to DOMMouseScroll event + if ( 'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) { + deltaX = deltaY * -1; + deltaY = 0; + } + + // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy + delta = deltaY === 0 ? deltaX : deltaY; + + // New school wheel delta (wheel event) + if ( 'deltaY' in orgEvent ) { + deltaY = orgEvent.deltaY * -1; + delta = deltaY; + } + if ( 'deltaX' in orgEvent ) { + deltaX = orgEvent.deltaX; + if ( deltaY === 0 ) { delta = deltaX * -1; } + } + + // No change actually happened, no reason to go any further + if ( deltaY === 0 && deltaX === 0 ) { return; } + + // Store lowest absolute delta to normalize the delta values + absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) ); + if ( !lowestDelta || absDelta < lowestDelta ) { + lowestDelta = absDelta; + } + + // Get a whole, normalized value for the deltas + delta = Math[ delta >= 1 ? 'floor' : 'ceil' ](delta / lowestDelta); + deltaX = Math[ deltaX >= 1 ? 'floor' : 'ceil' ](deltaX / lowestDelta); + deltaY = Math[ deltaY >= 1 ? 'floor' : 'ceil' ](deltaY / lowestDelta); + + // Add information to the event object + event.deltaX = deltaX; + event.deltaY = deltaY; + event.deltaFactor = lowestDelta; + + // Add event and delta to the front of the arguments + args.unshift(event, delta, deltaX, deltaY); + + // Clearout lowestDelta after sometime to better + // handle multiple device types that give different + // a different lowestDelta + // Ex: trackpad = 3 and mouse wheel = 120 + if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); } + nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200); + + return ($.event.dispatch || $.event.handle).apply(this, args); + } + + function nullLowestDelta() { + lowestDelta = null; + } + +})(); + + + +(function(){ +if($.event.special.mwheelIntent){return;} +var mwheelI = { + pos: [-260, -260] + }, + minDif = 3, + doc = document, + root = doc.documentElement, + body = doc.body, + longDelay, shortDelay +; +if(!body){ + $(function(){ + body = doc.body; + }); +} +function unsetPos(){ + if(this === mwheelI.elem){ + mwheelI.pos = [-260, -260]; + mwheelI.elem = false; + minDif = 3; + } +} + +$.event.special.mwheelIntent = { + setup: function(){ + var jElm = $(this).bind('mousewheel', $.event.special.mwheelIntent.handler); + if( this !== doc && this !== root && this !== body ){ + jElm.bind('mouseleave', unsetPos); + } + jElm = null; + return true; + }, + teardown: function(){ + $(this) + .unbind('mousewheel', $.event.special.mwheelIntent.handler) + .unbind('mouseleave', unsetPos) + ; + return true; + }, + handler: function(e, d){ + var pos = [e.clientX, e.clientY]; + if( this === mwheelI.elem || Math.abs(mwheelI.pos[0] - pos[0]) > minDif || Math.abs(mwheelI.pos[1] - pos[1]) > minDif ){ + mwheelI.elem = this; + mwheelI.pos = pos; + minDif = 250; + + clearTimeout(shortDelay); + shortDelay = setTimeout(function(){ + minDif = 10; + }, 200); + clearTimeout(longDelay); + longDelay = setTimeout(function(){ + minDif = 3; + }, 1500); + e = $.extend({}, e, {type: 'mwheelIntent'}); + return ($.event.dispatch || $.event.handle).apply(this, arguments); + } + } +}; +$.fn.extend({ + mwheelIntent: function(fn) { + return fn ? this.bind("mwheelIntent", fn) : this.trigger("mwheelIntent"); + }, + + unmwheelIntent: function(fn) { + return this.unbind("mwheelIntent", fn); + } +}); + +$(function(){ + body = doc.body; + //assume that document is always scrollable, doesn't hurt if not + $(doc).bind('mwheelIntent.mwheelIntentDefault', $.noop); +}); +})(); + + +(function(){ + if($.event.special.mousepress){return;} + var removeTimer = function(elem, full){ + var timer = elem.data('mousepresstimer'); + if(timer){ + clearTimeout(timer); + } + if(full){ + elem.off('mouseup.mousepressext mouseleave.mousepressext'); + } + elem = null; + }; + $.event.special.mousepress = { + setup: function(){ + var timer; + $(this).on('mousedown.mousepressext', function(e){ + var elem = $(this); + + var startIntervall = function(delay){ + var steps = 0; + removeTimer(elem); + elem.data('mousepresstimer', setInterval(function(){ + $.event.special.mousepress.handler(elem[0], e); + steps++; + if(steps > 3 && delay > 45){ + startIntervall(delay - 40); + } + }, delay)); + }; + var target = $(e.target).trigger('mousepressstart', [e]); + + removeTimer(elem); + elem.data('mousepresstimer', setTimeout(function(){ + startIntervall(180); + }, 200)); + + elem.on('mouseup.mousepressext mouseleave.mousepressext', function(e){ + removeTimer(elem, true); + target.trigger('mousepressend', [e]); + elem = null; + target = null; + }); + }); + }, + teardown: function(){ + removeTimer($(this).off('.mousepressext'), true); + }, + handler: function(elem, e){ + return $.event.dispatch.call(elem, {type: 'mousepress', target: e.target, pageX: e.pageX, pageY: e.pageY}); + } + }; + +})(); + +})(webshims.$); + + webshims.register('forms-picker', function($, webshims, window, document, undefined, options){ "use strict"; var picker = webshims.picker; var actions = picker._actions; var moduleOpts = options; @@ -56,21 +317,356 @@ } this.setFocus(this.activeButton, this.opts.noFocus); }; var formcfg = webshims.formcfg; - var curCfg = formcfg[$.webshims.activeLang()] || formcfg['']; - $.webshims.activeLang({ - register: 'form-core', - callback: function(){ - $.each(arguments, function(i, val){ - if(formcfg[val]){ - curCfg = formcfg[val]; - return false; + var curCfg = formcfg.__active || formcfg['']; + var stopPropagation = function(e){ + e.stopImmediatePropagation(); + }; + var steps = options.steps; + + var mousePress = function(e){ + $(this)[e.type == 'mousepressstart' ? 'addClass' : 'removeClass']('mousepress-ui'); + }; + + + var widgetProtos = { + _addBindings: function(){ + var isFocused; + + var that = this; + var o = this.options; + + var eventTimer = (function(){ + var events = {}; + return { + init: function(name, curVal, fn){ + if (!events[name]) { + events[name] = { + fn: fn + }; + $(that.orig).on(name, function(){ + events[name].val = $.prop(that.orig, 'value'); + }); + } + events[name].val = curVal; + }, + call: function(name, val){ + if (events[name] && events[name].val != val) { + clearTimeout(events[name].timer); + events[name].val = val; + events[name].timer = setTimeout(function(){ + events[name].fn(val, that); + }, 9); + } + } + }; + })(); + var initChangeEvents = function(){ + eventTimer.init('input', $.prop(that.orig, 'value'), that.options.input); + eventTimer.init('change', $.prop(that.orig, 'value'), that.options.change); + }; + + var step = {}; + + var preventBlur = function(e){ + if (preventBlur.prevent) { + e.preventDefault(); + $(isFocused || that.element.getShadowFocusElement()).trigger('focus'); + stopPropagation(e); + return true; } + }; + var callSplitChange = (function(){ + var timer; + + var call = function(e){ + var val; + clearTimeout(timer); + val = that.parseValue(); + if (that.type == 'color') { + that.inputElements.val(val); + } + $.prop(that.orig, 'value', val); + eventTimer.call('input', val); + if (!e || e.type != 'wsupdatevalue') { + eventTimer.call('change', val); + } + }; + + var onFocus = function(){ + clearTimeout(timer); + }; + var onBlur = function(e){ + clearTimeout(timer); + timer = setTimeout(call, 0); + + if (e.type == 'ws__change') { + stopPropagation(e); + if (!o.splitInput) { + call(); + } + } + }; + + that.element.on('wsupdatevalue', call); + + that.inputElements.add(that.buttonWrapper).add(that.element).on({ + 'ws__focusin': onFocus, + 'ws__blur ws__focusout ws__change': onBlur + }); + setTimeout(function(){ + if (that.popover) { + that.popover.element.on('wspopoverhide', onBlur); + that.popover.element.children().on({ + 'focusin': onFocus, + 'focusout': onBlur + }); + } + }, 0); + })(); + + var spinEvents = {}; + var spinElement = o.splitInput ? this.inputElements.filter('.ws-spin') : this.inputElements.eq(0); + var elementEvts = { + ws__blur: function(e){ + if (!preventBlur(e) && !o.disabled && !o.readonly) { + if (!preventBlur.prevent) { + isFocused = false; + } + } + stopPropagation(e); + }, + ws__focus: function(e){ + if (!isFocused) { + initChangeEvents(); + isFocused = this; + } + }, + keypress: function(e){ + if (e.isDefaultPrevented()) { + return; + } + var chr; + var stepped = true; + var code = e.keyCode; + if (!e.ctrlKey && !e.metaKey && curCfg[that.type + 'Signs']) { + chr = String.fromCharCode(e.charCode == null ? code : e.charCode); + stepped = !(chr < " " || (curCfg[that.type + 'Signs'] + '0123456789').indexOf(chr) > -1); + } + else { + stepped = false; + } + if (stepped) { + e.preventDefault(); + } + }, + ws__input: (this.type == 'color' && this.isValid) ? $.noop : (function(){ + var timer; + var check = function(){ + var val = that.parseValue(true); + if (val && that.isValid(val)) { + that.setInput(val); + } + + }; + return function(){ + clearTimeout(timer); + timer = setTimeout(check, 200); + }; + })(), + 'ws__input keydown keypress': (function(){ + var timer; + var isStopped = false; + var releaseTab = function(){ + if (isStopped === true) { + isStopped = 'semi'; + timer = setTimeout(releaseTab, 250); + } + else { + isStopped = false; + } + }; + var stopTab = function(){ + isStopped = true; + clearTimeout(timer); + timer = setTimeout(releaseTab, 300); + }; + var select = function(){ + var elem = this; + setTimeout(function(){ + elem.focus(); + elem.select(); + }, 4); + + stopTab(); + }; + + return function(e){ + if (o.splitInput && o.jumpInputs) { + if (e.type == 'ws__input') { + if ($.prop(this, 'value').length === $.prop(this, 'maxLength')) { + try { + $(this).next().next('input, select').each(select); + } + catch (er) { + } + } + } + else + if (!e.shiftKey && !e.crtlKey && e.keyCode == 9 && (isStopped === true || (isStopped && !$.prop(this, 'value')))) { + e.preventDefault(); + } + } + }; + })() + }; + var mouseDownInit = function(){ + if (!o.disabled && !isFocused) { + that.element.getShadowFocusElement().trigger('focus'); + } + preventBlur.set(); + + return false; + }; + + preventBlur.set = (function(){ + var timer; + var reset = function(){ + preventBlur.prevent = false; + }; + return function(){ + clearTimeout(timer); + preventBlur.prevent = true; + setTimeout(reset, 9); + }; + })(); + + + this.buttonWrapper.on('mousedown', mouseDownInit); + + this.setInput = function(value){ + that.value(value); + eventTimer.call('input', value); + }; + this.setChange = function(value){ + that.setInput(value); + eventTimer.call('change', value); + }; + + + + this.inputElements.on(elementEvts); + + if (steps[this.type]) { + ['stepUp', 'stepDown'].forEach(function(name){ + step[name] = function(factor){ + if (!o.disabled && !o.readonly) { + if (!isFocused) { + mouseDownInit(); + } + var ret = false; + if (!factor) { + factor = 1; + } + try { + that.elemHelper[name](factor); + + ret = that.elemHelper.prop('value'); + + } + catch (er) { + if (!o.value && that.maxAsNumber >= that.minAsNumber) { + ret = o.defValue; + } + } + if (ret !== false && o.value != ret) { + that.value(ret); + eventTimer.call('input', ret); + } + return ret; + } + }; + }); + if (!o.noSpinbtn) { + spinEvents.mwheelIntent = function(e, delta){ + if (delta && isFocused && !o.disabled) { + step[delta > 0 ? 'stepUp' : 'stepDown'](); + e.preventDefault(); + } + }; + spinEvents.keydown = function(e){ + if (o.list || e.isDefaultPrevented() || (e.altKey && e.keyCode == 40) || $.attr(this, 'list')) { + return; + } + var stepped = true; + var code = e.keyCode; + if (code == 38) { + step.stepUp(); + } + else + if (code == 40) { + step.stepDown(); + } + else { + stepped = false; + } + if (stepped) { + e.preventDefault(); + } + }; + + spinElement.attr({ + 'autocomplete': 'off', + role: 'spinbutton' + }).on(spinEvents); + } + $(this.buttonWrapper).on('mousepressstart mousepressend', '.step-up, .step-down', mousePress).on('mousedown mousepress', '.step-up', function(e){ + step.stepUp(); + }).on('mousedown mousepress', '.step-down', function(e){ + step.stepDown(); + }); + initChangeEvents(); + } + }, + initDataList: function(){ + var listTimer; + var that = this; + var updateList = function(){ + $(that.orig) + .jProp('list') + .off('updateDatalist', updateList) + .on('updateDatalist', updateList) + ; + clearTimeout(listTimer); + listTimer = setTimeout(function(){ + if(that.list){ + that.list(); + } + }, 9); + + }; + + $(this.orig).onTrigger('listdatalistchange', updateList); + }, + getOptions: function(){ + var options = {}; + var datalist = $(this.orig).jProp('list'); + datalist.find('option').each(function(){ + options[$.prop(this, 'value')] = $.prop(this, 'label'); }); + return [options, datalist.data('label')]; } + }; + + $.extend($.fn.wsBaseWidget.wsProto, widgetProtos); + $.extend($.fn.spinbtnUI.wsProto, widgetProtos); + + + $(formcfg).on('change', function(e, data){ + curCfg = formcfg.__active; }); webshims.ListBox = function (element, popover, opts){ this.element = $('ul', element); @@ -141,10 +737,10 @@ webshims.Grid = function (element, popover, opts){ this.element = $('tbody', element); this.popover = popover; this.opts = opts || {}; - this.buttons = $('button:not(:disabled,.othermonth)', this.element); + this.buttons = $('button:not(:disabled):not(.othermonth)', this.element); this.ons(this); this._initialFocus(); if(this.popover.openedByFocus){