/**
* Bootstrap v0.2.2 by Echo Logistics
* Copyright 2021 Echo Global Logistics
* Licensed under http://www.apache.org/licenses/LICENSE-2.0.
*
* Designed and built with all the love in the world by @mdo and @fat.
*/
/* ========================================================================
* Bootstrap: transition.js v3.1.0
* http://getbootstrap.com/javascript/#transitions
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// CSS TRANSITION SUPPORT (Shoutout: http://www.modernizr.com/)
// ============================================================
function transitionEnd() {
var el = document.createElement('bootstrap')
var transEndEventNames = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd otransitionend',
'transition' : 'transitionend'
}
for (var name in transEndEventNames) {
if (el.style[name] !== undefined) {
return { end: transEndEventNames[name] }
}
}
return false // explicit for ie8 ( ._.)
}
// http://blog.alexmaccaw.com/css-transitions
$.fn.emulateTransitionEnd = function (duration) {
var called = false, $el = this
$(this).one($.support.transition.end, function () { called = true })
var callback = function () { if (!called) $($el).trigger($.support.transition.end) }
setTimeout(callback, duration)
return this
}
$(function () {
$.support.transition = transitionEnd()
})
}(jQuery);
/* ========================================================================
* Bootstrap: alert.js v3.1.0
* http://getbootstrap.com/javascript/#alerts
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// ALERT CLASS DEFINITION
// ======================
var dismiss = '[data-dismiss="alert"]'
var Alert = function (el) {
$(el).on('click', dismiss, this.close)
}
Alert.prototype.close = function (e) {
var $this = $(this)
var selector = $this.attr('data-target')
if (!selector) {
selector = $this.attr('href')
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
}
var $parent = $(selector)
if (e) e.preventDefault()
if (!$parent.length) {
$parent = $this.hasClass('alert') ? $this : $this.parent()
}
$parent.trigger(e = $.Event('close.bs.alert'))
if (e.isDefaultPrevented()) return
$parent.removeClass('in')
function removeElement() {
$parent.trigger('closed.bs.alert').remove()
}
$.support.transition && $parent.hasClass('fade') ?
$parent
.one($.support.transition.end, removeElement)
.emulateTransitionEnd(150) :
removeElement()
}
// ALERT PLUGIN DEFINITION
// =======================
var old = $.fn.alert
$.fn.alert = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.alert')
if (!data) $this.data('bs.alert', (data = new Alert(this)))
if (typeof option == 'string') data[option].call($this)
})
}
$.fn.alert.Constructor = Alert
// ALERT NO CONFLICT
// =================
$.fn.alert.noConflict = function () {
$.fn.alert = old
return this
}
// ALERT DATA-API
// ==============
$(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close)
}(jQuery);
/* ========================================================================
* Bootstrap: button.js v3.1.0
* http://getbootstrap.com/javascript/#buttons
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// BUTTON PUBLIC CLASS DEFINITION
// ==============================
var Button = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, Button.DEFAULTS, options)
this.isLoading = false
}
Button.DEFAULTS = {
loadingText: 'loading...'
}
Button.prototype.setState = function (state) {
var d = 'disabled'
var $el = this.$element
var val = $el.is('input') ? 'val' : 'html'
var data = $el.data()
state = state + 'Text'
if (!data.resetText) $el.data('resetText', $el[val]())
$el[val](data[state] || this.options[state])
// push to event loop to allow forms to submit
setTimeout($.proxy(function () {
if (state == 'loadingText') {
this.isLoading = true
$el.addClass(d).attr(d, d)
} else if (this.isLoading) {
this.isLoading = false
$el.removeClass(d).removeAttr(d)
}
}, this), 0)
}
Button.prototype.toggle = function () {
var changed = true
var $parent = this.$element.closest('[data-toggle="buttons"]')
if ($parent.length) {
var $input = this.$element.find('input')
if ($input.prop('type') == 'radio') {
if ($input.prop('checked') && this.$element.hasClass('active')) changed = false
else $parent.find('.active').removeClass('active')
}
if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
}
if (changed) this.$element.toggleClass('active')
}
// BUTTON PLUGIN DEFINITION
// ========================
var old = $.fn.button
$.fn.button = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.button')
var options = typeof option == 'object' && option
if (!data) $this.data('bs.button', (data = new Button(this, options)))
if (option == 'toggle') data.toggle()
else if (option) data.setState(option)
})
}
$.fn.button.Constructor = Button
// BUTTON NO CONFLICT
// ==================
$.fn.button.noConflict = function () {
$.fn.button = old
return this
}
// BUTTON DATA-API
// ===============
$(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
var $btn = $(e.target)
if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
$btn.button('toggle')
e.preventDefault()
})
}(jQuery);
/* ========================================================================
* Bootstrap: carousel.js v3.1.0
* http://getbootstrap.com/javascript/#carousel
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// CAROUSEL CLASS DEFINITION
// =========================
var Carousel = function (element, options) {
this.$element = $(element)
this.$indicators = this.$element.find('.carousel-indicators')
this.options = options
this.paused =
this.sliding =
this.interval =
this.$active =
this.$items = null
this.options.pause == 'hover' && this.$element
.on('mouseenter', $.proxy(this.pause, this))
.on('mouseleave', $.proxy(this.cycle, this))
}
Carousel.DEFAULTS = {
interval: 5000,
pause: 'hover',
wrap: true
}
Carousel.prototype.cycle = function (e) {
e || (this.paused = false)
this.interval && clearInterval(this.interval)
this.options.interval
&& !this.paused
&& (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
return this
}
Carousel.prototype.getActiveIndex = function () {
this.$active = this.$element.find('.item.active')
this.$items = this.$active.parent().children()
return this.$items.index(this.$active)
}
Carousel.prototype.to = function (pos) {
var that = this
var activeIndex = this.getActiveIndex()
if (pos > (this.$items.length - 1) || pos < 0) return
if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) })
if (activeIndex == pos) return this.pause().cycle()
return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
}
Carousel.prototype.pause = function (e) {
e || (this.paused = true)
if (this.$element.find('.next, .prev').length && $.support.transition) {
this.$element.trigger($.support.transition.end)
this.cycle(true)
}
this.interval = clearInterval(this.interval)
return this
}
Carousel.prototype.next = function () {
if (this.sliding) return
return this.slide('next')
}
Carousel.prototype.prev = function () {
if (this.sliding) return
return this.slide('prev')
}
Carousel.prototype.slide = function (type, next) {
var $active = this.$element.find('.item.active')
var $next = next || $active[type]()
var isCycling = this.interval
var direction = type == 'next' ? 'left' : 'right'
var fallback = type == 'next' ? 'first' : 'last'
var that = this
if (!$next.length) {
if (!this.options.wrap) return
$next = this.$element.find('.item')[fallback]()
}
if ($next.hasClass('active')) return this.sliding = false
var e = $.Event('slide.bs.carousel', { relatedTarget: $next[0], direction: direction })
this.$element.trigger(e)
if (e.isDefaultPrevented()) return
this.sliding = true
isCycling && this.pause()
if (this.$indicators.length) {
this.$indicators.find('.active').removeClass('active')
this.$element.one('slid.bs.carousel', function () {
var $nextIndicator = $(that.$indicators.children()[that.getActiveIndex()])
$nextIndicator && $nextIndicator.addClass('active')
})
}
if ($.support.transition && this.$element.hasClass('slide')) {
$next.addClass(type)
$next[0].offsetWidth // force reflow
$active.addClass(direction)
$next.addClass(direction)
$active
.one($.support.transition.end, function () {
$next.removeClass([type, direction].join(' ')).addClass('active')
$active.removeClass(['active', direction].join(' '))
that.sliding = false
setTimeout(function () { that.$element.trigger('slid.bs.carousel') }, 0)
})
.emulateTransitionEnd($active.css('transition-duration').slice(0, -1) * 1000)
} else {
$active.removeClass('active')
$next.addClass('active')
this.sliding = false
this.$element.trigger('slid.bs.carousel')
}
isCycling && this.cycle()
return this
}
// CAROUSEL PLUGIN DEFINITION
// ==========================
var old = $.fn.carousel
$.fn.carousel = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.carousel')
var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option)
var action = typeof option == 'string' ? option : options.slide
if (!data) $this.data('bs.carousel', (data = new Carousel(this, options)))
if (typeof option == 'number') data.to(option)
else if (action) data[action]()
else if (options.interval) data.pause().cycle()
})
}
$.fn.carousel.Constructor = Carousel
// CAROUSEL NO CONFLICT
// ====================
$.fn.carousel.noConflict = function () {
$.fn.carousel = old
return this
}
// CAROUSEL DATA-API
// =================
$(document).on('click.bs.carousel.data-api', '[data-slide], [data-slide-to]', function (e) {
var $this = $(this), href
var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
var options = $.extend({}, $target.data(), $this.data())
var slideIndex = $this.attr('data-slide-to')
if (slideIndex) options.interval = false
$target.carousel(options)
if (slideIndex = $this.attr('data-slide-to')) {
$target.data('bs.carousel').to(slideIndex)
}
e.preventDefault()
})
$(window).on('load', function () {
$('[data-ride="carousel"]').each(function () {
var $carousel = $(this)
$carousel.carousel($carousel.data())
})
})
}(jQuery);
/* ========================================================================
* Bootstrap: collapse.js v3.1.0
* http://getbootstrap.com/javascript/#collapse
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// COLLAPSE PUBLIC CLASS DEFINITION
// ================================
var Collapse = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, Collapse.DEFAULTS, options)
this.transitioning = null
if (this.options.parent) this.$parent = $(this.options.parent)
if (this.options.toggle) this.toggle()
}
Collapse.DEFAULTS = {
toggle: true
}
Collapse.prototype.dimension = function () {
var hasWidth = this.$element.hasClass('width')
return hasWidth ? 'width' : 'height'
}
Collapse.prototype.show = function () {
if (this.transitioning || this.$element.hasClass('in')) return
var startEvent = $.Event('show.bs.collapse')
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return
var actives = this.$parent && this.$parent.find('> .panel > .in')
if (actives && actives.length) {
var hasData = actives.data('bs.collapse')
if (hasData && hasData.transitioning) return
actives.collapse('hide')
hasData || actives.data('bs.collapse', null)
}
var dimension = this.dimension()
this.$element
.removeClass('collapse')
.addClass('collapsing')
[dimension](0)
this.transitioning = 1
var complete = function () {
this.$element
.removeClass('collapsing')
.addClass('collapse in')
[dimension]('auto')
this.transitioning = 0
this.$element.trigger('shown.bs.collapse')
}
if (!$.support.transition) return complete.call(this)
var scrollSize = $.camelCase(['scroll', dimension].join('-'))
this.$element
.one($.support.transition.end, $.proxy(complete, this))
.emulateTransitionEnd(350)
[dimension](this.$element[0][scrollSize])
}
Collapse.prototype.hide = function () {
if (this.transitioning || !this.$element.hasClass('in')) return
var startEvent = $.Event('hide.bs.collapse')
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return
var dimension = this.dimension()
this.$element
[dimension](this.$element[dimension]())
[0].offsetHeight
this.$element
.addClass('collapsing')
.removeClass('collapse')
.removeClass('in')
this.transitioning = 1
var complete = function () {
this.transitioning = 0
this.$element
.trigger('hidden.bs.collapse')
.removeClass('collapsing')
.addClass('collapse')
}
if (!$.support.transition) return complete.call(this)
this.$element
[dimension](0)
.one($.support.transition.end, $.proxy(complete, this))
.emulateTransitionEnd(350)
}
Collapse.prototype.toggle = function () {
this[this.$element.hasClass('in') ? 'hide' : 'show']()
}
// COLLAPSE PLUGIN DEFINITION
// ==========================
var old = $.fn.collapse
$.fn.collapse = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.collapse')
var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data && options.toggle && option == 'show') option = !option
if (!data) $this.data('bs.collapse', (data = new Collapse(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.collapse.Constructor = Collapse
// COLLAPSE NO CONFLICT
// ====================
$.fn.collapse.noConflict = function () {
$.fn.collapse = old
return this
}
// COLLAPSE DATA-API
// =================
$(document).on('click.bs.collapse.data-api', '[data-toggle=collapse]', function (e) {
var $this = $(this), href
var target = $this.attr('data-target')
|| e.preventDefault()
|| (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
var $target = $(target)
var data = $target.data('bs.collapse')
var option = data ? 'toggle' : $this.data()
var parent = $this.attr('data-parent')
var $parent = parent && $(parent)
if (!data || !data.transitioning) {
if ($parent) $parent.find('[data-toggle=collapse][data-parent="' + parent + '"]').not($this).addClass('collapsed')
$this[$target.hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
}
$target.collapse(option)
})
}(jQuery);
/* ========================================================================
* Bootstrap: dropdown.js v3.1.0
* http://getbootstrap.com/javascript/#dropdowns
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// DROPDOWN CLASS DEFINITION
// =========================
var backdrop = '.dropdown-backdrop'
var toggle = '[data-toggle=dropdown]'
var Dropdown = function (element) {
$(element).on('click.bs.dropdown', this.toggle)
}
Dropdown.prototype.toggle = function (e) {
var $this = $(this)
if ($this.is('.disabled, :disabled')) return
var $parent = getParent($this)
var isActive = $parent.hasClass('open')
clearMenus()
if (!isActive) {
if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
// if mobile we use a backdrop because click events don't delegate
$('
').insertAfter($(this)).on('click', clearMenus)
}
var relatedTarget = { relatedTarget: this }
$parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget))
if (e.isDefaultPrevented()) return
$parent
.toggleClass('open')
.trigger('shown.bs.dropdown', relatedTarget)
$this.focus()
}
return false
}
Dropdown.prototype.keydown = function (e) {
if (!/(38|40|27)/.test(e.keyCode)) return
var $this = $(this)
e.preventDefault()
e.stopPropagation()
if ($this.is('.disabled, :disabled')) return
var $parent = getParent($this)
var isActive = $parent.hasClass('open')
if (!isActive || (isActive && e.keyCode == 27)) {
if (e.which == 27) $parent.find(toggle).focus()
return $this.click()
}
var desc = ' li:not(.divider):visible a'
var $items = $parent.find('[role=menu]' + desc + ', [role=listbox]' + desc)
if (!$items.length) return
var index = $items.index($items.filter(':focus'))
if (e.keyCode == 38 && index > 0) index-- // up
if (e.keyCode == 40 && index < $items.length - 1) index++ // down
if (!~index) index = 0
$items.eq(index).focus()
}
function clearMenus(e) {
$(backdrop).remove()
$(toggle).each(function () {
var $parent = getParent($(this))
var relatedTarget = { relatedTarget: this }
if (!$parent.hasClass('open')) return
$parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget))
if (e.isDefaultPrevented()) return
$parent.removeClass('open').trigger('hidden.bs.dropdown', relatedTarget)
})
}
function getParent($this) {
var selector = $this.attr('data-target')
if (!selector) {
selector = $this.attr('href')
selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
}
var $parent = selector && $(selector)
return $parent && $parent.length ? $parent : $this.parent()
}
// DROPDOWN PLUGIN DEFINITION
// ==========================
var old = $.fn.dropdown
$.fn.dropdown = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.dropdown')
if (!data) $this.data('bs.dropdown', (data = new Dropdown(this)))
if (typeof option == 'string') data[option].call($this)
})
}
$.fn.dropdown.Constructor = Dropdown
// DROPDOWN NO CONFLICT
// ====================
$.fn.dropdown.noConflict = function () {
$.fn.dropdown = old
return this
}
// APPLY TO STANDARD DROPDOWN ELEMENTS
// ===================================
$(document)
.on('click.bs.dropdown.data-api', clearMenus)
.on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
.on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle)
.on('keydown.bs.dropdown.data-api', toggle + ', [role=menu], [role=listbox]', Dropdown.prototype.keydown)
}(jQuery);
/* ========================================================================
* Bootstrap: modal.js v3.1.0
* http://getbootstrap.com/javascript/#modals
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// MODAL CLASS DEFINITION
// ======================
var Modal = function (element, options) {
this.options = options
this.$element = $(element)
this.$backdrop =
this.isShown = null
if (this.options.remote) {
this.$element
.find('.modal-content')
.load(this.options.remote, $.proxy(function () {
this.$element.trigger('loaded.bs.modal')
}, this))
}
}
Modal.DEFAULTS = {
backdrop: true,
keyboard: true,
show: true
}
Modal.prototype.toggle = function (_relatedTarget) {
return this[!this.isShown ? 'show' : 'hide'](_relatedTarget)
}
Modal.prototype.show = function (_relatedTarget) {
var that = this
var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
this.$element.trigger(e)
if (this.isShown || e.isDefaultPrevented()) return
this.isShown = true
this.escape()
this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
this.backdrop(function () {
var transition = $.support.transition && that.$element.hasClass('fade')
if (!that.$element.parent().length) {
that.$element.appendTo(document.body) // don't move modals dom position
}
that.$element
.show()
.scrollTop(0)
if (transition) {
that.$element[0].offsetWidth // force reflow
}
that.$element
.addClass('in')
.attr('aria-hidden', false)
that.enforceFocus()
var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
transition ?
that.$element.find('.modal-dialog') // wait for modal to slide in
.one($.support.transition.end, function () {
that.$element.focus().trigger(e)
})
.emulateTransitionEnd(300) :
that.$element.focus().trigger(e)
})
}
Modal.prototype.hide = function (e) {
if (e) e.preventDefault()
e = $.Event('hide.bs.modal')
this.$element.trigger(e)
if (!this.isShown || e.isDefaultPrevented()) return
this.isShown = false
this.escape()
$(document).off('focusin.bs.modal')
this.$element
.removeClass('in')
.attr('aria-hidden', true)
.off('click.dismiss.bs.modal')
$.support.transition && this.$element.hasClass('fade') ?
this.$element
.one($.support.transition.end, $.proxy(this.hideModal, this))
.emulateTransitionEnd(300) :
this.hideModal()
}
Modal.prototype.enforceFocus = function () {
$(document)
.off('focusin.bs.modal') // guard against infinite focus loop
.on('focusin.bs.modal', $.proxy(function (e) {
if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
this.$element.focus()
}
}, this))
}
Modal.prototype.escape = function () {
if (this.isShown && this.options.keyboard) {
this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
e.which == 27 && this.hide()
}, this))
} else if (!this.isShown) {
this.$element.off('keyup.dismiss.bs.modal')
}
}
Modal.prototype.hideModal = function () {
var that = this
this.$element.hide()
this.backdrop(function () {
that.removeBackdrop()
that.$element.trigger('hidden.bs.modal')
})
}
Modal.prototype.removeBackdrop = function () {
this.$backdrop && this.$backdrop.remove()
this.$backdrop = null
}
Modal.prototype.backdrop = function (callback) {
var animate = this.$element.hasClass('fade') ? 'fade' : ''
if (this.isShown && this.options.backdrop) {
var doAnimate = $.support.transition && animate
this.$backdrop = $('')
.appendTo(document.body)
this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
if (e.target !== e.currentTarget) return
this.options.backdrop == 'static'
? this.$element[0].focus.call(this.$element[0])
: this.hide.call(this)
}, this))
if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
this.$backdrop.addClass('in')
if (!callback) return
doAnimate ?
this.$backdrop
.one($.support.transition.end, callback)
.emulateTransitionEnd(150) :
callback()
} else if (!this.isShown && this.$backdrop) {
this.$backdrop.removeClass('in')
$.support.transition && this.$element.hasClass('fade') ?
this.$backdrop
.one($.support.transition.end, callback)
.emulateTransitionEnd(150) :
callback()
} else if (callback) {
callback()
}
}
// MODAL PLUGIN DEFINITION
// =======================
var old = $.fn.modal
$.fn.modal = function (option, _relatedTarget) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.modal')
var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
if (typeof option == 'string') data[option](_relatedTarget)
else if (options.show) data.show(_relatedTarget)
})
}
$.fn.modal.Constructor = Modal
// MODAL NO CONFLICT
// =================
$.fn.modal.noConflict = function () {
$.fn.modal = old
return this
}
// MODAL DATA-API
// ==============
$(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
var $this = $(this)
var href = $this.attr('href')
var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
var option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
if ($this.is('a')) e.preventDefault()
$target
.modal(option, this)
.one('hide', function () {
$this.is(':visible') && $this.focus()
})
})
$(document)
.on('show.bs.modal', '.modal', function () { $(document.body).addClass('modal-open') })
.on('hidden.bs.modal', '.modal', function () { $(document.body).removeClass('modal-open') })
}(jQuery);
/* ========================================================================
* Bootstrap: tooltip.js v3.1.0
* http://getbootstrap.com/javascript/#tooltip
* Inspired by the original jQuery.tipsy by Jason Frame
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// TOOLTIP PUBLIC CLASS DEFINITION
// ===============================
var Tooltip = function (element, options) {
this.type =
this.options =
this.enabled =
this.timeout =
this.hoverState =
this.$element = null
this.init('tooltip', element, options)
}
Tooltip.DEFAULTS = {
animation: true,
placement: 'top',
selector: false,
template: '
',
trigger: 'hover focus',
title: '',
delay: 0,
html: false,
container: false
}
Tooltip.prototype.init = function (type, element, options) {
this.enabled = true
this.type = type
this.$element = $(element)
this.options = this.getOptions(options)
var triggers = this.options.trigger.split(' ')
for (var i = triggers.length; i--;) {
var trigger = triggers[i]
if (trigger == 'click') {
this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
} else if (trigger != 'manual') {
var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin'
var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
}
}
this.options.selector ?
(this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
this.fixTitle()
}
Tooltip.prototype.getDefaults = function () {
return Tooltip.DEFAULTS
}
Tooltip.prototype.getOptions = function (options) {
options = $.extend({}, this.getDefaults(), this.$element.data(), options)
if (options.delay && typeof options.delay == 'number') {
options.delay = {
show: options.delay,
hide: options.delay
}
}
return options
}
Tooltip.prototype.getDelegateOptions = function () {
var options = {}
var defaults = this.getDefaults()
this._options && $.each(this._options, function (key, value) {
if (defaults[key] != value) options[key] = value
})
return options
}
Tooltip.prototype.enter = function (obj) {
var self = obj instanceof this.constructor ?
obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
clearTimeout(self.timeout)
self.hoverState = 'in'
if (!self.options.delay || !self.options.delay.show) return self.show()
self.timeout = setTimeout(function () {
if (self.hoverState == 'in') self.show()
}, self.options.delay.show)
}
Tooltip.prototype.leave = function (obj) {
var self = obj instanceof this.constructor ?
obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
clearTimeout(self.timeout)
self.hoverState = 'out'
if (!self.options.delay || !self.options.delay.hide) return self.hide()
self.timeout = setTimeout(function () {
if (self.hoverState == 'out') self.hide()
}, self.options.delay.hide)
}
Tooltip.prototype.show = function () {
var e = $.Event('show.bs.' + this.type)
if (this.hasContent() && this.enabled) {
this.$element.trigger(e)
if (e.isDefaultPrevented()) return
var that = this;
var $tip = this.tip()
this.setContent()
if (this.options.animation) $tip.addClass('fade')
var placement = typeof this.options.placement == 'function' ?
this.options.placement.call(this, $tip[0], this.$element[0]) :
this.options.placement
var autoToken = /\s?auto?\s?/i
var autoPlace = autoToken.test(placement)
if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
$tip
.detach()
.css({ top: 0, left: 0, display: 'block' })
.addClass(placement)
this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
var pos = this.getPosition()
var actualWidth = $tip[0].offsetWidth
var actualHeight = $tip[0].offsetHeight
if (autoPlace) {
var $parent = this.$element.parent()
var orgPlacement = placement
var docScroll = document.documentElement.scrollTop || document.body.scrollTop
var parentWidth = this.options.container == 'body' ? window.innerWidth : $parent.outerWidth()
var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
var parentLeft = this.options.container == 'body' ? 0 : $parent.offset().left
placement = placement == 'bottom' && pos.top + pos.height + actualHeight - docScroll > parentHeight ? 'top' :
placement == 'top' && pos.top - docScroll - actualHeight < 0 ? 'bottom' :
placement == 'right' && pos.right + actualWidth > parentWidth ? 'left' :
placement == 'left' && pos.left - actualWidth < parentLeft ? 'right' :
placement
$tip
.removeClass(orgPlacement)
.addClass(placement)
}
var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
this.applyPlacement(calculatedOffset, placement)
this.hoverState = null
var complete = function() {
that.$element.trigger('shown.bs.' + that.type)
}
$.support.transition && this.$tip.hasClass('fade') ?
$tip
.one($.support.transition.end, complete)
.emulateTransitionEnd(150) :
complete()
}
}
Tooltip.prototype.applyPlacement = function (offset, placement) {
var replace
var $tip = this.tip()
var width = $tip[0].offsetWidth
var height = $tip[0].offsetHeight
// manually read margins because getBoundingClientRect includes difference
var marginTop = parseInt($tip.css('margin-top'), 10)
var marginLeft = parseInt($tip.css('margin-left'), 10)
// we must check for NaN for ie 8/9
if (isNaN(marginTop)) marginTop = 0
if (isNaN(marginLeft)) marginLeft = 0
offset.top = offset.top + marginTop
offset.left = offset.left + marginLeft
// $.fn.offset doesn't round pixel values
// so we use setOffset directly with our own function B-0
$.offset.setOffset($tip[0], $.extend({
using: function (props) {
$tip.css({
top: Math.round(props.top),
left: Math.round(props.left)
})
}
}, offset), 0)
$tip.addClass('in')
// check to see if placing tip in new offset caused the tip to resize itself
var actualWidth = $tip[0].offsetWidth
var actualHeight = $tip[0].offsetHeight
if (placement == 'top' && actualHeight != height) {
replace = true
offset.top = offset.top + height - actualHeight
}
if (/bottom|top/.test(placement)) {
var delta = 0
if (offset.left < 0) {
delta = offset.left * -2
offset.left = 0
$tip.offset(offset)
actualWidth = $tip[0].offsetWidth
actualHeight = $tip[0].offsetHeight
}
this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
} else {
this.replaceArrow(actualHeight - height, actualHeight, 'top')
}
if (replace) $tip.offset(offset)
}
Tooltip.prototype.replaceArrow = function (delta, dimension, position) {
this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + '%') : '')
}
Tooltip.prototype.setContent = function () {
var $tip = this.tip()
var title = this.getTitle()
$tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
$tip.removeClass('fade in top bottom left right')
}
Tooltip.prototype.hide = function () {
var that = this
var $tip = this.tip()
var e = $.Event('hide.bs.' + this.type)
function complete() {
if (that.hoverState != 'in') $tip.detach()
that.$element.trigger('hidden.bs.' + that.type)
}
this.$element.trigger(e)
if (e.isDefaultPrevented()) return
$tip.removeClass('in')
$.support.transition && this.$tip.hasClass('fade') ?
$tip
.one($.support.transition.end, complete)
.emulateTransitionEnd(150) :
complete()
this.hoverState = null
return this
}
Tooltip.prototype.fixTitle = function () {
var $e = this.$element
if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
$e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
}
}
Tooltip.prototype.hasContent = function () {
return this.getTitle()
}
Tooltip.prototype.getPosition = function () {
var el = this.$element[0]
return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
width: el.offsetWidth,
height: el.offsetHeight
}, this.$element.offset())
}
Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
return placement == 'bottom' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2 } :
placement == 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
placement == 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
/* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
}
Tooltip.prototype.getTitle = function () {
var title
var $e = this.$element
var o = this.options
title = $e.attr('data-original-title')
|| (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
return title
}
Tooltip.prototype.tip = function () {
return this.$tip = this.$tip || $(this.options.template)
}
Tooltip.prototype.arrow = function () {
return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
}
Tooltip.prototype.validate = function () {
if (!this.$element[0].parentNode) {
this.hide()
this.$element = null
this.options = null
}
}
Tooltip.prototype.enable = function () {
this.enabled = true
}
Tooltip.prototype.disable = function () {
this.enabled = false
}
Tooltip.prototype.toggleEnabled = function () {
this.enabled = !this.enabled
}
Tooltip.prototype.toggle = function (e) {
var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
}
Tooltip.prototype.destroy = function () {
clearTimeout(this.timeout)
this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
}
// TOOLTIP PLUGIN DEFINITION
// =========================
var old = $.fn.tooltip
$.fn.tooltip = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.tooltip')
var options = typeof option == 'object' && option
if (!data && option == 'destroy') return
if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.tooltip.Constructor = Tooltip
// TOOLTIP NO CONFLICT
// ===================
$.fn.tooltip.noConflict = function () {
$.fn.tooltip = old
return this
}
}(jQuery);
/* ========================================================================
* Bootstrap: popover.js v3.1.0
* http://getbootstrap.com/javascript/#popovers
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// POPOVER PUBLIC CLASS DEFINITION
// ===============================
var Popover = function (element, options) {
this.init('popover', element, options)
}
if (!$.fn.tooltip) throw new Error('Popover requires tooltip.js')
Popover.DEFAULTS = $.extend({}, $.fn.tooltip.Constructor.DEFAULTS, {
placement: 'right',
trigger: 'click',
content: '',
template: '
'
})
// NOTE: POPOVER EXTENDS tooltip.js
// ================================
Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype)
Popover.prototype.constructor = Popover
Popover.prototype.getDefaults = function () {
return Popover.DEFAULTS
}
Popover.prototype.setContent = function () {
var $tip = this.tip()
var title = this.getTitle()
var content = this.getContent()
$tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
$tip.find('.popover-content')[ // we use append for html objects to maintain js events
this.options.html ? (typeof content == 'string' ? 'html' : 'append') : 'text'
](content)
$tip.removeClass('fade top bottom left right in')
// IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
// this manually by checking the contents.
if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
}
Popover.prototype.hasContent = function () {
return this.getTitle() || this.getContent()
}
Popover.prototype.getContent = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-content')
|| (typeof o.content == 'function' ?
o.content.call($e[0]) :
o.content)
}
Popover.prototype.arrow = function () {
return this.$arrow = this.$arrow || this.tip().find('.arrow')
}
Popover.prototype.tip = function () {
if (!this.$tip) this.$tip = $(this.options.template)
return this.$tip
}
// POPOVER PLUGIN DEFINITION
// =========================
var old = $.fn.popover
$.fn.popover = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.popover')
var options = typeof option == 'object' && option
if (!data && option == 'destroy') return
if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.popover.Constructor = Popover
// POPOVER NO CONFLICT
// ===================
$.fn.popover.noConflict = function () {
$.fn.popover = old
return this
}
}(jQuery);
/* ========================================================================
* Bootstrap: scrollspy.js v3.1.0
* http://getbootstrap.com/javascript/#scrollspy
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// SCROLLSPY CLASS DEFINITION
// ==========================
function ScrollSpy(element, options) {
var href
var process = $.proxy(this.process, this)
this.$element = $(element).is('body') ? $(window) : $(element)
this.$body = $('body')
this.$scrollElement = this.$element.on('scroll.bs.scroll-spy.data-api', process)
this.options = $.extend({}, ScrollSpy.DEFAULTS, options)
this.selector = (this.options.target
|| ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
|| '') + ' .nav li > a'
this.offsets = $([])
this.targets = $([])
this.activeTarget = null
this.refresh()
this.process()
}
ScrollSpy.DEFAULTS = {
offset: 10
}
ScrollSpy.prototype.refresh = function () {
var offsetMethod = this.$element[0] == window ? 'offset' : 'position'
this.offsets = $([])
this.targets = $([])
var self = this
var $targets = this.$body
.find(this.selector)
.map(function () {
var $el = $(this)
var href = $el.data('target') || $el.attr('href')
var $href = /^#./.test(href) && $(href)
return ($href
&& $href.length
&& $href.is(':visible')
&& [[ $href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
})
.sort(function (a, b) { return a[0] - b[0] })
.each(function () {
self.offsets.push(this[0])
self.targets.push(this[1])
})
}
ScrollSpy.prototype.process = function () {
var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
var scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
var maxScroll = scrollHeight - this.$scrollElement.height()
var offsets = this.offsets
var targets = this.targets
var activeTarget = this.activeTarget
var i
if (scrollTop >= maxScroll) {
return activeTarget != (i = targets.last()[0]) && this.activate(i)
}
if (activeTarget && scrollTop <= offsets[0]) {
return activeTarget != (i = targets[0]) && this.activate(i)
}
for (i = offsets.length; i--;) {
activeTarget != targets[i]
&& scrollTop >= offsets[i]
&& (!offsets[i + 1] || scrollTop <= offsets[i + 1])
&& this.activate( targets[i] )
}
}
ScrollSpy.prototype.activate = function (target) {
this.activeTarget = target
$(this.selector)
.parentsUntil(this.options.target, '.active')
.removeClass('active')
var selector = this.selector +
'[data-target="' + target + '"],' +
this.selector + '[href="' + target + '"]'
var active = $(selector)
.parents('li')
.addClass('active')
if (active.parent('.dropdown-menu').length) {
active = active
.closest('li.dropdown')
.addClass('active')
}
active.trigger('activate.bs.scrollspy')
}
// SCROLLSPY PLUGIN DEFINITION
// ===========================
var old = $.fn.scrollspy
$.fn.scrollspy = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.scrollspy')
var options = typeof option == 'object' && option
if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.scrollspy.Constructor = ScrollSpy
// SCROLLSPY NO CONFLICT
// =====================
$.fn.scrollspy.noConflict = function () {
$.fn.scrollspy = old
return this
}
// SCROLLSPY DATA-API
// ==================
$(window).on('load', function () {
$('[data-spy="scroll"]').each(function () {
var $spy = $(this)
$spy.scrollspy($spy.data())
})
})
}(jQuery);
/* ========================================================================
* Bootstrap: tab.js v3.1.0
* http://getbootstrap.com/javascript/#tabs
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// TAB CLASS DEFINITION
// ====================
var Tab = function (element) {
this.element = $(element)
}
Tab.prototype.show = function () {
var $this = this.element
var $ul = $this.closest('ul:not(.dropdown-menu)')
var selector = $this.data('target')
if (!selector) {
selector = $this.attr('href')
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
}
if ($this.parent('li').hasClass('active')) return
var previous = $ul.find('.active:last a')[0]
var e = $.Event('show.bs.tab', {
relatedTarget: previous
})
$this.trigger(e)
if (e.isDefaultPrevented()) return
var $target = $(selector)
this.activate($this.parent('li'), $ul)
this.activate($target, $target.parent(), function () {
$this.trigger({
type: 'shown.bs.tab',
relatedTarget: previous
})
})
}
Tab.prototype.activate = function (element, container, callback) {
var $active = container.find('> .active')
var transition = callback
&& $.support.transition
&& $active.hasClass('fade')
function next() {
$active
.removeClass('active')
.find('> .dropdown-menu > .active')
.removeClass('active')
element.addClass('active')
if (transition) {
element[0].offsetWidth // reflow for transition
element.addClass('in')
} else {
element.removeClass('fade')
}
if (element.parent('.dropdown-menu')) {
element.closest('li.dropdown').addClass('active')
}
callback && callback()
}
transition ?
$active
.one($.support.transition.end, next)
.emulateTransitionEnd(150) :
next()
$active.removeClass('in')
}
// TAB PLUGIN DEFINITION
// =====================
var old = $.fn.tab
$.fn.tab = function ( option ) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.tab')
if (!data) $this.data('bs.tab', (data = new Tab(this)))
if (typeof option == 'string') data[option]()
})
}
$.fn.tab.Constructor = Tab
// TAB NO CONFLICT
// ===============
$.fn.tab.noConflict = function () {
$.fn.tab = old
return this
}
// TAB DATA-API
// ============
$(document).on('click.bs.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
e.preventDefault()
$(this).tab('show')
})
}(jQuery);
/* ========================================================================
* Bootstrap: affix.js v3.1.0
* http://getbootstrap.com/javascript/#affix
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// AFFIX CLASS DEFINITION
// ======================
var Affix = function (element, options) {
this.options = $.extend({}, Affix.DEFAULTS, options)
this.$window = $(window)
.on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
.on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
this.$element = $(element)
this.affixed =
this.unpin =
this.pinnedOffset = null
this.checkPosition()
}
Affix.RESET = 'affix affix-top affix-bottom'
Affix.DEFAULTS = {
offset: 0
}
Affix.prototype.getPinnedOffset = function () {
if (this.pinnedOffset) return this.pinnedOffset
this.$element.removeClass(Affix.RESET).addClass('affix')
var scrollTop = this.$window.scrollTop()
var position = this.$element.offset()
return (this.pinnedOffset = position.top - scrollTop)
}
Affix.prototype.checkPositionWithEventLoop = function () {
setTimeout($.proxy(this.checkPosition, this), 1)
}
Affix.prototype.checkPosition = function () {
if (!this.$element.is(':visible')) return
var scrollHeight = $(document).height()
var scrollTop = this.$window.scrollTop()
var position = this.$element.offset()
var offset = this.options.offset
var offsetTop = offset.top
var offsetBottom = offset.bottom
if (this.affixed == 'top') position.top += scrollTop
if (typeof offset != 'object') offsetBottom = offsetTop = offset
if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
var affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ? false :
offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
offsetTop != null && (scrollTop <= offsetTop) ? 'top' : false
if (this.affixed === affix) return
if (this.unpin) this.$element.css('top', '')
var affixType = 'affix' + (affix ? '-' + affix : '')
var e = $.Event(affixType + '.bs.affix')
this.$element.trigger(e)
if (e.isDefaultPrevented()) return
this.affixed = affix
this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
this.$element
.removeClass(Affix.RESET)
.addClass(affixType)
.trigger($.Event(affixType.replace('affix', 'affixed')))
if (affix == 'bottom') {
this.$element.offset({ top: scrollHeight - offsetBottom - this.$element.height() })
}
}
// AFFIX PLUGIN DEFINITION
// =======================
var old = $.fn.affix
$.fn.affix = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.affix')
var options = typeof option == 'object' && option
if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.affix.Constructor = Affix
// AFFIX NO CONFLICT
// =================
$.fn.affix.noConflict = function () {
$.fn.affix = old
return this
}
// AFFIX DATA-API
// ==============
$(window).on('load', function () {
$('[data-spy="affix"]').each(function () {
var $spy = $(this)
var data = $spy.data()
data.offset = data.offset || {}
if (data.offsetBottom) data.offset.bottom = data.offsetBottom
if (data.offsetTop) data.offset.top = data.offsetTop
$spy.affix(data)
})
})
}(jQuery);
// Underscore.js 1.5.2
// http://underscorejs.org
// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `exports` on the server.
var root = this;
// Save the previous value of the `_` variable.
var previousUnderscore = root._;
// Establish the object that gets returned to break out of a loop iteration.
var breaker = {};
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
// Create quick reference variables for speed access to core prototypes.
var
push = ArrayProto.push,
slice = ArrayProto.slice,
concat = ArrayProto.concat,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
// All **ECMAScript 5** native function implementations that we hope to use
// are declared here.
var
nativeForEach = ArrayProto.forEach,
nativeMap = ArrayProto.map,
nativeReduce = ArrayProto.reduce,
nativeReduceRight = ArrayProto.reduceRight,
nativeFilter = ArrayProto.filter,
nativeEvery = ArrayProto.every,
nativeSome = ArrayProto.some,
nativeIndexOf = ArrayProto.indexOf,
nativeLastIndexOf = ArrayProto.lastIndexOf,
nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind;
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `_` as a global object via a string identifier,
// for Closure Compiler "advanced" mode.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
// Current version.
_.VERSION = '1.5.2';
// Collection Functions
// --------------------
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = _.each = _.forEach = function(obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
};
// Return the results of applying the iterator to each element.
// Delegates to **ECMAScript 5**'s native `map` if available.
_.map = _.collect = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
each(obj, function(value, index, list) {
results.push(iterator.call(context, value, index, list));
});
return results;
};
var reduceError = 'Reduce of empty array with no initial value';
// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduce && obj.reduce === nativeReduce) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
}
each(obj, function(value, index, list) {
if (!initial) {
memo = value;
initial = true;
} else {
memo = iterator.call(context, memo, value, index, list);
}
});
if (!initial) throw new TypeError(reduceError);
return memo;
};
// The right-associative version of reduce, also known as `foldr`.
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
}
var length = obj.length;
if (length !== +length) {
var keys = _.keys(obj);
length = keys.length;
}
each(obj, function(value, index, list) {
index = keys ? keys[--length] : --length;
if (!initial) {
memo = obj[index];
initial = true;
} else {
memo = iterator.call(context, memo, obj[index], index, list);
}
});
if (!initial) throw new TypeError(reduceError);
return memo;
};
// Return the first value which passes a truth test. Aliased as `detect`.
_.find = _.detect = function(obj, iterator, context) {
var result;
any(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) {
result = value;
return true;
}
});
return result;
};
// Return all the elements that pass a truth test.
// Delegates to **ECMAScript 5**'s native `filter` if available.
// Aliased as `select`.
_.filter = _.select = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) results.push(value);
});
return results;
};
// Return all the elements for which a truth test fails.
_.reject = function(obj, iterator, context) {
return _.filter(obj, function(value, index, list) {
return !iterator.call(context, value, index, list);
}, context);
};
// Determine whether all of the elements match a truth test.
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
_.every = _.all = function(obj, iterator, context) {
iterator || (iterator = _.identity);
var result = true;
if (obj == null) return result;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};
// Determine if at least one element in the object matches a truth test.
// Delegates to **ECMAScript 5**'s native `some` if available.
// Aliased as `any`.
var any = _.some = _.any = function(obj, iterator, context) {
iterator || (iterator = _.identity);
var result = false;
if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
each(obj, function(value, index, list) {
if (result || (result = iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};
// Determine if the array or object contains a given value (using `===`).
// Aliased as `include`.
_.contains = _.include = function(obj, target) {
if (obj == null) return false;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
return any(obj, function(value) {
return value === target;
});
};
// Invoke a method (with arguments) on every item in a collection.
_.invoke = function(obj, method) {
var args = slice.call(arguments, 2);
var isFunc = _.isFunction(method);
return _.map(obj, function(value) {
return (isFunc ? method : value[method]).apply(value, args);
});
};
// Convenience version of a common use case of `map`: fetching a property.
_.pluck = function(obj, key) {
return _.map(obj, function(value){ return value[key]; });
};
// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_.where = function(obj, attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return _[first ? 'find' : 'filter'](obj, function(value) {
for (var key in attrs) {
if (attrs[key] !== value[key]) return false;
}
return true;
});
};
// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
_.findWhere = function(obj, attrs) {
return _.where(obj, attrs, true);
};
// Return the maximum element or (element-based computation).
// Can't optimize arrays of integers longer than 65,535 elements.
// See [WebKit Bug 80797](https://bugs.webkit.org/show_bug.cgi?id=80797)
_.max = function(obj, iterator, context) {
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
return Math.max.apply(Math, obj);
}
if (!iterator && _.isEmpty(obj)) return -Infinity;
var result = {computed : -Infinity, value: -Infinity};
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
computed > result.computed && (result = {value : value, computed : computed});
});
return result.value;
};
// Return the minimum element (or element-based computation).
_.min = function(obj, iterator, context) {
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
return Math.min.apply(Math, obj);
}
if (!iterator && _.isEmpty(obj)) return Infinity;
var result = {computed : Infinity, value: Infinity};
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
computed < result.computed && (result = {value : value, computed : computed});
});
return result.value;
};
// Shuffle an array, using the modern version of the
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
_.shuffle = function(obj) {
var rand;
var index = 0;
var shuffled = [];
each(obj, function(value) {
rand = _.random(index++);
shuffled[index - 1] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;
};
// Sample **n** random values from an array.
// If **n** is not specified, returns a single random element from the array.
// The internal `guard` argument allows it to work with `map`.
_.sample = function(obj, n, guard) {
if (arguments.length < 2 || guard) {
return obj[_.random(obj.length - 1)];
}
return _.shuffle(obj).slice(0, Math.max(0, n));
};
// An internal function to generate lookup iterators.
var lookupIterator = function(value) {
return _.isFunction(value) ? value : function(obj){ return obj[value]; };
};
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = function(obj, value, context) {
var iterator = lookupIterator(value);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value: value,
index: index,
criteria: iterator.call(context, value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}), 'value');
};
// An internal function used for aggregate "group by" operations.
var group = function(behavior) {
return function(obj, value, context) {
var result = {};
var iterator = value == null ? _.identity : lookupIterator(value);
each(obj, function(value, index) {
var key = iterator.call(context, value, index, obj);
behavior(result, key, value);
});
return result;
};
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_.groupBy = group(function(result, key, value) {
(_.has(result, key) ? result[key] : (result[key] = [])).push(value);
});
// Indexes the object's values by a criterion, similar to `groupBy`, but for
// when you know that your index values will be unique.
_.indexBy = group(function(result, key, value) {
result[key] = value;
});
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
_.countBy = group(function(result, key) {
_.has(result, key) ? result[key]++ : result[key] = 1;
});
// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator, context) {
iterator = iterator == null ? _.identity : lookupIterator(iterator);
var value = iterator.call(context, obj);
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
iterator.call(context, array[mid]) < value ? low = mid + 1 : high = mid;
}
return low;
};
// Safely create a real, live array from anything iterable.
_.toArray = function(obj) {
if (!obj) return [];
if (_.isArray(obj)) return slice.call(obj);
if (obj.length === +obj.length) return _.map(obj, _.identity);
return _.values(obj);
};
// Return the number of elements in an object.
_.size = function(obj) {
if (obj == null) return 0;
return (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
};
// Array Functions
// ---------------
// Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head` and `take`. The **guard** check
// allows it to work with `_.map`.
_.first = _.head = _.take = function(array, n, guard) {
if (array == null) return void 0;
return (n == null) || guard ? array[0] : slice.call(array, 0, n);
};
// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N. The **guard** check allows it to work with
// `_.map`.
_.initial = function(array, n, guard) {
return slice.call(array, 0, array.length - ((n == null) || guard ? 1 : n));
};
// Get the last element of an array. Passing **n** will return the last N
// values in the array. The **guard** check allows it to work with `_.map`.
_.last = function(array, n, guard) {
if (array == null) return void 0;
if ((n == null) || guard) {
return array[array.length - 1];
} else {
return slice.call(array, Math.max(array.length - n, 0));
}
};
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
// Especially useful on the arguments object. Passing an **n** will return
// the rest N values in the array. The **guard**
// check allows it to work with `_.map`.
_.rest = _.tail = _.drop = function(array, n, guard) {
return slice.call(array, (n == null) || guard ? 1 : n);
};
// Trim out all falsy values from an array.
_.compact = function(array) {
return _.filter(array, _.identity);
};
// Internal implementation of a recursive `flatten` function.
var flatten = function(input, shallow, output) {
if (shallow && _.every(input, _.isArray)) {
return concat.apply(output, input);
}
each(input, function(value) {
if (_.isArray(value) || _.isArguments(value)) {
shallow ? push.apply(output, value) : flatten(value, shallow, output);
} else {
output.push(value);
}
});
return output;
};
// Flatten out an array, either recursively (by default), or just one level.
_.flatten = function(array, shallow) {
return flatten(array, shallow, []);
};
// Return a version of the array that does not contain the specified value(s).
_.without = function(array) {
return _.difference(array, slice.call(arguments, 1));
};
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iterator, context) {
if (_.isFunction(isSorted)) {
context = iterator;
iterator = isSorted;
isSorted = false;
}
var initial = iterator ? _.map(array, iterator, context) : array;
var results = [];
var seen = [];
each(initial, function(value, index) {
if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.contains(seen, value)) {
seen.push(value);
results.push(array[index]);
}
});
return results;
};
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
return _.uniq(_.flatten(arguments, true));
};
// Produce an array that contains every item shared between all the
// passed-in arrays.
_.intersection = function(array) {
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
return _.indexOf(other, item) >= 0;
});
});
};
// Take the difference between one array and a number of other arrays.
// Only the elements present in just the first array will remain.
_.difference = function(array) {
var rest = concat.apply(ArrayProto, slice.call(arguments, 1));
return _.filter(array, function(value){ return !_.contains(rest, value); });
};
// Zip together multiple lists into a single array -- elements that share
// an index go together.
_.zip = function() {
var length = _.max(_.pluck(arguments, "length").concat(0));
var results = new Array(length);
for (var i = 0; i < length; i++) {
results[i] = _.pluck(arguments, '' + i);
}
return results;
};
// Converts lists into objects. Pass either a single array of `[key, value]`
// pairs, or two parallel arrays of the same length -- one of keys, and one of
// the corresponding values.
_.object = function(list, values) {
if (list == null) return {};
var result = {};
for (var i = 0, length = list.length; i < length; i++) {
if (values) {
result[list[i]] = values[i];
} else {
result[list[i][0]] = list[i][1];
}
}
return result;
};
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
// we need this function. Return the position of the first occurrence of an
// item in an array, or -1 if the item is not included in the array.
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
// If the array is large and already in sort order, pass `true`
// for **isSorted** to use binary search.
_.indexOf = function(array, item, isSorted) {
if (array == null) return -1;
var i = 0, length = array.length;
if (isSorted) {
if (typeof isSorted == 'number') {
i = (isSorted < 0 ? Math.max(0, length + isSorted) : isSorted);
} else {
i = _.sortedIndex(array, item);
return array[i] === item ? i : -1;
}
}
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
for (; i < length; i++) if (array[i] === item) return i;
return -1;
};
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
_.lastIndexOf = function(array, item, from) {
if (array == null) return -1;
var hasIndex = from != null;
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) {
return hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item);
}
var i = (hasIndex ? from : array.length);
while (i--) if (array[i] === item) return i;
return -1;
};
// Generate an integer Array containing an arithmetic progression. A port of
// the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range).
_.range = function(start, stop, step) {
if (arguments.length <= 1) {
stop = start || 0;
start = 0;
}
step = arguments[2] || 1;
var length = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0;
var range = new Array(length);
while(idx < length) {
range[idx++] = start;
start += step;
}
return range;
};
// Function (ahem) Functions
// ------------------
// Reusable constructor function for prototype setting.
var ctor = function(){};
// Create a function bound to a given object (assigning `this`, and arguments,
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
// available.
_.bind = function(func, context) {
var args, bound;
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError;
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
ctor.prototype = null;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) return result;
return self;
};
};
// Partially apply a function by creating a version that has had some of its
// arguments pre-filled, without changing its dynamic `this` context.
_.partial = function(func) {
var args = slice.call(arguments, 1);
return function() {
return func.apply(this, args.concat(slice.call(arguments)));
};
};
// Bind all of an object's methods to that object. Useful for ensuring that
// all callbacks defined on an object belong to it.
_.bindAll = function(obj) {
var funcs = slice.call(arguments, 1);
if (funcs.length === 0) throw new Error("bindAll must be passed function names");
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
return obj;
};
// Memoize an expensive function by storing its results.
_.memoize = function(func, hasher) {
var memo = {};
hasher || (hasher = _.identity);
return function() {
var key = hasher.apply(this, arguments);
return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};
// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
var args = slice.call(arguments, 2);
return setTimeout(function(){ return func.apply(null, args); }, wait);
};
// Defers a function, scheduling it to run after the current call stack has
// cleared.
_.defer = function(func) {
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
};
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
options || (options = {});
var later = function() {
previous = options.leading === false ? 0 : new Date;
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date;
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
return function() {
context = this;
args = arguments;
timestamp = new Date();
var later = function() {
var last = (new Date()) - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) result = func.apply(context, args);
}
};
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) result = func.apply(context, args);
return result;
};
};
// Returns a function that will be executed at most one time, no matter how
// often you call it. Useful for lazy initialization.
_.once = function(func) {
var ran = false, memo;
return function() {
if (ran) return memo;
ran = true;
memo = func.apply(this, arguments);
func = null;
return memo;
};
};
// Returns the first function passed as an argument to the second,
// allowing you to adjust arguments, run code before and after, and
// conditionally execute the original function.
_.wrap = function(func, wrapper) {
return function() {
var args = [func];
push.apply(args, arguments);
return wrapper.apply(this, args);
};
};
// Returns a function that is the composition of a list of functions, each
// consuming the return value of the function that follows.
_.compose = function() {
var funcs = arguments;
return function() {
var args = arguments;
for (var i = funcs.length - 1; i >= 0; i--) {
args = [funcs[i].apply(this, args)];
}
return args[0];
};
};
// Returns a function that will only be executed after being called N times.
_.after = function(times, func) {
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};
// Object Functions
// ----------------
// Retrieve the names of an object's properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
};
// Retrieve the values of an object's properties.
_.values = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var values = new Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
};
// Convert an object into a list of `[key, value]` pairs.
_.pairs = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var pairs = new Array(length);
for (var i = 0; i < length; i++) {
pairs[i] = [keys[i], obj[keys[i]]];
}
return pairs;
};
// Invert the keys and values of an object. The values must be serializable.
_.invert = function(obj) {
var result = {};
var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
};
// Return a sorted list of the function names available on the object.
// Aliased as `methods`
_.functions = _.methods = function(obj) {
var names = [];
for (var key in obj) {
if (_.isFunction(obj[key])) names.push(key);
}
return names.sort();
};
// Extend a given object with all the properties in passed-in object(s).
_.extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
// Return a copy of the object only containing the whitelisted properties.
_.pick = function(obj) {
var copy = {};
var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
each(keys, function(key) {
if (key in obj) copy[key] = obj[key];
});
return copy;
};
// Return a copy of the object without the blacklisted properties.
_.omit = function(obj) {
var copy = {};
var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
for (var key in obj) {
if (!_.contains(keys, key)) copy[key] = obj[key];
}
return copy;
};
// Fill in a given object with default properties.
_.defaults = function(obj) {
each(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
if (obj[prop] === void 0) obj[prop] = source[prop];
}
}
});
return obj;
};
// Create a (shallow-cloned) duplicate of an object.
_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};
// Invokes interceptor with the obj, and then returns obj.
// The primary purpose of this method is to "tap into" a method chain, in
// order to perform operations on intermediate results within the chain.
_.tap = function(obj, interceptor) {
interceptor(obj);
return obj;
};
// Internal recursive comparison function for `isEqual`.
var eq = function(a, b, aStack, bStack) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
if (a === b) return a !== 0 || 1 / a == 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a == null || b == null) return a === b;
// Unwrap any wrapped objects.
if (a instanceof _) a = a._wrapped;
if (b instanceof _) b = b._wrapped;
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className != toString.call(b)) return false;
switch (className) {
// Strings, numbers, dates, and booleans are compared by value.
case '[object String]':
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
// equivalent to `new String("5")`.
return a == String(b);
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
// other numeric values.
return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a == +b;
// RegExps are compared by their source patterns and flags.
case '[object RegExp]':
return a.source == b.source &&
a.global == b.global &&
a.multiline == b.multiline &&
a.ignoreCase == b.ignoreCase;
}
if (typeof a != 'object' || typeof b != 'object') return false;
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] == a) return bStack[length] == b;
}
// Objects with different constructors are not equivalent, but `Object`s
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
_.isFunction(bCtor) && (bCtor instanceof bCtor))) {
return false;
}
// Add the first object to the stack of traversed objects.
aStack.push(a);
bStack.push(b);
var size = 0, result = true;
// Recursively compare objects and arrays.
if (className == '[object Array]') {
// Compare array lengths to determine if a deep comparison is necessary.
size = a.length;
result = size == b.length;
if (result) {
// Deep compare the contents, ignoring non-numeric properties.
while (size--) {
if (!(result = eq(a[size], b[size], aStack, bStack))) break;
}
}
} else {
// Deep compare objects.
for (var key in a) {
if (_.has(a, key)) {
// Count the expected number of properties.
size++;
// Deep compare each member.
if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
}
}
// Ensure that both objects contain the same number of properties.
if (result) {
for (key in b) {
if (_.has(b, key) && !(size--)) break;
}
result = !size;
}
}
// Remove the first object from the stack of traversed objects.
aStack.pop();
bStack.pop();
return result;
};
// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
return eq(a, b, [], []);
};
// Is a given array, string, or object empty?
// An "empty" object has no enumerable own-properties.
_.isEmpty = function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (_.has(obj, key)) return false;
return true;
};
// Is a given value a DOM element?
_.isElement = function(obj) {
return !!(obj && obj.nodeType === 1);
};
// Is a given value an array?
// Delegates to ECMA5's native Array.isArray
_.isArray = nativeIsArray || function(obj) {
return toString.call(obj) == '[object Array]';
};
// Is a given variable an object?
_.isObject = function(obj) {
return obj === Object(obj);
};
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
_['is' + name] = function(obj) {
return toString.call(obj) == '[object ' + name + ']';
};
});
// Define a fallback version of the method in browsers (ahem, IE), where
// there isn't any inspectable "Arguments" type.
if (!_.isArguments(arguments)) {
_.isArguments = function(obj) {
return !!(obj && _.has(obj, 'callee'));
};
}
// Optimize `isFunction` if appropriate.
if (typeof (/./) !== 'function') {
_.isFunction = function(obj) {
return typeof obj === 'function';
};
}
// Is a given object a finite number?
_.isFinite = function(obj) {
return isFinite(obj) && !isNaN(parseFloat(obj));
};
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
_.isNaN = function(obj) {
return _.isNumber(obj) && obj != +obj;
};
// Is a given value a boolean?
_.isBoolean = function(obj) {
return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
};
// Is a given value equal to null?
_.isNull = function(obj) {
return obj === null;
};
// Is a given variable undefined?
_.isUndefined = function(obj) {
return obj === void 0;
};
// Shortcut function for checking if an object has a given property directly
// on itself (in other words, not on a prototype).
_.has = function(obj, key) {
return hasOwnProperty.call(obj, key);
};
// Utility Functions
// -----------------
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
// previous owner. Returns a reference to the Underscore object.
_.noConflict = function() {
root._ = previousUnderscore;
return this;
};
// Keep the identity function around for default iterators.
_.identity = function(value) {
return value;
};
// Run a function **n** times.
_.times = function(n, iterator, context) {
var accum = Array(Math.max(0, n));
for (var i = 0; i < n; i++) accum[i] = iterator.call(context, i);
return accum;
};
// Return a random integer between min and max (inclusive).
_.random = function(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
};
// List of HTML entities for escaping.
var entityMap = {
escape: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}
};
entityMap.unescape = _.invert(entityMap.escape);
// Regexes containing the keys and values listed immediately above.
var entityRegexes = {
escape: new RegExp('[' + _.keys(entityMap.escape).join('') + ']', 'g'),
unescape: new RegExp('(' + _.keys(entityMap.unescape).join('|') + ')', 'g')
};
// Functions for escaping and unescaping strings to/from HTML interpolation.
_.each(['escape', 'unescape'], function(method) {
_[method] = function(string) {
if (string == null) return '';
return ('' + string).replace(entityRegexes[method], function(match) {
return entityMap[method][match];
});
};
});
// If the value of the named `property` is a function then invoke it with the
// `object` as context; otherwise, return it.
_.result = function(object, property) {
if (object == null) return void 0;
var value = object[property];
return _.isFunction(value) ? value.call(object) : value;
};
// Add your own custom functions to the Underscore object.
_.mixin = function(obj) {
each(_.functions(obj), function(name) {
var func = _[name] = obj[name];
_.prototype[name] = function() {
var args = [this._wrapped];
push.apply(args, arguments);
return result.call(this, func.apply(_, args));
};
});
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
var idCounter = 0;
_.uniqueId = function(prefix) {
var id = ++idCounter + '';
return prefix ? prefix + id : id;
};
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(text, data, settings) {
var render;
settings = _.defaults({}, settings, _.templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = new RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
}
if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
}
if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
index = offset + match.length;
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + "return __p;\n";
try {
render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
}
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
};
// Provide the compiled function source as a convenience for precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
// Add a "chain" function, which will delegate to the wrapper.
_.chain = function(obj) {
return _(obj).chain();
};
// OOP
// ---------------
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
// Helper function to continue chaining intermediate results.
var result = function(obj) {
return this._chain ? _(obj).chain() : obj;
};
// Add all of the Underscore functions to the wrapper object.
_.mixin(_);
// Add all mutator Array functions to the wrapper.
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
var method = ArrayProto[name];
_.prototype[name] = function() {
var obj = this._wrapped;
method.apply(obj, arguments);
if ((name == 'shift' || name == 'splice') && obj.length === 0) delete obj[0];
return result.call(this, obj);
};
});
// Add all accessor Array functions to the wrapper.
each(['concat', 'join', 'slice'], function(name) {
var method = ArrayProto[name];
_.prototype[name] = function() {
return result.call(this, method.apply(this._wrapped, arguments));
};
});
_.extend(_.prototype, {
// Start chaining a wrapped Underscore object.
chain: function() {
this._chain = true;
return this;
},
// Extracts the result from a wrapped and chained object.
value: function() {
return this._wrapped;
}
});
}).call(this);
/**
* autoNumeric.js
* @author: Bob Knothe
* @author: Sokolov Yura aka funny_falcon
* @version: 1.9.17 - 2013-12-03 GMT 9:00 PM
*
* Created by Robert J. Knothe on 2010-10-25. Please report any bugs to https://github.com/BobKnothe/autoNumeric
* Created by Sokolov Yura on 2010-11-07
*
* Copyright (c) 2011 Robert J. Knothe http://www.decorplanit.com/plugin/
*
* The MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
(function ($) {
"use strict";
/*jslint browser: true*/
/*global jQuery: false*/
/* Cross browser routine for getting selected range/cursor position
*/
function getElementSelection(that) {
var position = {};
if (that.selectionStart === undefined) {
that.focus();
var select = document.selection.createRange();
position.length = select.text.length;
select.moveStart('character', -that.value.length);
position.end = select.text.length;
position.start = position.end - position.length;
} else {
position.start = that.selectionStart;
position.end = that.selectionEnd;
position.length = position.end - position.start;
}
return position;
}
/**
* Cross browser routine for setting selected range/cursor position
*/
function setElementSelection(that, start, end) {
if (that.selectionStart === undefined) {
that.focus();
var r = that.createTextRange();
r.collapse(true);
r.moveEnd('character', end);
r.moveStart('character', start);
r.select();
} else {
that.selectionStart = start;
that.selectionEnd = end;
}
}
/**
* run callbacks in parameters if any
* any parameter could be a callback:
* - a function, which invoked with jQuery element, parameters and this parameter name and returns parameter value
* - a name of function, attached to $(selector).autoNumeric.functionName(){} - which was called previously
*/
function runCallbacks($this, settings) {
/**
* loops through the settings object (option array) to find the following
* k = option name example k=aNum
* val = option value example val=0123456789
*/
$.each(settings, function (k, val) {
if (typeof val === 'function') {
settings[k] = val($this, settings, k);
} else if (typeof $this.autoNumeric[val] === 'function') {
/**
* calls the attached function from the html5 data example: data-a-sign="functionName"
*/
settings[k] = $this.autoNumeric[val]($this, settings, k);
}
});
}
function convertKeyToNumber(settings, key) {
if (typeof (settings[key]) === 'string') {
settings[key] *= 1;
}
}
/**
* Preparing user defined options for further usage
* merge them with defaults appropriately
*/
function autoCode($this, settings) {
runCallbacks($this, settings);
settings.oEvent = null;
settings.tagList = ['B', 'CAPTION', 'CITE', 'CODE', 'DD', 'DEL', 'DIV', 'DFN', 'DT', 'EM', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'INS', 'KDB', 'LABEL', 'LI', 'OUTPUT', 'P', 'Q', 'S', 'SAMPLE', 'SPAN', 'STRONG', 'TD', 'TH', 'U', 'VAR'];
var vmax = settings.vMax.toString().split('.'),
vmin = (!settings.vMin && settings.vMin !== 0) ? [] : settings.vMin.toString().split('.');
convertKeyToNumber(settings, 'vMax');
convertKeyToNumber(settings, 'vMin');
convertKeyToNumber(settings, 'mDec'); /** set mDec if not defained by user */
settings.allowLeading = true;
settings.aNeg = settings.vMin < 0 ? '-' : '';
vmax[0] = vmax[0].replace('-', '');
vmin[0] = vmin[0].replace('-', '');
settings.mInt = Math.max(vmax[0].length, vmin[0].length, 1);
if (settings.mDec === null) {
var vmaxLength = 0,
vminLength = 0;
if (vmax[1]) {
vmaxLength = vmax[1].length;
}
if (vmin[1]) {
vminLength = vmin[1].length;
}
settings.mDec = Math.max(vmaxLength, vminLength);
} /** set alternative decimal separator key */
if (settings.altDec === null && settings.mDec > 0) {
if (settings.aDec === '.' && settings.aSep !== ',') {
settings.altDec = ',';
} else if (settings.aDec === ',' && settings.aSep !== '.') {
settings.altDec = '.';
}
}
/** cache regexps for autoStrip */
var aNegReg = settings.aNeg ? '([-\\' + settings.aNeg + ']?)' : '(-?)';
settings.aNegRegAutoStrip = aNegReg;
settings.skipFirstAutoStrip = new RegExp(aNegReg + '[^-' + (settings.aNeg ? '\\' + settings.aNeg : '') + '\\' + settings.aDec + '\\d]' + '.*?(\\d|\\' + settings.aDec + '\\d)');
settings.skipLastAutoStrip = new RegExp('(\\d\\' + settings.aDec + '?)[^\\' + settings.aDec + '\\d]\\D*$');
var allowed = '-' + settings.aNum + '\\' + settings.aDec;
settings.allowedAutoStrip = new RegExp('[^' + allowed + ']', 'gi');
settings.numRegAutoStrip = new RegExp(aNegReg + '(?:\\' + settings.aDec + '?(\\d+\\' + settings.aDec + '\\d+)|(\\d*(?:\\' + settings.aDec + '\\d*)?))');
return settings;
}
/**
* strip all unwanted characters and leave only a number alert
*/
function autoStrip(s, settings, strip_zero) {
if (settings.aSign) { /** remove currency sign */
while (s.indexOf(settings.aSign) > -1) {
s = s.replace(settings.aSign, '');
}
}
s = s.replace(settings.skipFirstAutoStrip, '$1$2'); /** first replace anything before digits */
s = s.replace(settings.skipLastAutoStrip, '$1'); /** then replace anything after digits */
s = s.replace(settings.allowedAutoStrip, ''); /** then remove any uninterested characters */
if (settings.altDec) {
s = s.replace(settings.altDec, settings.aDec);
} /** get only number string */
var m = s.match(settings.numRegAutoStrip);
s = m ? [m[1], m[2], m[3]].join('') : '';
if ((settings.lZero === 'allow' || settings.lZero === 'keep') && strip_zero !== 'strip') {
var parts = [],
nSign = '';
parts = s.split(settings.aDec);
if (parts[0].indexOf('-') !== -1) {
nSign = '-';
parts[0] = parts[0].replace('-', '');
}
if (parts[0].length > settings.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */
parts[0] = parts[0].slice(1);
}
s = nSign + parts.join(settings.aDec);
}
if ((strip_zero && settings.lZero === 'deny') || (strip_zero && settings.lZero === 'allow' && settings.allowLeading === false)) {
var strip_reg = '^' + settings.aNegRegAutoStrip + '0*(\\d' + (strip_zero === 'leading' ? ')' : '|$)');
strip_reg = new RegExp(strip_reg);
s = s.replace(strip_reg, '$1$2');
}
return s;
}
/**
* places or removes brackets on negative values
*/
function negativeBracket(s, nBracket, oEvent) { /** oEvent = settings.oEvent */
nBracket = nBracket.split(',');
if (oEvent === 'set' || oEvent === 'focusout') {
s = s.replace('-', '');
s = nBracket[0] + s + nBracket[1];
} else if ((oEvent === 'get' || oEvent === 'focusin' || oEvent === 'pageLoad') && s.charAt(0) === nBracket[0]) {
s = s.replace(nBracket[0], '-');
s = s.replace(nBracket[1], '');
}
return s;
}
/**
* truncate decimal part of a number
*/
function truncateDecimal(s, aDec, mDec) {
if (aDec && mDec) {
var parts = s.split(aDec);
/** truncate decimal part to satisfying length
* cause we would round it anyway */
if (parts[1] && parts[1].length > mDec) {
if (mDec > 0) {
parts[1] = parts[1].substring(0, mDec);
s = parts.join(aDec);
} else {
s = parts[0];
}
}
}
return s;
}
/**
* prepare number string to be converted to real number
*/
function fixNumber(s, aDec, aNeg) {
if (aDec && aDec !== '.') {
s = s.replace(aDec, '.');
}
if (aNeg && aNeg !== '-') {
s = s.replace(aNeg, '-');
}
if (!s.match(/\d/)) {
s += '0';
}
return s;
}
/**
* function to handle numbers less than 0 that are stored in Exponential notation ex: .0000001 stored as 1e-7
*/
function checkValue(value, settings) {
var decimal = value.indexOf('.'),
checkSmall = +value;
if (decimal !== -1) {
if (checkSmall < 0.000001 && checkSmall > -1) {
value = +value;
if (value < 0.000001 && value > 0) {
value = (value + 10).toString();
value = value.substring(1);
}
if (value < 0 && value > -1) {
value = (value - 10).toString();
value = '-' + value.substring(2);
}
value = value.toString();
} else {
var parts = value.split('.');
if (parts[1] !== undefined) {
if (+parts[1] === 0) {
value = parts[0];
} else {
parts[1] = parts[1].replace(/0*$/, '');
value = parts.join('.');
}
}
}
}
return (settings.lZero === 'keep') ? value : value.replace(/^0*(\d)/, '$1');
}
/**
* prepare real number to be converted to our format
*/
function presentNumber(s, aDec, aNeg) {
if (aNeg && aNeg !== '-') {
s = s.replace('-', aNeg);
}
if (aDec && aDec !== '.') {
s = s.replace('.', aDec);
}
return s;
}
/**
* checking that number satisfy format conditions
* and lays between settings.vMin and settings.vMax
* and the string length does not exceed the digits in settings.vMin and settings.vMax
*/
function autoCheck(s, settings) {
s = autoStrip(s, settings);
s = truncateDecimal(s, settings.aDec, settings.mDec);
s = fixNumber(s, settings.aDec, settings.aNeg);
var value = +s;
if (settings.oEvent === 'set' && (value < settings.vMin || value > settings.vMax)) {
$.error("The value (" + value + ") from the 'set' method falls outside of the vMin / vMax range");
}
return value >= settings.vMin && value <= settings.vMax;
}
/**
* private function to check for empty value
*/
function checkEmpty(iv, settings, signOnEmpty) {
if (iv === '' || iv === settings.aNeg) {
if (settings.wEmpty === 'zero') {
return iv + '0';
}
if (settings.wEmpty === 'sign' || signOnEmpty) {
return iv + settings.aSign;
}
return iv;
}
return null;
}
/**
* private function that formats our number
*/
function autoGroup(iv, settings) {
iv = autoStrip(iv, settings);
var testNeg = iv.replace(',', '.'),
empty = checkEmpty(iv, settings, true);
if (empty !== null) {
return empty;
}
var digitalGroup = '';
if (settings.dGroup === 2) {
digitalGroup = /(\d)((\d)(\d{2}?)+)$/;
} else if (settings.dGroup === 4) {
digitalGroup = /(\d)((\d{4}?)+)$/;
} else {
digitalGroup = /(\d)((\d{3}?)+)$/;
} /** splits the string at the decimal string */
var ivSplit = iv.split(settings.aDec);
if (settings.altDec && ivSplit.length === 1) {
ivSplit = iv.split(settings.altDec);
} /** assigns the whole number to the a varibale (s) */
var s = ivSplit[0];
if (settings.aSep) {
while (digitalGroup.test(s)) { /** re-inserts the thousand sepparator via a regualer expression */
s = s.replace(digitalGroup, '$1' + settings.aSep + '$2');
}
}
if (settings.mDec !== 0 && ivSplit.length > 1) {
if (ivSplit[1].length > settings.mDec) {
ivSplit[1] = ivSplit[1].substring(0, settings.mDec);
} /** joins the whole number with the deciaml value */
iv = s + settings.aDec + ivSplit[1];
} else { /** if whole numbers only */
iv = s;
}
if (settings.aSign) {
var has_aNeg = iv.indexOf(settings.aNeg) !== -1;
iv = iv.replace(settings.aNeg, '');
iv = settings.pSign === 'p' ? settings.aSign + iv : iv + settings.aSign;
if (has_aNeg) {
iv = settings.aNeg + iv;
}
}
if (settings.oEvent === 'set' && testNeg < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */
iv = negativeBracket(iv, settings.nBracket, settings.oEvent);
}
return iv;
}
/**
* round number after setting by pasting or $().autoNumericSet()
* private function for round the number
* please note this handled as text - JavaScript math function can return inaccurate values
* also this offers multiple rounding methods that are not easily accomplished in JavaScript
*/
function autoRound(iv, settings) { /** value to string */
iv = (iv === '') ? '0' : iv.toString();
convertKeyToNumber(settings, 'mDec'); /** set mDec to number needed when mDec set by 'update method */
var ivRounded = '',
i = 0,
nSign = '',
rDec = (typeof (settings.aPad) === 'boolean' || settings.aPad === null) ? (settings.aPad ? settings.mDec : 0) : +settings.aPad;
var truncateZeros = function (ivRounded) { /** truncate not needed zeros */
var regex = rDec === 0 ? (/(\.[1-9]*)0*$/) : rDec === 1 ? (/(\.\d[1-9]*)0*$/) : new RegExp('(\\.\\d{' + rDec + '}[1-9]*)0*$');
ivRounded = ivRounded.replace(regex, '$1'); /** If there are no decimal places, we don't need a decimal point at the end */
if (rDec === 0) {
ivRounded = ivRounded.replace(/\.$/, '');
}
return ivRounded;
};
if (iv.charAt(0) === '-') { /** Checks if the iv (input Value)is a negative value */
nSign = '-'; /** removes the negative sign will be added back later if required */
iv = iv.replace('-', '');
} /** prepend a zero if first character is not a digit (then it is likely to be a dot)*/
if (!iv.match(/^\d/)) {
iv = '0' + iv;
} /** determines if the value is zero - if zero no negative sign */
if (nSign === '-' && +iv === 0) {
nSign = '';
}
if ((+iv > 0 && settings.lZero !== 'keep') || (iv.length > 0 && settings.lZero === 'allow')) { /** trims leading zero's if needed */
iv = iv.replace(/^0*(\d)/, '$1');
}
var dPos = iv.lastIndexOf('.'), /** virtual decimal position */
vdPos = dPos === -1 ? iv.length - 1 : dPos, /** checks decimal places to determine if rounding is required */
cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */
if (cDec <= settings.mDec) {
ivRounded = iv; /** check if we need to pad with zeros */
if (cDec < rDec) {
if (dPos === -1) {
ivRounded += '.';
}
while (cDec < rDec) {
var zeros = '000000'.substring(0, rDec - cDec);
ivRounded += zeros;
cDec += zeros.length;
}
} else if (cDec > rDec) {
ivRounded = truncateZeros(ivRounded);
} else if (cDec === 0 && rDec === 0) {
ivRounded = ivRounded.replace(/\.$/, '');
}
return nSign + ivRounded;
} /** rounded length of the string after rounding */
var rLength = dPos + settings.mDec, /** test round */
tRound = +iv.charAt(rLength + 1),
ivArray = iv.substring(0, rLength + 1).split(''),
odd = (iv.charAt(rLength) === '.') ? (iv.charAt(rLength - 1) % 2) : (iv.charAt(rLength) % 2);
if ((tRound > 4 && settings.mRound === 'S') || (tRound > 4 && settings.mRound === 'A' && nSign === '') || (tRound > 5 && settings.mRound === 'A' && nSign === '-') || (tRound > 5 && settings.mRound === 's') || (tRound > 5 && settings.mRound === 'a' && nSign === '') || (tRound > 4 && settings.mRound === 'a' && nSign === '-') || (tRound > 5 && settings.mRound === 'B') || (tRound === 5 && settings.mRound === 'B' && odd === 1) || (tRound > 0 && settings.mRound === 'C' && nSign === '') || (tRound > 0 && settings.mRound === 'F' && nSign === '-') || (tRound > 0 && settings.mRound === 'U')) {
/** Round up the last digit if required, and continue until no more 9's are found */
for (i = (ivArray.length - 1); i >= 0; i -= 1) {
if (ivArray[i] !== '.') {
ivArray[i] = +ivArray[i] + 1;
if (ivArray[i] < 10) {
break;
}
if (i > 0) {
ivArray[i] = '0';
}
}
}
} /** Reconstruct the string, converting any 10's to 0's */
ivArray = ivArray.slice(0, rLength + 1);
ivRounded = truncateZeros(ivArray.join('')); /** return rounded value */
return (+ivRounded === 0) ? ivRounded : nSign + ivRounded;
}
/**
* Holder object for field properties
*/
function AutoNumericHolder(that, settings) {
this.settings = settings;
this.that = that;
this.$that = $(that);
this.formatted = false;
this.settingsClone = autoCode(this.$that, this.settings);
this.value = that.value;
}
AutoNumericHolder.prototype = {
init: function (e) {
this.value = this.that.value;
this.settingsClone = autoCode(this.$that, this.settings);
this.ctrlKey = e.ctrlKey;
this.cmdKey = e.metaKey;
this.shiftKey = e.shiftKey;
this.selection = getElementSelection(this.that); /** keypress event overwrites meaningful value of e.keyCode */
if (e.type === 'keydown' || e.type === 'keyup') {
this.kdCode = e.keyCode;
}
this.which = e.which;
this.processed = false;
this.formatted = false;
},
setSelection: function (start, end, setReal) {
start = Math.max(start, 0);
end = Math.min(end, this.that.value.length);
this.selection = {
start: start,
end: end,
length: end - start
};
if (setReal === undefined || setReal) {
setElementSelection(this.that, start, end);
}
},
setPosition: function (pos, setReal) {
this.setSelection(pos, pos, setReal);
},
getBeforeAfter: function () {
var value = this.value,
left = value.substring(0, this.selection.start),
right = value.substring(this.selection.end, value.length);
return [left, right];
},
getBeforeAfterStriped: function () {
var parts = this.getBeforeAfter();
parts[0] = autoStrip(parts[0], this.settingsClone);
parts[1] = autoStrip(parts[1], this.settingsClone);
return parts;
},
/**
* strip parts from excess characters and leading zeroes
*/
normalizeParts: function (left, right) {
var settingsClone = this.settingsClone;
right = autoStrip(right, settingsClone); /** if right is not empty and first character is not aDec, */
/** we could strip all zeros, otherwise only leading */
var strip = right.match(/^\d/) ? true : 'leading';
left = autoStrip(left, settingsClone, strip); /** prevents multiple leading zeros from being entered */
if ((left === '' || left === settingsClone.aNeg) && settingsClone.lZero === 'deny') {
if (right > '') {
right = right.replace(/^0*(\d)/, '$1');
}
}
var new_value = left + right; /** insert zero if has leading dot */
if (settingsClone.aDec) {
var m = new_value.match(new RegExp('^' + settingsClone.aNegRegAutoStrip + '\\' + settingsClone.aDec));
if (m) {
left = left.replace(m[1], m[1] + '0');
new_value = left + right;
}
} /** insert zero if number is empty and io.wEmpty == 'zero' */
if (settingsClone.wEmpty === 'zero' && (new_value === settingsClone.aNeg || new_value === '')) {
left += '0';
}
return [left, right];
},
/**
* set part of number to value keeping position of cursor
*/
setValueParts: function (left, right) {
var settingsClone = this.settingsClone,
parts = this.normalizeParts(left, right),
new_value = parts.join(''),
position = parts[0].length;
if (autoCheck(new_value, settingsClone)) {
new_value = truncateDecimal(new_value, settingsClone.aDec, settingsClone.mDec);
if (position > new_value.length) {
position = new_value.length;
}
this.value = new_value;
this.setPosition(position, false);
return true;
}
return false;
},
/**
* helper function for expandSelectionOnSign
* returns sign position of a formatted value
*/
signPosition: function () {
var settingsClone = this.settingsClone,
aSign = settingsClone.aSign,
that = this.that;
if (aSign) {
var aSignLen = aSign.length;
if (settingsClone.pSign === 'p') {
var hasNeg = settingsClone.aNeg && that.value && that.value.charAt(0) === settingsClone.aNeg;
return hasNeg ? [1, aSignLen + 1] : [0, aSignLen];
}
var valueLen = that.value.length;
return [valueLen - aSignLen, valueLen];
}
return [1000, -1];
},
/**
* expands selection to cover whole sign
* prevents partial deletion/copying/overwriting of a sign
*/
expandSelectionOnSign: function (setReal) {
var sign_position = this.signPosition(),
selection = this.selection;
if (selection.start < sign_position[1] && selection.end > sign_position[0]) { /** if selection catches something except sign and catches only space from sign */
if ((selection.start < sign_position[0] || selection.end > sign_position[1]) && this.value.substring(Math.max(selection.start, sign_position[0]), Math.min(selection.end, sign_position[1])).match(/^\s*$/)) { /** then select without empty space */
if (selection.start < sign_position[0]) {
this.setSelection(selection.start, sign_position[0], setReal);
} else {
this.setSelection(sign_position[1], selection.end, setReal);
}
} else { /** else select with whole sign */
this.setSelection(Math.min(selection.start, sign_position[0]), Math.max(selection.end, sign_position[1]), setReal);
}
}
},
/**
* try to strip pasted value to digits
*/
checkPaste: function () {
if (this.valuePartsBeforePaste !== undefined) {
var parts = this.getBeforeAfter(),
oldParts = this.valuePartsBeforePaste;
delete this.valuePartsBeforePaste; /** try to strip pasted value first */
parts[0] = parts[0].substr(0, oldParts[0].length) + autoStrip(parts[0].substr(oldParts[0].length), this.settingsClone);
if (!this.setValueParts(parts[0], parts[1])) {
this.value = oldParts.join('');
this.setPosition(oldParts[0].length, false);
}
}
},
/**
* process pasting, cursor moving and skipping of not interesting keys
* if returns true, futher processing is not performed
*/
skipAllways: function (e) {
var kdCode = this.kdCode,
which = this.which,
ctrlKey = this.ctrlKey,
cmdKey = this.cmdKey,
shiftKey = this.shiftKey; /** catch the ctrl up on ctrl-v */
if (((ctrlKey || cmdKey) && e.type === 'keyup' && this.valuePartsBeforePaste !== undefined) || (shiftKey && kdCode === 45)) {
this.checkPaste();
return false;
}
/** codes are taken from http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
* skip Fx keys, windows keys, other special keys
*/
if ((kdCode >= 112 && kdCode <= 123) || (kdCode >= 91 && kdCode <= 93) || (kdCode >= 9 && kdCode <= 31) || (kdCode < 8 && (which === 0 || which === kdCode)) || kdCode === 144 || kdCode === 145 || kdCode === 45) {
return true;
}
if ((ctrlKey || cmdKey) && kdCode === 65) { /** if select all (a=65)*/
return true;
}
if ((ctrlKey || cmdKey) && (kdCode === 67 || kdCode === 86 || kdCode === 88)) { /** if copy (c=67) paste (v=86) or cut (x=88) */
if (e.type === 'keydown') {
this.expandSelectionOnSign();
}
if (kdCode === 86 || kdCode === 45) { /** try to prevent wrong paste */
if (e.type === 'keydown' || e.type === 'keypress') {
if (this.valuePartsBeforePaste === undefined) {
this.valuePartsBeforePaste = this.getBeforeAfter();
}
} else {
this.checkPaste();
}
}
return e.type === 'keydown' || e.type === 'keypress' || kdCode === 67;
}
if (ctrlKey || cmdKey) {
return true;
}
if (kdCode === 37 || kdCode === 39) { /** jump over thousand separator */
var aSep = this.settingsClone.aSep,
start = this.selection.start,
value = this.that.value;
if (e.type === 'keydown' && aSep && !this.shiftKey) {
if (kdCode === 37 && value.charAt(start - 2) === aSep) {
this.setPosition(start - 1);
} else if (kdCode === 39 && value.charAt(start + 1) === aSep) {
this.setPosition(start + 1);
}
}
return true;
}
if (kdCode >= 34 && kdCode <= 40) {
return true;
}
return false;
},
/**
* process deletion of characters
* returns true if processing performed
*/
processAllways: function () {
var parts; /** process backspace or delete */
if (this.kdCode === 8 || this.kdCode === 46) {
if (!this.selection.length) {
parts = this.getBeforeAfterStriped();
if (this.kdCode === 8) {
parts[0] = parts[0].substring(0, parts[0].length - 1);
} else {
parts[1] = parts[1].substring(1, parts[1].length);
}
this.setValueParts(parts[0], parts[1]);
} else {
this.expandSelectionOnSign(false);
parts = this.getBeforeAfterStriped();
this.setValueParts(parts[0], parts[1]);
}
return true;
}
return false;
},
/**
* process insertion of characters
* returns true if processing performed
*/
processKeypress: function () {
var settingsClone = this.settingsClone,
cCode = String.fromCharCode(this.which),
parts = this.getBeforeAfterStriped(),
left = parts[0],
right = parts[1]; /** start rules when the decimal character key is pressed */
/** always use numeric pad dot to insert decimal separator */
if (cCode === settingsClone.aDec || (settingsClone.altDec && cCode === settingsClone.altDec) || ((cCode === '.' || cCode === ',') && this.kdCode === 110)) { /** do not allow decimal character if no decimal part allowed */
if (!settingsClone.mDec || !settingsClone.aDec) {
return true;
} /** do not allow decimal character before aNeg character */
if (settingsClone.aNeg && right.indexOf(settingsClone.aNeg) > -1) {
return true;
} /** do not allow decimal character if other decimal character present */
if (left.indexOf(settingsClone.aDec) > -1) {
return true;
}
if (right.indexOf(settingsClone.aDec) > 0) {
return true;
}
if (right.indexOf(settingsClone.aDec) === 0) {
right = right.substr(1);
}
this.setValueParts(left + settingsClone.aDec, right);
return true;
} /** start rule on negative sign */
if (cCode === '-' || cCode === '+') { /** prevent minus if not allowed */
if (!settingsClone.aNeg) {
return true;
} /** caret is always after minus */
if (left === '' && right.indexOf(settingsClone.aNeg) > -1) {
left = settingsClone.aNeg;
right = right.substring(1, right.length);
} /** change sign of number, remove part if should */
if (left.charAt(0) === settingsClone.aNeg) {
left = left.substring(1, left.length);
} else {
left = (cCode === '-') ? settingsClone.aNeg + left : left;
}
this.setValueParts(left, right);
return true;
} /** digits */
if (cCode >= '0' && cCode <= '9') { /** if try to insert digit before minus */
if (settingsClone.aNeg && left === '' && right.indexOf(settingsClone.aNeg) > -1) {
left = settingsClone.aNeg;
right = right.substring(1, right.length);
}
if (settingsClone.vMax <= 0 && settingsClone.vMin < settingsClone.vMax && this.value.indexOf(settingsClone.aNeg) === -1 && cCode !== '0') {
left = settingsClone.aNeg + left;
}
this.setValueParts(left + cCode, right);
return true;
} /** prevent any other character */
return true;
},
/**
* formatting of just processed value with keeping of cursor position
*/
formatQuick: function () {
var settingsClone = this.settingsClone,
parts = this.getBeforeAfterStriped(),
leftLength = this.value;
if ((settingsClone.aSep === '' || (settingsClone.aSep !== '' && leftLength.indexOf(settingsClone.aSep) === -1)) && (settingsClone.aSign === '' || (settingsClone.aSign !== '' && leftLength.indexOf(settingsClone.aSign) === -1))) {
var subParts = [],
nSign = '';
subParts = leftLength.split(settingsClone.aDec);
if (subParts[0].indexOf('-') > -1) {
nSign = '-';
subParts[0] = subParts[0].replace('-', '');
parts[0] = parts[0].replace('-', '');
}
if (subParts[0].length > settingsClone.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */
parts[0] = parts[0].slice(1);
}
parts[0] = nSign + parts[0];
}
var value = autoGroup(this.value, this.settingsClone),
position = value.length;
if (value) {
/** prepare regexp which searches for cursor position from unformatted left part */
var left_ar = parts[0].split(''),
i = 0;
for (i; i < left_ar.length; i += 1) { /** thanks Peter Kovari */
if (!left_ar[i].match('\\d')) {
left_ar[i] = '\\' + left_ar[i];
}
}
var leftReg = new RegExp('^.*?' + left_ar.join('.*?'));
/** search cursor position in formatted value */
var newLeft = value.match(leftReg);
if (newLeft) {
position = newLeft[0].length;
/** if we are just before sign which is in prefix position */
if (((position === 0 && value.charAt(0) !== settingsClone.aNeg) || (position === 1 && value.charAt(0) === settingsClone.aNeg)) && settingsClone.aSign && settingsClone.pSign === 'p') {
/** place carret after prefix sign */
position = this.settingsClone.aSign.length + (value.charAt(0) === '-' ? 1 : 0);
}
} else if (settingsClone.aSign && settingsClone.pSign === 's') {
/** if we could not find a place for cursor and have a sign as a suffix */
/** place carret before suffix currency sign */
position -= settingsClone.aSign.length;
}
}
this.that.value = value;
this.setPosition(position);
this.formatted = true;
}
};
/** thanks to Anthony & Evan C */
function autoGet(obj) {
if (typeof obj === 'string') {
obj = obj.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
obj = '#' + obj.replace(/(:|\.)/g, '\\$1');
/** obj = '#' + obj.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); */
/** possible modification to replace the above 2 lines */
}
return $(obj);
}
function getHolder($that, settings, update) {
var data = $that.data('autoNumeric');
if (!data) {
data = {};
$that.data('autoNumeric', data);
}
var holder = data.holder;
if ((holder === undefined && settings) || update) {
holder = new AutoNumericHolder($that.get(0), settings);
data.holder = holder;
}
return holder;
}
var methods = {
init: function (options) {
return this.each(function () {
var $this = $(this),
settings = $this.data('autoNumeric'), /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
tagData = $this.data(); /** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/
if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */
var defaults = {
/** allowed numeric values
* please do not modify
*/
aNum: '0123456789',
/** allowed thousand separator characters
* comma = ','
* period "full stop" = '.'
* apostrophe is escaped = '\''
* space = ' '
* none = ''
* NOTE: do not use numeric characters
*/
aSep: ',',
/** digital grouping for the thousand separator used in Format
* dGroup: '2', results in 99,99,99,999 common in India for values less than 1 billion and greater than -1 billion
* dGroup: '3', results in 999,999,999 default
* dGroup: '4', results in 9999,9999,9999 used in some Asian countries
*/
dGroup: '3',
/** allowed decimal separator characters
* period "full stop" = '.'
* comma = ','
*/
aDec: '.',
/** allow to declare alternative decimal separator which is automatically replaced by aDec
* developed for countries the use a comma ',' as the decimal character
* and have keyboards\numeric pads that have a period 'full stop' as the decimal characters (Spain is an example)
*/
altDec: null,
/** allowed currency symbol
* Must be in quotes aSign: '$', a space is allowed aSign: '$ '
*/
aSign: '',
/** placement of currency sign
* for prefix pSign: 'p',
* for suffix pSign: 's',
*/
pSign: 'p',
/** maximum possible value
* value must be enclosed in quotes and use the period for the decimal point
* value must be larger than vMin
*/
vMax: '999999999.99',
/** minimum possible value
* value must be enclosed in quotes and use the period for the decimal point
* value must be smaller than vMax
*/
vMin: '0.00',
/** max number of decimal places = used to override decimal places set by the vMin & vMax values
* value must be enclosed in quotes example mDec: '3',
* This can also set the value via a call back function mDec: 'css:#
*/
mDec: null,
/** method used for rounding
* mRound: 'S', Round-Half-Up Symmetric (default)
* mRound: 'A', Round-Half-Up Asymmetric
* mRound: 's', Round-Half-Down Symmetric (lower case s)
* mRound: 'a', Round-Half-Down Asymmetric (lower case a)
* mRound: 'B', Round-Half-Even "Bankers Rounding"
* mRound: 'U', Round Up "Round-Away-From-Zero"
* mRound: 'D', Round Down "Round-Toward-Zero" - same as truncate
* mRound: 'C', Round to Ceiling "Toward Positive Infinity"
* mRound: 'F', Round to Floor "Toward Negative Infinity"
*/
mRound: 'S',
/** controls decimal padding
* aPad: true - always Pad decimals with zeros
* aPad: false - does not pad with zeros.
* aPad: `some number` - pad decimals with zero to number different from mDec
* thanks to Jonas Johansson for the suggestion
*/
aPad: true,
/** places brackets on negative value -$ 999.99 to (999.99)
* visible only when the field does NOT have focus the left and right symbols should be enclosed in quotes and seperated by a comma
* nBracket: null, nBracket: '(,)', nBracket: '[,]', nBracket: '<,>' or nBracket: '{,}'
*/
nBracket: null,
/** Displayed on empty string
* wEmpty: 'empty', - input can be blank
* wEmpty: 'zero', - displays zero
* wEmpty: 'sign', - displays the currency sign
*/
wEmpty: 'empty',
/** controls leading zero behavior
* lZero: 'allow', - allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted.
* lZero: 'deny', - allows only one leading zero on values less than one
* lZero: 'keep', - allows leading zeros to be entered. on fousout zeros will be retained.
*/
lZero: 'allow',
/** determine if the default value will be formatted on page ready.
* true = automatically formats the default value on page ready
* false = will not format the default value
*/
aForm: true,
/** future use */
onSomeEvent: function () {}
};
settings = $.extend({}, defaults, tagData, options); /** Merge defaults, tagData and options */
if (settings.aDec === settings.aSep) {
$.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
return this;
}
$this.data('autoNumeric', settings); /** Save our new settings */
} else {
return this;
}
settings.lastSetValue = '';
settings.runOnce = false;
var holder = getHolder($this, settings);
if ($.inArray($this.prop('tagName'), settings.tagList) === -1 && $this.prop('tagName') !== 'INPUT') {
$.error("The <" + $this.prop('tagName') + "> is not supported by autoNumeric()");
return this;
}
if (settings.runOnce === false && settings.aForm) {/** routine to format default value on page load */
if ($this.is('input[type=text], input[type=hidden], input:not([type])')) {
var setValue = true;
if ($this[0].value === '' && settings.wEmpty === 'empty') {
$this[0].value = '';
setValue = false;
}
if ($this[0].value === '' && settings.wEmpty === 'sign') {
$this[0].value = settings.aSign;
setValue = false;
}
if (setValue) {
$this.autoNumeric('set', $this.val());
}
}
if ($.inArray($this.prop('tagName'), settings.tagList) !== -1 && $this.text() !== '') {
$this.autoNumeric('set', $this.text());
}
}
settings.runOnce = true;
if ($this.is('input[type=text], input[type=hidden], input:not([type])')) { /**added hidden type */
$this.on('keydown.autoNumeric', function (e) {
holder = getHolder($this);
if (holder.settings.aDec === holder.settings.aSep) {
$.error("autoNumeric will not function properly when the decimal character aDec: '" + holder.settings.aDec + "' and thousand separator aSep: '" + holder.settings.aSep + "' are the same character");
return this;
}
if (holder.that.readOnly) {
holder.processed = true;
return true;
}
/** The below streamed code / comment allows the "enter" keydown to throw a change() event */
/** if (e.keyCode === 13 && holder.inVal !== $this.val()){
$this.change();
holder.inVal = $this.val();
}*/
holder.init(e);
holder.settings.oEvent = 'keydown';
if (holder.skipAllways(e)) {
holder.processed = true;
return true;
}
if (holder.processAllways()) {
holder.processed = true;
holder.formatQuick();
e.preventDefault();
return false;
}
holder.formatted = false;
return true;
});
$this.on('keypress.autoNumeric', function (e) {
var holder = getHolder($this),
processed = holder.processed;
holder.init(e);
holder.settings.oEvent = 'keypress';
if (holder.skipAllways(e)) {
return true;
}
if (processed) {
e.preventDefault();
return false;
}
if (holder.processAllways() || holder.processKeypress()) {
holder.formatQuick();
e.preventDefault();
return false;
}
holder.formatted = false;
});
$this.on('keyup.autoNumeric', function (e) {
var holder = getHolder($this);
holder.init(e);
holder.settings.oEvent = 'keyup';
var skip = holder.skipAllways(e);
holder.kdCode = 0;
delete holder.valuePartsBeforePaste;
if ($this[0].value === holder.settings.aSign) { /** added to properly place the caret when only the currency is present */
if (holder.settings.pSign === 's') {
setElementSelection(this, 0, 0);
} else {
setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
}
}
if (skip) {
return true;
}
if (this.value === '') {
return true;
}
if (!holder.formatted) {
holder.formatQuick();
}
});
$this.on('focusin.autoNumeric', function () {
var holder = getHolder($this);
holder.settingsClone.oEvent = 'focusin';
if (holder.settingsClone.nBracket !== null) {
var checkVal = $this.val();
$this.val(negativeBracket(checkVal, holder.settingsClone.nBracket, holder.settingsClone.oEvent));
}
holder.inVal = $this.val();
var onempty = checkEmpty(holder.inVal, holder.settingsClone, true);
if (onempty !== null) {
$this.val(onempty);
if (holder.settings.pSign === 's') {
setElementSelection(this, 0, 0);
} else {
setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
}
}
});
$this.on('focusout.autoNumeric', function () {
var holder = getHolder($this),
settingsClone = holder.settingsClone,
value = $this.val(),
origValue = value;
holder.settingsClone.oEvent = 'focusout';
var strip_zero = ''; /** added to control leading zero */
if (settingsClone.lZero === 'allow') { /** added to control leading zero */
settingsClone.allowLeading = false;
strip_zero = 'leading';
}
if (value !== '') {
value = autoStrip(value, settingsClone, strip_zero);
if (checkEmpty(value, settingsClone) === null && autoCheck(value, settingsClone, $this[0])) {
value = fixNumber(value, settingsClone.aDec, settingsClone.aNeg);
value = autoRound(value, settingsClone);
value = presentNumber(value, settingsClone.aDec, settingsClone.aNeg);
} else {
value = '';
}
}
var groupedValue = checkEmpty(value, settingsClone, false);
if (groupedValue === null) {
groupedValue = autoGroup(value, settingsClone);
}
if (groupedValue !== origValue) {
$this.val(groupedValue);
}
if (groupedValue !== holder.inVal) {
$this.change();
delete holder.inVal;
}
if (settingsClone.nBracket !== null && $this.autoNumeric('get') < 0) {
holder.settingsClone.oEvent = 'focusout';
$this.val(negativeBracket($this.val(), settingsClone.nBracket, settingsClone.oEvent));
}
});
}
});
},
/** method to remove settings and stop autoNumeric() */
destroy: function () {
return $(this).each(function () {
var $this = $(this);
$this.off('.autoNumeric');
$this.removeData('autoNumeric');
});
},
/** method to update settings - can call as many times */
update: function (options) {
return $(this).each(function () {
var $this = autoGet($(this)),
settings = $this.data('autoNumeric');
if (typeof settings !== 'object') {
$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'update' method");
return this;
}
var strip = $this.autoNumeric('get');
settings = $.extend(settings, options);
getHolder($this, settings, true);
if (settings.aDec === settings.aSep) {
$.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
return this;
}
$this.data('autoNumeric', settings);
if ($this.val() !== '' || $this.text() !== '') {
return $this.autoNumeric('set', strip);
}
return;
});
},
/** returns a formatted strings for "input:text" fields Uses jQuery's .val() method*/
set: function (valueIn) {
return $(this).each(function () {
var $this = autoGet($(this)),
settings = $this.data('autoNumeric'),
value = valueIn.toString(),
testValue = valueIn.toString();
if (typeof settings !== 'object') {
$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'set' method");
return this;
}
/** allows locale decimal separator to be a comma */
if ((testValue === $this.attr('value') || testValue === $this.text()) && settings.runOnce === false) {
value = value.replace(',', '.');
}
/** routine to handle page re-load from back button */
if (testValue !== $this.attr('value') && $this.prop('tagName') === 'INPUT' && settings.runOnce === false) {
value = autoStrip(value, settings);
}
/** returns a empty string if the value being 'set' contains non-numeric characters and or more than decimal point (full stop) and will not be formatted */
if (!$.isNumeric(+value)) {
return '';
}
value = checkValue(value, settings);
settings.oEvent = 'set';
settings.lastSetValue = value; /** saves the unrounded value from the set method - $('selector').data('autoNumeric').lastSetValue; - helpful when you need to change the rounding accuracy*/
value.toString();
if (value !== '') {
value = autoRound(value, settings);
}
value = presentNumber(value, settings.aDec, settings.aNeg);
if (!autoCheck(value, settings)) {
value = autoRound('', settings);
}
value = autoGroup(value, settings);
if ($this.is('input[type=text], input[type=hidden], input:not([type])')) { /**added hidden type */
return $this.val(value);
}
if ($.inArray($this.prop('tagName'), settings.tagList) !== -1) {
return $this.text(value);
}
$.error("The <" + $this.prop('tagName') + "> is not supported by autoNumeric()");
return false;
});
},
/** method to get the unformatted value from a specific input field, returns a numeric value */
get: function () {
var $this = autoGet($(this)),
settings = $this.data('autoNumeric');
if (typeof settings !== 'object') {
$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'get' method");
return this;
}
settings.oEvent = 'get';
var getValue = '';
/** determine the element type then use .eq(0) selector to grab the value of the first element in selector */
if ($this.is('input[type=text], input[type=hidden], input:not([type])')) { /**added hidden type */
getValue = $this.eq(0).val();
} else if ($.inArray($this.prop('tagName'), settings.tagList) !== -1) {
getValue = $this.eq(0).text();
} else {
$.error("The <" + $this.prop('tagName') + "> is not supported by autoNumeric()");
return false;
}
if ((getValue === '' && settings.wEmpty === 'empty') || (getValue === settings.aSign && (settings.wEmpty === 'sign' || settings.wEmpty === 'empty'))) {
return '';
}
if (settings.nBracket !== null && getValue !== '') {
getValue = negativeBracket(getValue, settings.nBracket, settings.oEvent);
}
if (settings.runOnce || settings.aForm === false) {
getValue = autoStrip(getValue, settings);
}
getValue = fixNumber(getValue, settings.aDec, settings.aNeg);
if (+getValue === 0 && settings.lZero !== 'keep') {
getValue = '0';
}
if (settings.lZero === 'keep') {
return getValue;
}
getValue = checkValue(getValue, settings);
return getValue; /** returned Numeric String */
},
/** method to get the unformatted value from multiple fields */
getString: function () {
var isAutoNumeric = false,
$this = autoGet($(this)),
str = $this.serialize(),
parts = str.split('&'),
i = 0;
for (i; i < parts.length; i += 1) {
var miniParts = parts[i].split('=');
var settings = $('*[name="' + decodeURIComponent(miniParts[0]) + '"]').data('autoNumeric');
if (typeof settings === 'object') {
if (miniParts[1] !== null && $('*[name="' + decodeURIComponent(miniParts[0]) + '"]').data('autoNumeric') !== undefined) {
miniParts[1] = $('input[name="' + decodeURIComponent(miniParts[0]) + '"]').autoNumeric('get');
parts[i] = miniParts.join('=');
isAutoNumeric = true;
}
}
}
if (isAutoNumeric === true) {
return parts.join('&');
}
$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'getString' method");
return this;
},
/** method to get the unformatted value from multiple fields */
getArray: function () {
var isAutoNumeric = false,
$this = autoGet($(this)),
formFields = $this.serializeArray();
$.each(formFields, function (i, field) {
var settings = $('*[name="' + decodeURIComponent(field.name) + '"]').data('autoNumeric');
if (typeof settings === 'object') {
if (field.value !== '' && $('*[name="' + decodeURIComponent(field.name) + '"]').data('autoNumeric') !== undefined) {
field.value = $('input[name="' + decodeURIComponent(field.name) + '"]').autoNumeric('get').toString();
}
isAutoNumeric = true;
}
});
if (isAutoNumeric === true) {
return formFields;
}
$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'getArray' method");
return this;
},
/** returns the settings object for those who need to look under the hood */
getSettings: function () {
var $this = autoGet($(this));
return $this.eq(0).data('autoNumeric');
}
};
$.fn.autoNumeric = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
$.error('Method "' + method + '" is not supported by autoNumeric()');
};
}(jQuery));
/**
* @license Input Mask plugin for jquery
* http://github.com/RobinHerbots/jquery.inputmask
* Copyright (c) 2010 - 2013 Robin Herbots
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
* Version: 2.3.0
*/
(function ($) {
if ($.fn.inputmask == undefined) {
$.inputmask = {
//options default
defaults: {
placeholder: "_",
optionalmarker: {
start: "[",
end: "]"
},
escapeChar: "\\",
mask: null,
oncomplete: $.noop, //executes when the mask is complete
onincomplete: $.noop, //executes when the mask is incomplete and focus is lost
oncleared: $.noop, //executes when the mask is cleared
repeat: 0, //repetitions of the mask: * ~ forever, otherwise specify an integer
greedy: true, //true: allocated buffer for the mask and repetitions - false: allocate only if needed
autoUnmask: false, //automatically unmask when retrieving the value with $.fn.val or value if the browser supports __lookupGetter__ or getOwnPropertyDescriptor
clearMaskOnLostFocus: true,
insertMode: true, //insert the input or overwrite the input
clearIncomplete: false, //clear the incomplete input on blur
aliases: {}, //aliases definitions => see jquery.inputmask.extensions.js
onKeyUp: $.noop, //override to implement autocomplete on certain keys for example
onKeyDown: $.noop, //override to implement autocomplete on certain keys for example
showMaskOnFocus: true, //show the mask-placeholder when the input has focus
showMaskOnHover: true, //show the mask-placeholder when hovering the empty input
onKeyValidation: $.noop, //executes on every key-press with the result of isValid. Params: result, opts
skipOptionalPartCharacter: " ", //a character which can be used to skip an optional part of a mask
showTooltip: false, //show the activemask as tooltip
//numeric basic properties
numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
radixPoint: "", //".", // | ","
skipRadixDance: false, //disable radixpoint caret positioning
rightAlignNumerics: true, //align numerics to the right
//numeric basic properties
definitions: {
'9': {
validator: "[0-9]",
cardinality: 1
},
'a': {
validator: "[A-Za-z\u0410-\u044F\u0401\u0451]",
cardinality: 1
},
'*': {
validator: "[A-Za-z\u0410-\u044F\u0401\u04510-9]",
cardinality: 1
}
},
keyCode: {
ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108,
NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38, WINDOWS: 91
},
//specify keycodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
ignorables: [9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123],
getMaskLength: function (buffer, greedy, repeat, currentBuffer, opts) {
var calculatedLength = buffer.length;
if (!greedy) {
if (repeat == "*") {
calculatedLength = currentBuffer.length + 1;
} else if (repeat > 1) {
calculatedLength += (buffer.length * (repeat - 1));
}
}
return calculatedLength;
}
},
val: $.fn.val, //store the original jquery val function
escapeRegex: function (str) {
var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'];
return str.replace(new RegExp('(\\' + specials.join('|\\') + ')', 'gim'), '\\$1');
}
};
$.fn.inputmask = function (fn, options) {
var opts = $.extend(true, {}, $.inputmask.defaults, options),
msie10 = navigator.userAgent.match(new RegExp("msie 10", "i")) !== null,
iphone = navigator.userAgent.match(new RegExp("iphone", "i")) !== null,
android = navigator.userAgent.match(new RegExp("android.*safari.*", "i")) !== null,
pasteEvent = isInputEventSupported('paste') && !msie10 ? 'paste' : 'input',
android53x,
masksets,
activeMasksetIndex = 0;
if (android) {
var browser = navigator.userAgent.match(/safari.*/i),
version = parseInt(new RegExp(/[0-9]+/).exec(browser));
android53x = (version <= 537);
//android534 = (533 < version) && (version <= 534);
}
if (typeof fn === "string") {
switch (fn) {
case "mask":
//resolve possible aliases given by options
resolveAlias(opts.alias, options);
masksets = generateMaskSets();
return this.each(function () {
maskScope($.extend(true, {}, masksets), 0).mask(this);
});
case "unmaskedvalue":
var $input = $(this), input = this;
if ($input.data('_inputmask')) {
masksets = $input.data('_inputmask')['masksets'];
activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
opts = $input.data('_inputmask')['opts'];
return maskScope(masksets, activeMasksetIndex).unmaskedvalue($input);
} else return $input.val();
case "remove":
return this.each(function () {
var $input = $(this), input = this;
setTimeout(function () {
if ($input.data('_inputmask')) {
masksets = $input.data('_inputmask')['masksets'];
activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
opts = $input.data('_inputmask')['opts'];
//writeout the unmaskedvalue
input._valueSet(maskScope(masksets, activeMasksetIndex).unmaskedvalue($input, true));
//clear data
$input.removeData('_inputmask');
//unbind all events
$input.unbind(".inputmask");
$input.removeClass('focus.inputmask');
//restore the value property
var valueProperty;
if (Object.getOwnPropertyDescriptor)
valueProperty = Object.getOwnPropertyDescriptor(input, "value");
if (valueProperty && valueProperty.get) {
if (input._valueGet) {
Object.defineProperty(input, "value", {
get: input._valueGet,
set: input._valueSet
});
}
} else if (document.__lookupGetter__ && input.__lookupGetter__("value")) {
if (input._valueGet) {
input.__defineGetter__("value", input._valueGet);
input.__defineSetter__("value", input._valueSet);
}
}
delete input._valueGet;
delete input._valueSet;
}
}, 0);
});
break;
case "getemptymask": //return the default (empty) mask value, usefull for setting the default value in validation
if (this.data('_inputmask')) {
masksets = this.data('_inputmask')['masksets'];
activeMasksetIndex = this.data('_inputmask')['activeMasksetIndex'];
return masksets[activeMasksetIndex]['_buffer'].join('');
}
else return "";
case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
return this.data('_inputmask') ? !this.data('_inputmask')['opts'].autoUnmask : false;
case "isComplete":
masksets = this.data('_inputmask')['masksets'];
activeMasksetIndex = this.data('_inputmask')['activeMasksetIndex'];
opts = this.data('_inputmask')['opts'];
return maskScope(masksets, activeMasksetIndex).isComplete(this[0]._valueGet().split(''));
default:
//check if the fn is an alias
if (!resolveAlias(fn, options)) {
//maybe fn is a mask so we try
//set mask
opts.mask = fn;
}
masksets = generateMaskSets();
return this.each(function () {
maskScope($.extend(true, {}, masksets), activeMasksetIndex).mask(this);
});
break;
}
} else if (typeof fn == "object") {
opts = $.extend(true, {}, $.inputmask.defaults, fn);
resolveAlias(opts.alias, fn); //resolve aliases
masksets = generateMaskSets();
return this.each(function () {
maskScope($.extend(true, {}, masksets), activeMasksetIndex).mask(this);
});
} else if (fn == undefined) {
//look for data-inputmask atribute - the attribute should only contain optipns
return this.each(function () {
var attrOptions = $(this).attr("data-inputmask");
if (attrOptions && attrOptions != "") {
try {
attrOptions = attrOptions.replace(new RegExp("'", "g"), '"');
var dataoptions = $.parseJSON("{" + attrOptions + "}");
$.extend(true, dataoptions, options);
opts = $.extend(true, {}, $.inputmask.defaults, dataoptions);
resolveAlias(opts.alias, dataoptions);
opts.alias = undefined;
$(this).inputmask(opts);
} catch (ex) { } //need a more relax parseJSON
}
});
}
//helper functions
function isInputEventSupported(eventName) {
var el = document.createElement('input'),
eventName = 'on' + eventName,
isSupported = (eventName in el);
if (!isSupported) {
el.setAttribute(eventName, 'return;');
isSupported = typeof el[eventName] == 'function';
}
el = null;
return isSupported;
}
function resolveAlias(aliasStr, options) {
var aliasDefinition = opts.aliases[aliasStr];
if (aliasDefinition) {
if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias); //alias is another alias
$.extend(true, opts, aliasDefinition); //merge alias definition in the options
$.extend(true, opts, options); //reapply extra given options
return true;
}
return false;
}
function getMaskTemplate(mask) {
var escaped = false, outCount = 0, greedy = opts.greedy, repeat = opts.repeat;
if (repeat == "*") greedy = false;
if (mask.length == 1 && greedy == false) { opts.placeholder = ""; } //hide placeholder with single non-greedy mask
var singleMask = $.map(mask.split(""), function (element, index) {
var outElem = [];
if (element == opts.escapeChar) {
escaped = true;
}
else if ((element != opts.optionalmarker.start && element != opts.optionalmarker.end) || escaped) {
var maskdef = opts.definitions[element];
if (maskdef && !escaped) {
for (var i = 0; i < maskdef.cardinality; i++) {
outElem.push(getPlaceHolder(outCount + i));
}
} else {
outElem.push(element);
escaped = false;
}
outCount += outElem.length;
return outElem;
}
});
//allocate repetitions
var repeatedMask = singleMask.slice();
for (var i = 1; i < repeat && greedy; i++) {
repeatedMask = repeatedMask.concat(singleMask.slice());
}
return { "mask": repeatedMask, "repeat": repeat, "greedy": greedy };
}
//test definition => {fn: RegExp/function, cardinality: int, optionality: bool, newBlockMarker: bool, offset: int, casing: null/upper/lower, def: definitionSymbol}
function getTestingChain(mask) {
var isOptional = false, escaped = false;
var newBlockMarker = false; //indicates wheter the begin/ending of a block should be indicated
return $.map(mask.split(""), function (element, index) {
var outElem = [];
if (element == opts.escapeChar) {
escaped = true;
} else if (element == opts.optionalmarker.start && !escaped) {
isOptional = true;
newBlockMarker = true;
}
else if (element == opts.optionalmarker.end && !escaped) {
isOptional = false;
newBlockMarker = true;
}
else {
var maskdef = opts.definitions[element];
if (maskdef && !escaped) {
var prevalidators = maskdef["prevalidator"], prevalidatorsL = prevalidators ? prevalidators.length : 0;
for (var i = 1; i < maskdef.cardinality; i++) {
var prevalidator = prevalidatorsL >= i ? prevalidators[i - 1] : [], validator = prevalidator["validator"], cardinality = prevalidator["cardinality"];
outElem.push({ fn: validator ? typeof validator == 'string' ? new RegExp(validator) : new function () { this.test = validator; } : new RegExp("."), cardinality: cardinality ? cardinality : 1, optionality: isOptional, newBlockMarker: isOptional == true ? newBlockMarker : false, offset: 0, casing: maskdef["casing"], def: maskdef["definitionSymbol"] | element });
if (isOptional == true) //reset newBlockMarker
newBlockMarker = false;
}
outElem.push({ fn: maskdef.validator ? typeof maskdef.validator == 'string' ? new RegExp(maskdef.validator) : new function () { this.test = maskdef.validator; } : new RegExp("."), cardinality: maskdef.cardinality, optionality: isOptional, newBlockMarker: newBlockMarker, offset: 0, casing: maskdef["casing"], def: maskdef["definitionSymbol"] | element });
} else {
outElem.push({ fn: null, cardinality: 0, optionality: isOptional, newBlockMarker: newBlockMarker, offset: 0, casing: null, def: element });
escaped = false;
}
//reset newBlockMarker
newBlockMarker = false;
return outElem;
}
});
}
function generateMaskSets() {
var ms = [];
var genmasks = []; //used to keep track of the masks that where processed, to avoid duplicates
function markOptional(maskPart) { //needed for the clearOptionalTail functionality
return opts.optionalmarker.start + maskPart + opts.optionalmarker.end;
}
function splitFirstOptionalEndPart(maskPart) {
var optionalStartMarkers = 0, optionalEndMarkers = 0, mpl = maskPart.length;
for (i = 0; i < mpl; i++) {
if (maskPart.charAt(i) == opts.optionalmarker.start) {
optionalStartMarkers++;
}
if (maskPart.charAt(i) == opts.optionalmarker.end) {
optionalEndMarkers++;
}
if (optionalStartMarkers > 0 && optionalStartMarkers == optionalEndMarkers)
break;
}
var maskParts = [maskPart.substring(0, i)];
if (i < mpl) {
maskParts.push(maskPart.substring(i + 1, mpl));
}
return maskParts;
}
function splitFirstOptionalStartPart(maskPart) {
var mpl = maskPart.length;
for (i = 0; i < mpl; i++) {
if (maskPart.charAt(i) == opts.optionalmarker.start) {
break;
}
}
var maskParts = [maskPart.substring(0, i)];
if (i < mpl) {
maskParts.push(maskPart.substring(i + 1, mpl));
}
return maskParts;
}
function generateMask(maskPrefix, maskPart) {
var maskParts = splitFirstOptionalEndPart(maskPart);
var newMask, maskTemplate;
var masks = splitFirstOptionalStartPart(maskParts[0]);
if (masks.length > 1) {
newMask = maskPrefix + masks[0] + markOptional(masks[1]) + (maskParts.length > 1 ? maskParts[1] : "");
if ($.inArray(newMask, genmasks) == -1) {
genmasks.push(newMask);
maskTemplate = getMaskTemplate(newMask);
ms.push({
"mask": newMask,
"_buffer": maskTemplate["mask"],
"buffer": maskTemplate["mask"].slice(),
"tests": getTestingChain(newMask),
"lastValidPosition": undefined,
"greedy": maskTemplate["greedy"],
"repeat": maskTemplate["repeat"]
});
}
newMask = maskPrefix + masks[0] + (maskParts.length > 1 ? maskParts[1] : "");
if ($.inArray(newMask, genmasks) == -1) {
genmasks.push(newMask);
maskTemplate = getMaskTemplate(newMask);
ms.push({
"mask": newMask,
"_buffer": maskTemplate["mask"],
"buffer": maskTemplate["mask"].slice(),
"tests": getTestingChain(newMask),
"lastValidPosition": undefined,
"greedy": maskTemplate["greedy"],
"repeat": maskTemplate["repeat"]
});
}
if (splitFirstOptionalStartPart(masks[1]).length > 1) { //optional contains another optional
generateMask(maskPrefix + masks[0], masks[1] + maskParts[1]);
}
if (maskParts.length > 1 && splitFirstOptionalStartPart(maskParts[1]).length > 1) {
generateMask(maskPrefix + masks[0] + markOptional(masks[1]), maskParts[1]);
generateMask(maskPrefix + masks[0], maskParts[1]);
}
}
else {
newMask = maskPrefix + maskParts;
if ($.inArray(newMask, genmasks) == -1) {
genmasks.push(newMask);
maskTemplate = getMaskTemplate(newMask);
ms.push({
"mask": newMask,
"_buffer": maskTemplate["mask"],
"buffer": maskTemplate["mask"].slice(),
"tests": getTestingChain(newMask),
"lastValidPosition": undefined,
"greedy": maskTemplate["greedy"],
"repeat": maskTemplate["repeat"]
});
}
}
}
if ($.isArray(opts.mask)) {
$.each(opts.mask, function (ndx, lmnt) {
generateMask("", lmnt.toString());
});
} else generateMask("", opts.mask.toString());
return ms;
}
function getPlaceHolder(pos) {
return opts.placeholder.charAt(pos % opts.placeholder.length);
}
function maskScope(masksets, activeMasksetIndex) {
//maskset helperfunctions
function getActiveMaskSet() {
return masksets[activeMasksetIndex];
}
function getActiveTests() {
return getActiveMaskSet()['tests'];
}
function getActiveBufferTemplate() {
return getActiveMaskSet()['_buffer'];
}
function getActiveBuffer() {
return getActiveMaskSet()['buffer'];
}
function isValid(pos, c, strict, isRTL) { //strict true ~ no correction or autofill
strict = strict === true; //always set a value to strict to prevent possible strange behavior in the extensions
function _isValid(position, activeMaskset) {
var testPos = determineTestPosition(position), loopend = c ? 1 : 0, chrs = '', buffer = activeMaskset["buffer"];
for (var i = activeMaskset['tests'][testPos].cardinality; i > loopend; i--) {
chrs += getBufferElement(buffer, testPos - (i - 1));
}
if (c) {
chrs += c;
}
//return is false or a json object => { pos: ??, c: ??} or true
return activeMaskset['tests'][testPos].fn != null ? activeMaskset['tests'][testPos].fn.test(chrs, buffer, position, strict, opts) : false;
}
if (strict) {
var result = _isValid(pos, getActiveMaskSet()); //only check validity in current mask when validating strict
if (result === true) {
result = { "pos": pos }; //always take a possible corrected maskposition into account
}
return result;
}
var results = [], result = false, currentActiveMasksetIndex = activeMasksetIndex;
$.each(masksets, function (index, value) {
var activeMaskset = this;
activeMasksetIndex = index;
var maskPos = pos;
if (currentActiveMasksetIndex != activeMasksetIndex && !isMask(pos)) {
if (c == activeMaskset['_buffer'][maskPos] || c == opts.skipOptionalPartCharacter) { //match non-mask item
results.push({ "activeMasksetIndex": index, "result": { "refresh": true, c: activeMaskset['_buffer'][maskPos] } }); //new command hack only rewrite buffer
activeMaskset['lastValidPosition'] = maskPos;
return false;
} else activeMaskset['lastValidPosition'] = isRTL ? getMaskLength() + 1 : -1; //mark mask as validated and invalid
//maskPos = isRTL ? seekPrevious(pos) : seekNext(pos);
}
if ((activeMaskset['lastValidPosition'] == undefined
&& maskPos == (isRTL ? seekPrevious(getMaskLength()) : seekNext(-1))
)
|| (isRTL || opts.numericInput)
? activeMaskset['lastValidPosition'] <= opts.numericInput ? getMaskLength() : seekNext(maskPos)
: activeMaskset['lastValidPosition'] >= seekPrevious(maskPos)) {
if (maskPos >= 0 && maskPos < getMaskLength()) {
result = _isValid(maskPos, activeMaskset);
if (result !== false) {
if (result === true) {
result = { "pos": maskPos }; //always take a possible corrected maskposition into account
}
var newValidPosition = result.pos || maskPos;
if (activeMaskset['lastValidPosition'] == undefined ||
(isRTL ? (opts.greedy ? activeMaskset['lastValidPosition'] > newValidPosition : newValidPosition == getActiveBuffer().length - 1)
: activeMaskset['lastValidPosition'] < newValidPosition))
activeMaskset['lastValidPosition'] = newValidPosition; //set new position from isValid
}
results.push({ "activeMasksetIndex": index, "result": result });
}
}
});
activeMasksetIndex = currentActiveMasksetIndex; //reset activeMasksetIndex
return results; //return results of the multiple mask validations
}
function determineActiveMasksetIndex(isRTL) {
var currentMasksetIndex = activeMasksetIndex,
highestValid = { "activeMasksetIndex": 0, "lastValidPosition": isRTL ? getMaskLength() + 1 : -1 };
$.each(masksets, function (index, value) {
var activeMaskset = this;
if (activeMaskset['lastValidPosition'] != undefined) {
if ((isRTL || opts.numericInput) ? (activeMaskset['lastValidPosition'] < highestValid['lastValidPosition']) : (activeMaskset['lastValidPosition'] > highestValid['lastValidPosition'])) {
highestValid["activeMasksetIndex"] = index;
highestValid["lastValidPosition"] = activeMaskset['lastValidPosition'];
}
}
});
activeMasksetIndex = highestValid["activeMasksetIndex"];
if (currentMasksetIndex != activeMasksetIndex) {
if (isRTL) {
clearBuffer(getActiveBuffer(), 0, seekPrevious(highestValid["lastValidPosition"]));
} else {
clearBuffer(getActiveBuffer(), seekNext(highestValid["lastValidPosition"]), getMaskLength());
}
getActiveMaskSet()["writeOutBuffer"] = true;
}
}
function isMask(pos) {
var testPos = determineTestPosition(pos);
var test = getActiveTests()[testPos];
return test != undefined ? test.fn : false;
}
function determineTestPosition(pos) {
return pos % getActiveTests().length;
}
function getMaskLength() {
return opts.getMaskLength(getActiveBufferTemplate(), getActiveMaskSet()['greedy'], getActiveMaskSet()['repeat'], getActiveBuffer(), opts);
}
//pos: from position
function seekNext(pos) {
var maskL = getMaskLength();
if (pos >= maskL) return maskL;
var position = pos;
while (++position < maskL && !isMask(position)) {
}
;
return position;
}
//pos: from position
function seekPrevious(pos) {
var position = pos;
if (position <= 0) return 0;
while (--position > 0 && !isMask(position)) {
}
;
return position;
}
function setBufferElement(buffer, position, element, autoPrepare, isRTL) {
if (autoPrepare) position = prepareBuffer(buffer, position, isRTL);
var test = getActiveTests()[determineTestPosition(position)];
var elem = element;
if (elem != undefined) {
switch (test.casing) {
case "upper":
elem = element.toUpperCase();
break;
case "lower":
elem = element.toLowerCase();
break;
}
}
buffer[position] = elem;
}
function getBufferElement(buffer, position, autoPrepare) {
if (autoPrepare) position = prepareBuffer(buffer, position);
return buffer[position];
}
//needed to handle the non-greedy mask repetitions
function prepareBuffer(buffer, position, isRTL) {
var j;
if (isRTL) {
while (position < 0 && buffer.length < getMaskLength()) {
j = getActiveBufferTemplate().length - 1;
position = getActiveBufferTemplate().length;
while (getActiveBufferTemplate()[j] !== undefined) {
buffer.unshift(getActiveBufferTemplate()[j--]);
}
}
} else {
while (buffer[position] == undefined && buffer.length < getMaskLength()) {
j = 0;
while (getActiveBufferTemplate()[j] !== undefined) { //add a new buffer
buffer.push(getActiveBufferTemplate()[j++]);
}
}
}
return position;
}
function writeBuffer(input, buffer, caretPos) {
input._valueSet(buffer.join(''));
if (caretPos != undefined) {
caret(input, caretPos);
}
}
;
function clearBuffer(buffer, start, end) {
for (var i = start, maskL = getMaskLength() ; i < end && i < maskL; i++) {
setBufferElement(buffer, i, getBufferElement(getActiveBufferTemplate().slice(), i, true));
}
}
;
function setReTargetPlaceHolder(buffer, pos) {
var testPos = determineTestPosition(pos);
setBufferElement(buffer, pos, getBufferElement(getActiveBufferTemplate(), testPos));
}
function checkVal(input, writeOut, strict, nptvl) {
var isRTL = $(input).data('_inputmask')['isRTL'],
inputValue = nptvl != undefined ? nptvl.slice() : truncateInput(input._valueGet(), isRTL).split('');
$.each(masksets, function (ndx, ms) {
ms["buffer"] = ms["_buffer"].slice();
ms["lastValidPosition"] = undefined;
ms["p"] = isRTL ? getMaskLength() : 0;
});
if (strict !== true) activeMasksetIndex = 0;
if (writeOut) input._valueSet(""); //initial clear
if (isRTL && !opts.numericInput)
inputValue = inputValue.reverse();
var ml = getMaskLength();
$.each(inputValue, function (ndx, charCode) {
var index = isRTL ? (opts.numericInput ? ml : ml - ndx) : ndx,
lvp = getActiveMaskSet()["lastValidPosition"],
pos = getActiveMaskSet()["p"];
pos = lvp == undefined ? index : pos;
lvp = lvp == undefined ? -1 : lvp;
if ((strict && isMask(isRTL ? index - 1 : index)) ||
((charCode != getBufferElement(getActiveBufferTemplate().slice(), isRTL ? index - 1 : index, true) || isMask(isRTL ? index - 1 : index)) &&
$.inArray(charCode, getActiveBufferTemplate().slice(lvp + 1, pos)) == -1)
) {
$(input).trigger("keypress", [true, charCode.charCodeAt(0), writeOut, strict, index, isRTL]);
}
});
if (strict === true) {
getActiveMaskSet()["lastValidPosition"] = isRTL ? seekNext(getActiveMaskSet()["p"]) : seekPrevious(getActiveMaskSet()["p"]);
}
}
function escapeRegex(str) {
return $.inputmask.escapeRegex.call(this, str);
}
function truncateInput(inputValue, rtl) {
return rtl ? inputValue.replace(new RegExp("^(" + escapeRegex(getActiveBufferTemplate().join('')) + ")*"), "") : inputValue.replace(new RegExp("(" + escapeRegex(getActiveBufferTemplate().join('')) + ")*$"), "");
}
function clearOptionalTail(input) {
var buffer = getActiveBuffer(), tmpBuffer = buffer.slice(), testPos, pos;
if ($(input).data('_inputmask')['isRTL']) {
for (var pos = 0; pos <= tmpBuffer.length - 1; pos++) {
var testPos = determineTestPosition(pos);
if (getActiveTests()[testPos].optionality) {
if (!isMask(pos) || !isValid(pos, buffer[pos], true))
tmpBuffer.splice(0, 1);
else break;
} else break;
}
} else {
for (var pos = tmpBuffer.length - 1; pos >= 0; pos--) {
var testPos = determineTestPosition(pos);
if (getActiveTests()[testPos].optionality) {
if (!isMask(pos) || !isValid(pos, buffer[pos], true))
tmpBuffer.pop();
else break;
} else break;
}
}
writeBuffer(input, tmpBuffer);
}
//functionality fn
this.unmaskedvalue = function ($input, skipDatepickerCheck) {
return unmaskedvalue($input, skipDatepickerCheck);
};
function unmaskedvalue($input, skipDatepickerCheck) {
if (getActiveTests() && (skipDatepickerCheck === true || !$input.hasClass('hasDatepicker'))) {
//checkVal(input, false, true);
return $.map(getActiveBuffer(), function (element, index) {
return isMask(index) && isValid(index, element, true) ? element : null;
}).join('');
} else {
return $input[0]._valueGet();
}
}
function caret(input, begin, end) {
var npt = input.jquery && input.length > 0 ? input[0] : input, range;
if (typeof begin == 'number') {
if (!$(input).is(':visible')) {
return;
}
end = (typeof end == 'number') ? end : begin;
if (opts.insertMode == false && begin == end) end++; //set visualization for insert/overwrite mode
if (npt.setSelectionRange) {
npt.selectionStart = begin;
npt.selectionEnd = android ? begin : end;
} else if (npt.createTextRange) {
range = npt.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', begin);
range.select();
}
} else {
if (!$(input).is(':visible')) {
return { "begin": 0, "end": 0 };
}
if (npt.setSelectionRange) {
begin = npt.selectionStart;
end = npt.selectionEnd;
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
begin = 0 - range.duplicate().moveStart('character', -100000);
end = begin + range.text.length;
}
return { "begin": begin, "end": end };
}
};
this.isComplete = function (buffer) {
return isComplete(buffer);
};
function isComplete(buffer) {
var complete = false, highestValidPosition = 0, currentActiveMasksetIndex = activeMasksetIndex;
$.each(masksets, function (ndx, ms) {
activeMasksetIndex = ndx;
var aml = seekPrevious(getMaskLength());
if (ms["lastValidPosition"] != undefined && ms["lastValidPosition"] >= highestValidPosition && ms["lastValidPosition"] == aml) {
var msComplete = true;
for (var i = 0; i <= aml; i++) {
var mask = isMask(i), testPos = determineTestPosition(i);
if ((mask && (buffer[i] == undefined || buffer[i] == getPlaceHolder(i))) || (!mask && buffer[i] != getActiveBufferTemplate()[testPos])) {
msComplete = false;
break;
}
}
complete = complete || msComplete;
if (complete) //break loop
return false;
}
highestValidPosition = ms["lastValidPosition"];
});
activeMasksetIndex = currentActiveMasksetIndex; //reset activeMaskset
return complete;
}
this.mask = function (el) {
var $input = $(el);
if (!$input.is(":input")) return;
//store tests & original buffer in the input element - used to get the unmasked value
$input.data('_inputmask', {
'masksets': masksets,
'activeMasksetIndex': activeMasksetIndex,
'opts': opts,
'isRTL': false
});
//show tooltip
if (opts.showTooltip) {
$input.prop("title", getActiveMaskSet()["mask"]);
}
//correct greedy setting if needed
getActiveMaskSet()['greedy'] = getActiveMaskSet()['greedy'] ? getActiveMaskSet()['greedy'] : getActiveMaskSet()['repeat'] == 0;
//handle maxlength attribute
var maxLength = $input.prop('maxLength');
if (getMaskLength() > maxLength && maxLength > -1) { //FF sets no defined max length to -1
if (maxLength < getActiveBufferTemplate().length) getActiveBufferTemplate().length = maxLength;
if (getActiveMaskSet()['greedy'] == false) {
getActiveMaskSet()['repeat'] = Math.round(maxLength / getActiveBufferTemplate().length);
}
$input.prop('maxLength', getMaskLength() * 2);
}
patchValueProperty(el);
//init vars
getActiveMaskSet()["undoBuffer"] = el._valueGet();
var skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
ignorable = false,
isRTL = false;
if (el.dir == "rtl" || opts.numericInput) {
if (el.dir == "rtl" || (opts.numericInput && opts.rightAlignNumerics))
$input.css("text-align", "right");
el.dir = "ltr";
$input.removeAttr("dir");
var inputData = $input.data('_inputmask');
inputData['isRTL'] = true;
$input.data('_inputmask', inputData);
isRTL = true;
}
//unbind all events - to make sure that no other mask will interfere when re-masking
$input.unbind(".inputmask");
$input.removeClass('focus.inputmask');
//bind events
$input.closest('form').bind("submit", function () { //trigger change on submit if any
if ($input[0]._valueGet && $input[0]._valueGet() != getActiveMaskSet()["undoBuffer"]) {
$input.change();
}
});
$input.bind("mouseenter.inputmask", function () {
var $input = $(this), input = this;
if (!$input.hasClass('focus.inputmask') && opts.showMaskOnHover) {
if (input._valueGet() != getActiveBuffer().join('')) {
writeBuffer(input, getActiveBuffer());
}
}
}).bind("blur.inputmask", function () {
var $input = $(this), input = this, nptValue = input._valueGet(), buffer = getActiveBuffer();
$input.removeClass('focus.inputmask');
if (nptValue != getActiveMaskSet()["undoBuffer"]) {
$input.change();
}
if (opts.clearMaskOnLostFocus && nptValue != '') {
if (nptValue == getActiveBufferTemplate().join(''))
input._valueSet('');
else { //clearout optional tail of the mask
clearOptionalTail(input);
}
}
if (!isComplete(buffer)) {
$input.trigger("incomplete");
if (opts.clearIncomplete) {
$.each(masksets, function (ndx, ms) {
ms["buffer"] = ms["_buffer"].slice();
ms["lastValidPosition"] = undefined;
ms["p"] = isRTL ? getMaskLength() : 0;
});
activeMasksetIndex = 0;
if (opts.clearMaskOnLostFocus)
input._valueSet('');
else {
buffer = getActiveBufferTemplate().slice();
writeBuffer(input, buffer);
}
}
}
}).bind("focus.inputmask", function () {
var $input = $(this), input = this, nptValue = input._valueGet();
if (opts.showMaskOnFocus && !$input.hasClass('focus.inputmask') && (!opts.showMaskOnHover || (opts.showMaskOnHover && nptValue == ''))) {
if (input._valueGet() != getActiveBuffer().join('')) {
writeBuffer(input, getActiveBuffer(), getActiveMaskSet()["p"]);
}
}
$input.addClass('focus.inputmask');
getActiveMaskSet()["undoBuffer"] = input._valueGet();
$input.click();
}).bind("mouseleave.inputmask", function () {
var $input = $(this), input = this;
if (opts.clearMaskOnLostFocus) {
if (!$input.hasClass('focus.inputmask')) {
if (input._valueGet() == getActiveBufferTemplate().join('') || input._valueGet() == '')
input._valueSet('');
else { //clearout optional tail of the mask
clearOptionalTail(input);
}
}
}
}).bind("click.inputmask", function () {
var input = this;
setTimeout(function () {
var selectedCaret = caret(input), buffer = getActiveBuffer();
if (selectedCaret.begin == selectedCaret.end) {
var clickPosition = selectedCaret.begin,
lvp = getActiveMaskSet()["lastValidPosition"],
lastPosition;
determineInputDirection(input, selectedCaret);
if (isRTL) {
if (opts.numericInput) {
lastPosition = opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1 ? $.inArray(opts.radixPoint, buffer) : getMaskLength();
} else {
lastPosition = seekPrevious((lvp == undefined ? getMaskLength() : lvp) + 1);
}
caret(input, clickPosition > lastPosition && (isValid(clickPosition, buffer[clickPosition], true, isRTL) !== false || !isMask(clickPosition)) ? clickPosition : lastPosition);
} else {
lastPosition = seekNext(lvp == undefined ? -1 : lvp);
caret(input, clickPosition < lastPosition && (isValid(clickPosition, buffer[clickPosition], true, isRTL) !== false || !isMask(clickPosition)) ? clickPosition : lastPosition);
}
}
}, 0);
}).bind('dblclick.inputmask', function () {
var input = this;
if (getActiveMaskSet()["lastValidPosition"] != undefined) {
setTimeout(function () {
isRTL ?
caret(input, seekPrevious(getActiveMaskSet()["lastValidPosition"]), getMaskLength()) :
caret(input, 0, seekNext(getActiveMaskSet()["lastValidPosition"]));
}, 0);
}
}).bind("keydown.inputmask", keydownEvent
).bind("keypress.inputmask", keypressEvent
).bind("keyup.inputmask", keyupEvent
).bind(pasteEvent + ".inputmask dragdrop.inputmask drop.inputmask", function () {
var input = this, $input = $(input);
setTimeout(function () {
checkVal(input, true, false);
if (isComplete(getActiveBuffer()))
$input.trigger("complete");
$input.click();
}, 0);
}).bind('setvalue.inputmask', function () {
var input = this;
getActiveMaskSet()["undoBuffer"] = input._valueGet();
checkVal(input, true);
if (input._valueGet() == getActiveBufferTemplate().join(''))
input._valueSet('');
}).bind('complete.inputmask', opts.oncomplete)
.bind('incomplete.inputmask', opts.onincomplete)
.bind('cleared.inputmask', opts.oncleared);
//apply mask
checkVal(el, true, false);
// Wrap document.activeElement in a try/catch block since IE9 throw "Unspecified error" if document.activeElement is undefined when we are in an IFrame.
var activeElement;
try {
activeElement = document.activeElement;
} catch (e) {
}
if (activeElement === el) { //position the caret when in focus
$input.addClass('focus.inputmask');
caret(el, getActiveMaskSet()["p"]);
} else if (opts.clearMaskOnLostFocus) {
if (getActiveBuffer().join('') == getActiveBufferTemplate().join('')) {
el._valueSet('');
} else {
clearOptionalTail(el);
}
} else {
writeBuffer(el, getActiveBuffer());
}
installEventRuler(el);
//private functions
function installEventRuler(npt) {
var events = $._data(npt).events;
$.each(events, function (eventType, eventHandlers) {
$.each(eventHandlers, function (ndx, eventHandler) {
if (eventHandler.namespace == "inputmask") {
var handler = eventHandler.handler;
eventHandler.handler = function (e) {
if (this.readOnly || this.disabled)
e.preventDefault;
else
return handler.apply(this, arguments);
};
}
});
});
}
function patchValueProperty(npt) {
var valueProperty;
if (Object.getOwnPropertyDescriptor)
valueProperty = Object.getOwnPropertyDescriptor(npt, "value");
if (valueProperty && valueProperty.get) {
if (!npt._valueGet) {
npt._valueGet = valueProperty.get;
npt._valueSet = valueProperty.set;
Object.defineProperty(npt, "value", {
get: function () {
var $self = $(this), inputData = $(this).data('_inputmask'), masksets = inputData['masksets'],
activeMasksetIndex = inputData['activeMasksetIndex'];
return inputData && inputData['opts'].autoUnmask ? $self.inputmask('unmaskedvalue') : this._valueGet() != masksets[activeMasksetIndex]['_buffer'].join('') ? this._valueGet() : '';
},
set: function (value) {
this._valueSet(value);
$(this).triggerHandler('setvalue.inputmask');
}
});
}
} else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
if (!npt._valueGet) {
npt._valueGet = npt.__lookupGetter__("value");
npt._valueSet = npt.__lookupSetter__("value");
npt.__defineGetter__("value", function () {
var $self = $(this), inputData = $(this).data('_inputmask'), masksets = inputData['masksets'],
activeMasksetIndex = inputData['activeMasksetIndex'];
return inputData && inputData['opts'].autoUnmask ? $self.inputmask('unmaskedvalue') : this._valueGet() != masksets[activeMasksetIndex]['_buffer'].join('') ? this._valueGet() : '';
});
npt.__defineSetter__("value", function (value) {
this._valueSet(value);
$(this).triggerHandler('setvalue.inputmask');
});
}
} else {
if (!npt._valueGet) {
npt._valueGet = function () { return this.value; };
npt._valueSet = function (value) { this.value = value; };
}
if ($.fn.val.inputmaskpatch != true) {
$.fn.val = function () {
if (arguments.length == 0) {
var $self = $(this);
if ($self.data('_inputmask')) {
if ($self.data('_inputmask')['opts'].autoUnmask)
return $self.inputmask('unmaskedvalue');
else {
var result = $.inputmask.val.apply($self);
var inputData = $(this).data('_inputmask'), masksets = inputData['masksets'],
activeMasksetIndex = inputData['activeMasksetIndex'];
return result != masksets[activeMasksetIndex]['_buffer'].join('') ? result : '';
}
} else return $.inputmask.val.apply($self);
} else {
var args = arguments;
return this.each(function () {
var $self = $(this);
var result = $.inputmask.val.apply($self, args);
if ($self.data('_inputmask')) $self.triggerHandler('setvalue.inputmask');
return result;
});
}
};
$.extend($.fn.val, {
inputmaskpatch: true
});
}
}
}
function determineInputDirection(input, pos) {
//set input direction according the position to the radixPoint
if (opts.numericInput && opts.radixPoint != "" && opts.skipRadixDance === false) {
var nptStr = input._valueGet();
var radixPosition = nptStr.indexOf(opts.radixPoint);
isRTL = pos.begin <= radixPosition || pos.end <= radixPosition || radixPosition == -1;
}
}
//shift chars to left from start to end and put c at end position if defined
function shiftL(start, end, c) {
var buffer = getActiveBuffer();
while (!isMask(start) && start - 1 >= 0) start--; //jumping over nonmask position
for (var i = start; i < end && i < getMaskLength() ; i++) {
if (isMask(i)) {
setReTargetPlaceHolder(buffer, i);
var j = seekNext(i);
var p = getBufferElement(buffer, j);
if (p != getPlaceHolder(j)) {
if (j < getMaskLength() && isValid(i, p, true, isRTL) !== false && getActiveTests()[determineTestPosition(i)].def == getActiveTests()[determineTestPosition(j)].def) {
setBufferElement(buffer, i, getBufferElement(buffer, j), true, isRTL);
if (j < end) {
setReTargetPlaceHolder(buffer, j); //cleanup next position
}
} else {
if (isMask(i))
break;
}
} //else if (c == undefined) break;
} else {
setReTargetPlaceHolder(buffer, i);
}
}
if (c != undefined)
setBufferElement(buffer, isRTL ? end : seekPrevious(end), c);
if (getActiveMaskSet()["greedy"] == false) {
var trbuffer = truncateInput(buffer.join(''), isRTL).split('');
buffer.length = trbuffer.length;
for (var i = 0, bl = buffer.length; i < bl; i++) {
buffer[i] = trbuffer[i];
}
if (buffer.length == 0) getActiveMaskSet()["buffer"] = getActiveBufferTemplate().slice();
}
return start; //return the used start position
}
function shiftR(start, end, c, full) { //full => behave like a push right ~ do not stop on placeholders
var buffer = getActiveBuffer();
for (var i = start; i <= end && i < getMaskLength() ; i++) {
if (isMask(i)) {
var t = getBufferElement(buffer, i, true);
setBufferElement(buffer, i, c, true, isRTL);
if (t != getPlaceHolder(i)) {
var j = seekNext(i);
if (j < getMaskLength()) {
if (isValid(j, t, true, isRTL) !== false && getActiveTests()[determineTestPosition(i)].def == getActiveTests()[determineTestPosition(j)].def)
c = t;
else {
if (isMask(j))
break;
else c = t;
}
} else break;
} else {
c = t;
if (full !== true) break;
}
} else
setReTargetPlaceHolder(buffer, i);
}
var lengthBefore = buffer.length;
if (getActiveMaskSet()["greedy"] == false) {
var trbuffer = truncateInput(buffer.join(''), isRTL).split('');
buffer.length = trbuffer.length;
for (var i = 0, bl = buffer.length; i < bl; i++) {
buffer[i] = trbuffer[i];
}
if (buffer.length == 0) getActiveMaskSet()["buffer"] = getActiveBufferTemplate().slice();
}
return end - (lengthBefore - buffer.length); //return new start position
}
;
function keydownEvent(e) {
//Safari 5.1.x - modal dialog fires keypress twice workaround
skipKeyPressEvent = false;
var input = this, k = e.keyCode, pos = caret(input);
determineInputDirection(input, pos);
//backspace, delete, and escape get special treatment
if (k == opts.keyCode.BACKSPACE || k == opts.keyCode.DELETE || (iphone && k == 127) || (e.ctrlKey && k == 88)) { //backspace/delete
e.preventDefault(); //stop default action but allow propagation
var beginPos = pos.begin;
if (pos.begin == 0 && pos.end == getMaskLength()) {
clearBuffer(getActiveBuffer(), pos.begin, pos.end);
$.each(masksets, function (ndx, ms) {
ms["buffer"] = ms["_buffer"].slice();
ms["lastValidPosition"] = undefined;
ms["p"] = isRTL ? getMaskLength() : 0;
});
} else if ((pos.end - pos.begin) > 1 || ((pos.end - pos.begin) == 1 && opts.insertMode)) { //partial selection
clearBuffer(getActiveBuffer(), pos.begin, pos.end);
var ml = getMaskLength();
if (opts.greedy == false) {
isRTL ? shiftR(0, pos.end - 1, getPlaceHolder(pos.end), true) : shiftL(pos.begin, ml);
} else {
for (var i = pos.begin; i < pos.end; i++) {
if (isMask(i))
isRTL ? shiftR(0, pos.end - 1, getPlaceHolder(pos.end), true) : shiftL(pos.begin, ml);
}
}
checkVal(input, false, true, getActiveBuffer());
} else {
$.each(masksets, function (ndx, ms) {
activeMasksetIndex = ndx;
beginPos = android53x ? pos.end : pos.begin;
var buffer = getActiveBuffer(), firstMaskPos = isRTL ? seekPrevious(getMaskLength() + 1) : seekNext(-1),
maskL = getMaskLength();
if (k == opts.keyCode.DELETE) { //handle delete
if (isRTL ? beginPos > firstMaskPos : beginPos < firstMaskPos)
beginPos = firstMaskPos;
if (beginPos < maskL) {
if (opts.numericInput && opts.radixPoint != "" && buffer[beginPos] == opts.radixPoint) {
beginPos = (buffer.length - 1 == beginPos) /* radixPoint is latest? delete it */ ? beginPos : seekNext(beginPos);
beginPos = shiftL(beginPos, maskL);
} else {
if (isRTL) {
beginPos = shiftR(0, beginPos, getPlaceHolder(beginPos), true);
beginPos = seekNext(beginPos);
} else {
beginPos = shiftL(beginPos, maskL);
}
}
if (getActiveMaskSet()['lastValidPosition'] != undefined) {
if (getActiveMaskSet()['lastValidPosition'] != -1 && getActiveBuffer()[getActiveMaskSet()['lastValidPosition']] == getActiveBufferTemplate()[getActiveMaskSet()['lastValidPosition']])
getActiveMaskSet()["lastValidPosition"] = isRTL ? seekNext(getActiveMaskSet()["lastValidPosition"]) : (getActiveMaskSet()["lastValidPosition"] == 0 ? -1 : seekPrevious(getActiveMaskSet()["lastValidPosition"]));
if (isRTL ? getActiveMaskSet()['lastValidPosition'] > firstMaskPos : getActiveMaskSet()['lastValidPosition'] < firstMaskPos) {
getActiveMaskSet()["lastValidPosition"] = undefined;
getActiveMaskSet()["p"] = firstMaskPos;
} else {
getActiveMaskSet()["writeOutBuffer"] = true;
getActiveMaskSet()["p"] = beginPos;
}
}
}
} else if (k == opts.keyCode.BACKSPACE) { //handle backspace
if (isRTL ? beginPos <= firstMaskPos : beginPos > firstMaskPos) {
beginPos -= 1;
if (opts.numericInput && opts.radixPoint != "" && buffer[beginPos] == opts.radixPoint) {
beginPos = shiftR(0, (buffer.length - 1 == beginPos) /* radixPoint is latest? delete it */ ? beginPos : beginPos - 1, getPlaceHolder(beginPos), true);
beginPos++;
} else {
if (isRTL) {
beginPos = shiftR(0, beginPos, getPlaceHolder(beginPos), true);
beginPos = buffer[beginPos + 1] == opts.radixPoint ? beginPos + 1 : seekNext(beginPos);
} else {
beginPos = shiftL(beginPos, maskL);
}
}
if (getActiveMaskSet()['lastValidPosition'] != undefined) {
if (getActiveMaskSet()['lastValidPosition'] != -1 && getActiveBuffer()[getActiveMaskSet()['lastValidPosition']] == getActiveBufferTemplate()[getActiveMaskSet()['lastValidPosition']])
getActiveMaskSet()["lastValidPosition"] = isRTL ? seekNext(getActiveMaskSet()["lastValidPosition"]) : (getActiveMaskSet()["lastValidPosition"] == 0 ? -1 : seekPrevious(getActiveMaskSet()["lastValidPosition"]));
if (isRTL ? getActiveMaskSet()['lastValidPosition'] > firstMaskPos : getActiveMaskSet()['lastValidPosition'] < firstMaskPos) {
getActiveMaskSet()["lastValidPosition"] = undefined;
getActiveMaskSet()["p"] = firstMaskPos;
} else {
getActiveMaskSet()["writeOutBuffer"] = true;
getActiveMaskSet()["p"] = beginPos;
}
}
} else if (activeMasksetIndex > 0) { //retry other masks
getActiveMaskSet()["lastValidPosition"] = undefined;
getActiveMaskSet()["writeOutBuffer"] = true;
getActiveMaskSet()["p"] = firstMaskPos;
//init first
activeMasksetIndex = 0;
getActiveMaskSet()["buffer"] = getActiveBufferTemplate().slice();
getActiveMaskSet()["p"] = isRTL ? seekPrevious(getMaskLength() + 1) : seekNext(-1);
getActiveMaskSet()["lastValidPosition"] = undefined;
}
}
});
}
determineActiveMasksetIndex(isRTL);
writeBuffer(input, getActiveBuffer(), getActiveMaskSet()["p"]);
if (input._valueGet() == getActiveBufferTemplate().join(''))
$(input).trigger('cleared');
if (opts.showTooltip) { //update tooltip
$input.prop("title", getActiveMaskSet()["mask"]);
}
} else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
setTimeout(function () {
var caretPos = isRTL ? getActiveMaskSet()["lastValidPosition"] : seekNext(getActiveMaskSet()["lastValidPosition"]);
if (!opts.insertMode && caretPos == getMaskLength() && !e.shiftKey) caretPos--;
caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
}, 0);
} else if ((k == opts.keyCode.HOME && !e.shiftKey) || k == opts.keyCode.PAGE_UP) { //Home or page_up
caret(input, 0, e.shiftKey ? pos.begin : 0);
} else if (k == opts.keyCode.ESCAPE) { //escape
input._valueSet(getActiveMaskSet()["undoBuffer"]);
checkVal(input, true, true);
} else if (k == opts.keyCode.INSERT && !(e.shiftKey || e.ctrlKey)) { //insert
opts.insertMode = !opts.insertMode;
caret(input, !opts.insertMode && pos.begin == getMaskLength() ? pos.begin - 1 : pos.begin);
} else if (opts.insertMode == false && !e.shiftKey) {
if (k == opts.keyCode.RIGHT) {
setTimeout(function () {
var caretPos = caret(input);
caret(input, caretPos.begin);
}, 0);
} else if (k == opts.keyCode.LEFT) {
setTimeout(function () {
var caretPos = caret(input);
caret(input, caretPos.begin - 1);
}, 0);
}
}
opts.onKeyDown.call(this, e, getActiveBuffer(), opts); //extra stuff to execute on keydown
ignorable = $.inArray(k, opts.ignorables) != -1;
}
function keypressEvent(e, checkval, k, writeOut, strict, ndx, rtl) {
isRTL = rtl == undefined ? isRTL : rtl;
//Safari 5.1.x - modal dialog fires keypress twice workaround
if (k == undefined && skipKeyPressEvent) return false;
skipKeyPressEvent = true;
var input = this, $input = $(input);
e = e || window.event;
var k = k || e.which || e.charCode || e.keyCode,
c = String.fromCharCode(k);
if (opts.numericInput && c == opts.radixPoint && checkval !== true) {
var nptStr = input._valueGet();
var radixPosition = nptStr.indexOf(opts.radixPoint);
caret(input, seekNext(radixPosition != -1 ? radixPosition : getMaskLength()));
}
if ((e.ctrlKey || e.metaKey || ignorable) && checkval !== true) {
return true;
} else {
if (k) {
var pos, results, result;
if (checkval) {
var pcaret = strict ? ndx : (opts.numericInput ? seekNext(getActiveMaskSet()["p"]) : getActiveMaskSet()["p"]);
pos = { begin: pcaret, end: pcaret };
} else {
pos = caret(input);
}
//should we clear a possible selection??
var isSelection = (pos.end - pos.begin) > 1 || ((pos.end - pos.begin) == 1 && opts.insertMode), redetermineLVP = false;
if (isSelection) {
var initialIndex = activeMasksetIndex;
$.each(masksets, function (ndx, lmnt) {
activeMasksetIndex = ndx;
getActiveMaskSet()["undoBuffer"] = getActiveBuffer().join(''); //init undobuffer for recovery when not valid
var posend = pos.end < getMaskLength() ? pos.end : getMaskLength();
clearBuffer(getActiveBuffer(), pos.begin, posend);
var ml = getMaskLength();
if (opts.greedy == false) {
isRTL ? shiftR(0, posend - 1, getPlaceHolder(posend), true) : shiftL(pos.begin, ml);
} else {
for (var i = pos.begin; i < posend; i++) {
if (isMask(i))
isRTL ? shiftR(0, posend - 1, getPlaceHolder(posend - 1), true) : shiftL(pos.begin, ml);
}
}
if (getActiveMaskSet()["lastValidPosition"] > pos.begin && getActiveMaskSet()["lastValidPosition"] < posend) {
getActiveMaskSet()["lastValidPosition"] = isRTL ? seekNext(posend) : seekPrevious(pos.begin);
} else {
redetermineLVP = true;
}
});
if (redetermineLVP === true) {
activeMasksetIndex = initialIndex;
checkVal(input, false, true, getActiveBuffer());
if (!opts.insertMode) { //preserve some space
$.each(masksets, function (ndx, lmnt) {
activeMasksetIndex = ndx;
isRTL ? shiftL(0, posend) : shiftR(pos.begin, getMaskLength(), getPlaceHolder(pos.begin), true);
getActiveMaskSet()["lastValidPosition"] = isRTL ? seekPrevious(getActiveMaskSet()["lastValidPosition"]) : seekNext(getActiveMaskSet()["lastValidPosition"]);
});
}
}
activeMasksetIndex = initialIndex; //restore index
}
if (isRTL) {
var p = seekPrevious(pos.end);
results = isValid(p, c, strict, isRTL);
if (strict === true) results = [{ "activeMasksetIndex": activeMasksetIndex, "result": results }];
$.each(results, function (index, result) {
activeMasksetIndex = result["activeMasksetIndex"];
getActiveMaskSet()["writeOutBuffer"] = true;
var np = result["result"];
if (np !== false) {
var refresh = false, buffer = getActiveBuffer();
if (np !== true) {
refresh = np["refresh"]; //only rewrite buffer from isValid
p = np.pos != undefined ? np.pos : p; //set new position from isValid
c = np.c != undefined ? np.c : c; //set new char from isValid
}
if (refresh !== true) {
var maskL = getMaskLength(); //update masklength to include possible groupSeparator offset
var firstMaskPos = seekNext(-1), firstUnmaskedPosition = firstMaskPos;
if (opts.insertMode == true) {
if (getActiveMaskSet()['greedy'] == true) {
var bfrClone = buffer.slice();
while (getBufferElement(bfrClone, firstUnmaskedPosition, true) != getPlaceHolder(firstUnmaskedPosition) && firstUnmaskedPosition <= p) {
firstUnmaskedPosition = firstUnmaskedPosition == maskL ? (maskL + 1) : seekNext(firstUnmaskedPosition);
}
}
if (firstUnmaskedPosition <= p && (getActiveMaskSet()['greedy'] || (buffer.length < maskL || getBufferElement(buffer, p) == getPlaceHolder(p)))) {
if (buffer[firstMaskPos] != getPlaceHolder(firstMaskPos) && buffer.length < maskL) {
var offset = prepareBuffer(buffer, -1, isRTL);
if ((isSelection ? pos.begin : pos.end) != 0) p = p + offset;
maskL = buffer.length;
}
shiftL(firstUnmaskedPosition, p, c);
//shift the lvp if needed
var lvp = getActiveMaskSet()["lastValidPosition"], plvp = seekPrevious(lvp);
if (lvp <= p && (getBufferElement(getActiveBuffer(), plvp) != getPlaceHolder(plvp))) {
getActiveMaskSet()["lastValidPosition"] = plvp;
}
} else getActiveMaskSet()["writeOutBuffer"] = false;
} else setBufferElement(buffer, p, c, true, isRTL);
}
getActiveMaskSet()["p"] = p;
}
});
} else {
var p = seekNext(pos.begin - 1);
results = isValid(p, c, strict, isRTL);
if (strict === true) results = [{ "activeMasksetIndex": activeMasksetIndex, "result": results }];
$.each(results, function (index, result) {
activeMasksetIndex = result["activeMasksetIndex"];
getActiveMaskSet()["writeOutBuffer"] = true;
var np = result["result"];
if (np !== false) {
var refresh = false, buffer = getActiveBuffer();
if (np !== true) {
refresh = np["refresh"]; //only rewrite buffer from isValid
p = np.pos != undefined ? np.pos : p; //set new position from isValid
c = np.c != undefined ? np.c : c; //set new char from isValid
}
if (refresh !== true) {
if (opts.insertMode == true) {
var lastUnmaskedPosition = getMaskLength();
var bfrClone = buffer.slice();
while (getBufferElement(bfrClone, lastUnmaskedPosition, true) != getPlaceHolder(lastUnmaskedPosition) && lastUnmaskedPosition >= p) {
lastUnmaskedPosition = lastUnmaskedPosition == 0 ? -1 : seekPrevious(lastUnmaskedPosition);
}
if (lastUnmaskedPosition >= p) {
shiftR(p, buffer.length, c);
//shift the lvp if needed
var lvp = getActiveMaskSet()["lastValidPosition"], nlvp = seekNext(lvp);
if (nlvp != getMaskLength() && lvp >= p && (getBufferElement(getActiveBuffer(), nlvp) != getPlaceHolder(nlvp))) {
getActiveMaskSet()["lastValidPosition"] = nlvp;
}
} else getActiveMaskSet()["writeOutBuffer"] = false;
} else setBufferElement(buffer, p, c, true, isRTL);
}
getActiveMaskSet()["p"] = seekNext(p);
}
});
}
if (strict !== true) determineActiveMasksetIndex(isRTL);
if (writeOut !== false) {
$.each(results, function (ndx, rslt) {
if (rslt["activeMasksetIndex"] == activeMasksetIndex) {
result = rslt;
return false;
}
});
if (result != undefined) {
var self = this;
setTimeout(function () { opts.onKeyValidation.call(self, result["result"], opts); }, 0);
if (getActiveMaskSet()["writeOutBuffer"] && result["result"] !== false) {
var buffer = getActiveBuffer();
writeBuffer(input, buffer, checkval ? undefined : (opts.numericInput ? seekNext(getActiveMaskSet()["p"]) : getActiveMaskSet()["p"]));
setTimeout(function () { //timeout needed for IE
if (isComplete(buffer))
$input.trigger("complete");
}, 0);
} else if (isSelection) {
getActiveMaskSet()["buffer"] = getActiveMaskSet()["undoBuffer"].split('');
}
}
}
if (opts.showTooltip) { //update tooltip
$input.prop("title", getActiveMaskSet()["mask"]);
}
e.preventDefault();
}
}
}
function keyupEvent(e) {
var $input = $(this), input = this, k = e.keyCode, buffer = getActiveBuffer();
opts.onKeyUp.call(this, e, buffer, opts); //extra stuff to execute on keyup
if (k == opts.keyCode.TAB && $input.hasClass('focus.inputmask') && input._valueGet().length == 0 && opts.showMaskOnFocus) {
buffer = getActiveBufferTemplate().slice();
writeBuffer(input, buffer);
if (!isRTL) caret(input, 0);
getActiveMaskSet()["undoBuffer"] = input._valueGet();
}
}
};
return this;
};
return this;
};
}
})(jQuery);
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) 2010 - 2013 Robin Herbots
Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
Version: 2.3.0
Optional extensions on the jquery.inputmask base
*/
(function ($) {
//extra definitions
$.extend($.inputmask.defaults.definitions, {
'A': {
validator: "[A-Za-z]",
cardinality: 1,
casing: "upper" //auto uppercasing
},
'#': {
validator: "[A-Za-z\u0410-\u044F\u0401\u04510-9]",
cardinality: 1,
casing: "upper"
}
});
$.extend($.inputmask.defaults.aliases, {
'url': {
mask: "ir",
placeholder: "",
separator: "",
defaultPrefix: "http://",
regex: {
urlpre1: new RegExp("[fh]"),
urlpre2: new RegExp("(ft|ht)"),
urlpre3: new RegExp("(ftp|htt)"),
urlpre4: new RegExp("(ftp:|http|ftps)"),
urlpre5: new RegExp("(ftp:/|ftps:|http:|https)"),
urlpre6: new RegExp("(ftp://|ftps:/|http:/|https:)"),
urlpre7: new RegExp("(ftp://|ftps://|http://|https:/)"),
urlpre8: new RegExp("(ftp://|ftps://|http://|https://)")
},
definitions: {
'i': {
validator: function (chrs, buffer, pos, strict, opts) {
return true;
},
cardinality: 8,
prevalidator: (function () {
var result = [], prefixLimit = 8;
for (var i = 0; i < prefixLimit; i++) {
result[i] = (function () {
var j = i;
return {
validator: function (chrs, buffer, pos, strict, opts) {
if (opts.regex["urlpre" + (j + 1)]) {
var tmp = chrs, k;
if (((j + 1) - chrs.length) > 0) {
tmp = buffer.join('').substring(0, ((j + 1) - chrs.length)) + "" + tmp;
}
var isValid = opts.regex["urlpre" + (j + 1)].test(tmp);
if (!strict && !isValid) {
pos = pos - j;
for (k = 0; k < opts.defaultPrefix.length; k++) {
buffer[pos] = opts.defaultPrefix[k]; pos++;
}
for (k = 0; k < tmp.length - 1; k++) {
buffer[pos] = tmp[k]; pos++;
}
return { "pos": pos };
}
return isValid;
} else {
return false;
}
}, cardinality: j
};
})();
}
return result;
})()
},
"r": {
validator: ".",
cardinality: 50
}
},
insertMode: false,
autoUnmask: false
},
"ip": {
mask: "i.i.i.i",
definitions: {
'i': {
validator: "25[0-5]|2[0-4][0-9]|[01][0-9][0-9]",
cardinality: 3,
prevalidator: [
{ validator: "[0-2]", cardinality: 1 },
{ validator: "2[0-5]|[01][0-9]", cardinality: 2 }
]
}
}
}
});
})(jQuery);
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) 2010 - 2012 Robin Herbots
Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
Version: 2.3.0
Optional extensions on the jquery.inputmask base
*/
(function ($) {
//date & time aliases
$.extend($.inputmask.defaults.definitions, {
'h': { //hours
validator: "[01][0-9]|2[0-3]",
cardinality: 2,
prevalidator: [{ validator: "[0-2]", cardinality: 1 }]
},
's': { //seconds || minutes
validator: "[0-5][0-9]",
cardinality: 2,
prevalidator: [{ validator: "[0-5]", cardinality: 1 }]
},
'd': { //basic day
validator: "0[1-9]|[12][0-9]|3[01]",
cardinality: 2,
prevalidator: [{ validator: "[0-3]", cardinality: 1 }]
},
'm': { //basic month
validator: "0[1-9]|1[012]",
cardinality: 2,
prevalidator: [{ validator: "[01]", cardinality: 1 }]
},
'y': { //basic year
validator: "(19|20)\\d{2}",
cardinality: 4,
prevalidator: [
{ validator: "[12]", cardinality: 1 },
{ validator: "(19|20)", cardinality: 2 },
{ validator: "(19|20)\\d", cardinality: 3 }
]
}
});
$.extend($.inputmask.defaults.aliases, {
'dd/mm/yyyy': {
mask: "1/2/y",
placeholder: "dd/mm/yyyy",
regex: {
val1pre: new RegExp("[0-3]"), //daypre
val1: new RegExp("0[1-9]|[12][0-9]|3[01]"), //day
val2pre: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|[12][0-9]|3[01])" + escapedSeparator + "[01])"); }, //monthpre
val2: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|[12][0-9])" + escapedSeparator + "(0[1-9]|1[012]))|(30" + escapedSeparator + "(0[13-9]|1[012]))|(31" + escapedSeparator + "(0[13578]|1[02]))"); }//month
},
leapday: "29/02/",
separator: '/',
yearrange: { minyear: 1900, maxyear: 2099 },
isInYearRange: function (chrs, minyear, maxyear) {
var enteredyear = parseInt(chrs.concat(minyear.toString().slice(chrs.length)));
var enteredyear2 = parseInt(chrs.concat(maxyear.toString().slice(chrs.length)));
return (enteredyear != NaN ? minyear <= enteredyear && enteredyear <= maxyear : false) ||
(enteredyear2 != NaN ? minyear <= enteredyear2 && enteredyear2 <= maxyear : false);
},
determinebaseyear: function (minyear, maxyear) {
var currentyear = (new Date()).getFullYear();
if (minyear > currentyear) return minyear;
if (maxyear < currentyear) return maxyear;
return currentyear;
},
onKeyUp: function (e, buffer, opts) {
var $input = $(this);
if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
var today = new Date();
$input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString());
}
},
definitions: {
'1': { //val1 ~ day or month
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.regex.val1.test(chrs);
if (!strict && !isValid) {
if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
isValid = opts.regex.val1.test("0" + chrs.charAt(0));
if (isValid) {
buffer[pos - 1] = "0";
return { "pos": pos, "c": chrs.charAt(0) };
}
}
}
return isValid;
},
cardinality: 2,
prevalidator: [{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.regex.val1pre.test(chrs);
if (!strict && !isValid) {
isValid = opts.regex.val1.test("0" + chrs);
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
return isValid;
}, cardinality: 1
}]
},
'2': { //val2 ~ day or month
validator: function (chrs, buffer, pos, strict, opts) {
var frontValue = buffer.join('').substr(0, 3);
var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
if (!strict && !isValid) {
if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
if (isValid) {
buffer[pos - 1] = "0";
return { "pos": pos, "c": chrs.charAt(0) };
}
}
}
return isValid;
},
cardinality: 2,
prevalidator: [{
validator: function (chrs, buffer, pos, strict, opts) {
var frontValue = buffer.join('').substr(0, 3);
var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
if (!strict && !isValid) {
isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
return isValid;
}, cardinality: 1
}]
},
'y': { //year
validator: function (chrs, buffer, pos, strict, opts) {
if (opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
var dayMonthValue = buffer.join('').substr(0, 6);
if (dayMonthValue != opts.leapday)
return true;
else {
var year = parseInt(chrs, 10);//detect leap year
if (year % 4 === 0)
if (year % 100 === 0)
if (year % 400 === 0)
return true;
else return false;
else return true;
else return false;
}
} else return false;
},
cardinality: 4,
prevalidator: [
{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (!strict && !isValid) {
var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 1);
isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (isValid) {
buffer[pos++] = yearPrefix[0];
return { "pos": pos };
}
yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 2);
isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (isValid) {
buffer[pos++] = yearPrefix[0];
buffer[pos++] = yearPrefix[1];
return { "pos": pos };
}
}
return isValid;
},
cardinality: 1
},
{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (!strict && !isValid) {
var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 2);
isValid = opts.isInYearRange(chrs[0] + yearPrefix[1] + chrs[1], opts.yearrange.minyear, opts.yearrange.maxyear);
if (isValid) {
buffer[pos++] = yearPrefix[1];
return { "pos": pos };
}
yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 2);
if (opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
var dayMonthValue = buffer.join('').substr(0, 6);
if (dayMonthValue != opts.leapday)
isValid = true;
else {
var year = parseInt(chrs, 10);//detect leap year
if (year % 4 === 0)
if (year % 100 === 0)
if (year % 400 === 0)
isValid = true;
else isValid = false;
else isValid = true;
else isValid = false;
}
} else isValid = false;
if (isValid) {
buffer[pos - 1] = yearPrefix[0];
buffer[pos++] = yearPrefix[1];
buffer[pos++] = chrs[0];
return { "pos": pos };
}
}
return isValid;
}, cardinality: 2
},
{
validator: function (chrs, buffer, pos, strict, opts) {
return opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
}, cardinality: 3
}
]
}
},
insertMode: false,
autoUnmask: false
},
'mm/dd/yyyy': {
placeholder: "mm/dd/yyyy",
alias: "dd/mm/yyyy", //reuse functionality of dd/mm/yyyy alias
regex: {
val2pre: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])"); }, //daypre
val2: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "(0[1-9]|[12][0-9]))|((0[13-9]|1[012])" + escapedSeparator + "30)|((0[13578]|1[02])" + escapedSeparator + "31)"); }, //day
val1pre: new RegExp("[01]"), //monthpre
val1: new RegExp("0[1-9]|1[012]") //month
},
leapday: "02/29/",
onKeyUp: function (e, buffer, opts) {
var $input = $(this);
if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
var today = new Date();
$input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
}
}
},
'yyyy/mm/dd': {
mask: "y/1/2",
placeholder: "yyyy/mm/dd",
alias: "mm/dd/yyyy",
leapday: "/02/29",
onKeyUp: function (e, buffer, opts) {
var $input = $(this);
if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
var today = new Date();
$input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
}
},
definitions: {
'2': { //val2 ~ day or month
validator: function (chrs, buffer, pos, strict, opts) {
var frontValue = buffer.join('').substr(5, 3);
var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
if (!strict && !isValid) {
if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
if (isValid) {
buffer[pos - 1] = "0";
return { "pos": pos, "c": chrs.charAt(0) };
}
}
}
//check leap yeap
if (isValid) {
var dayMonthValue = buffer.join('').substr(4, 4) + chrs;
if (dayMonthValue != opts.leapday)
return true;
else {
var year = parseInt(buffer.join('').substr(0, 4), 10); //detect leap year
if (year % 4 === 0)
if (year % 100 === 0)
if (year % 400 === 0)
return true;
else return false;
else return true;
else return false;
}
}
return isValid;
},
cardinality: 2,
prevalidator: [{
validator: function (chrs, buffer, pos, strict, opts) {
var frontValue = buffer.join('').substr(5, 3);
var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
if (!strict && !isValid) {
isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
return isValid;
}, cardinality: 1
}]
}
}
},
'dd.mm.yyyy': {
mask: "1.2.y",
placeholder: "dd.mm.yyyy",
leapday: "29.02.",
separator: '.',
alias: "dd/mm/yyyy"
},
'dd-mm-yyyy': {
mask: "1-2-y",
placeholder: "dd-mm-yyyy",
leapday: "29-02-",
separator: '-',
alias: "dd/mm/yyyy"
},
'mm.dd.yyyy': {
mask: "1.2.y",
placeholder: "mm.dd.yyyy",
leapday: "02.29.",
separator: '.',
alias: "mm/dd/yyyy"
},
'mm-dd-yyyy': {
mask: "1-2-y",
placeholder: "mm-dd-yyyy",
leapday: "02-29-",
separator: '-',
alias: "mm/dd/yyyy"
},
'yyyy.mm.dd': {
mask: "y.1.2",
placeholder: "yyyy.mm.dd",
leapday: ".02.29",
separator: '.',
alias: "yyyy/mm/dd"
},
'yyyy-mm-dd': {
mask: "y-1-2",
placeholder: "yyyy-mm-dd",
leapday: "-02-29",
separator: '-',
alias: "yyyy/mm/dd"
},
'datetime': {
mask: "1/2/y h:s",
placeholder: "dd/mm/yyyy hh:mm",
alias: "dd/mm/yyyy",
regex: {
hrspre: new RegExp("[012]"), //hours pre
hrs24: new RegExp("2[0-9]|1[3-9]"),
hrs: new RegExp("[01][0-9]|2[0-3]"), //hours
ampm: new RegExp("^[a|p|A|P][m|M]")
},
timeseparator: ':',
hourFormat: "24", // or 12
definitions: {
'h': { //hours
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.regex.hrs.test(chrs);
if (!strict && !isValid) {
if (chrs.charAt(1) == opts.timeseparator || "-.:".indexOf(chrs.charAt(1)) != -1) {
isValid = opts.regex.hrs.test("0" + chrs.charAt(0));
if (isValid) {
buffer[pos - 1] = "0";
buffer[pos] = chrs.charAt(0);
pos++;
return { "pos": pos };
}
}
}
if (isValid && opts.hourFormat !== "24" && opts.regex.hrs24.test(chrs)) {
var tmp = parseInt(chrs, 10);
if (tmp == 24) {
buffer[pos + 5] = "a";
buffer[pos + 6] = "m";
} else {
buffer[pos + 5] = "p";
buffer[pos + 6] = "m";
}
tmp = tmp - 12;
if (tmp < 10) {
buffer[pos] = tmp.toString();
buffer[pos - 1] = "0";
} else {
buffer[pos] = tmp.toString().charAt(1);
buffer[pos - 1] = tmp.toString().charAt(0);
}
return { "pos": pos, "c": buffer[pos] };
}
return isValid;
},
cardinality: 2,
prevalidator: [{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.regex.hrspre.test(chrs);
if (!strict && !isValid) {
isValid = opts.regex.hrs.test("0" + chrs);
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
return isValid;
}, cardinality: 1
}]
},
't': { //am/pm
validator: function (chrs, buffer, pos, strict, opts) {
return opts.regex.ampm.test(chrs + "m");
},
casing: "lower",
cardinality: 1
}
},
insertMode: false,
autoUnmask: false
},
'datetime12': {
mask: "1/2/y h:s t\\m",
placeholder: "dd/mm/yyyy hh:mm xm",
alias: "datetime",
hourFormat: "12"
},
'hh:mm t': {
mask: "h:s t\\m",
placeholder: "hh:mm xm",
alias: "datetime",
hourFormat: "12"
},
'h:s t': {
mask: "h:s t\\m",
placeholder: "hh:mm xm",
alias: "datetime",
hourFormat: "12"
},
'hh:mm:ss': {
mask: "h:s:s",
autoUnmask: false
},
'hh:mm': {
mask: "h:s",
autoUnmask: false
},
'date': {
alias: "dd/mm/yyyy" // "mm/dd/yyyy"
}
});
})(jQuery);
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) 2010 - 2013 Robin Herbots
Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
Version: 2.3.0
Optional extensions on the jquery.inputmask base
*/
(function ($) {
//number aliases
$.extend($.inputmask.defaults.aliases, {
'decimal': {
mask: "~",
placeholder: "",
repeat: "*",
greedy: false,
numericInput: true,
digits: "*", //numer of digits
groupSeparator: "",//",", // | "."
radixPoint: ".",
groupSize: 3,
autoGroup: false,
allowPlus: true,
allowMinus: true,
getMaskLength: function (buffer, greedy, repeat, currentBuffer, opts) { //custom getMaskLength to take the groupSeparator into account
var calculatedLength = buffer.length;
if (!greedy) {
if(repeat == "*") {
calculatedLength = currentBuffer.length + 1;
} else if(repeat > 1) {
calculatedLength += (buffer.length * (repeat - 1));
}
}
var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
var currentBufferStr = currentBuffer.join(''), strippedBufferStr = currentBufferStr.replace(new RegExp(escapedGroupSeparator, "g"), "").replace(new RegExp(escapedRadixPoint), ""),
groupOffset = currentBufferStr.length - strippedBufferStr.length;
return calculatedLength + groupOffset;
},
postFormat: function (buffer, pos, reformatOnly, opts) {
if (opts.groupSeparator == "") return pos;
var cbuf = buffer.slice(),
radixPos = $.inArray(opts.radixPoint, buffer);
if (!reformatOnly) {
cbuf.splice(pos == 0 || pos <= radixPos || opts.skipRadixDance ? pos + 1 : pos, 0, "?"); //set position indicator
}
var bufVal = cbuf.join('');
if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
var radixSplit = bufVal.split(opts.radixPoint);
bufVal = radixSplit[0];
var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
while (reg.test(bufVal)) {
bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
}
if (radixSplit.length > 1)
bufVal += opts.radixPoint + radixSplit[1];
}
buffer.length = bufVal.length; //align the length
for (var i = 0, l = bufVal.length; i < l; i++) {
buffer[i] = bufVal.charAt(i);
}
var newPos = $.inArray("?", buffer);
if (!reformatOnly) buffer.splice(newPos, 1);
return reformatOnly ? pos : newPos <= radixPos || (opts.skipRadixDance && newPos != 0) ? newPos - 1 : newPos;
},
regex: {
number: function (opts) {
var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
var signedExpression = "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?";
return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
}
},
onKeyDown: function (e, buffer, opts) {
var $input = $(this), input = this;
if (e.keyCode == opts.keyCode.TAB) {
var radixPosition = $.inArray(opts.radixPoint, buffer);
if (radixPosition != -1) {
var masksets = $input.data('_inputmask')['masksets'];
var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], masksets[activeMasksetIndex]["greedy"], masksets[activeMasksetIndex]["repeat"], buffer, opts) ; i++) {
if (buffer[radixPosition + i] == undefined) buffer[radixPosition + i] = "0";
}
input._valueSet(buffer.join(''));
}
} else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
opts.postFormat(buffer, 0, true, opts);
input._valueSet(buffer.join(''));
}
},
definitions: {
'~': { //real number
validator: function (chrs, buffer, pos, strict, opts) {
if (chrs == "") return false;
if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.length == 1) { //handle first char
buffer[0] = "";
return { "pos": 0 };
}
var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
cbuf.splice((pos == 0 && buffer.length == 0) ? pos : pos + 1, 0, chrs);
var bufferStr = cbuf.join('');
if (opts.autoGroup && !strict) { //strip groupseparator
var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
}
var isValid = opts.regex.number(opts).test(bufferStr);
if (!isValid) {
//let's help the regex a bit
bufferStr += "0";
isValid = opts.regex.number(opts).test(bufferStr);
if (!isValid) {
//make a valid group
var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
for (i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
bufferStr += "0";
}
isValid = opts.regex.number(opts).test(bufferStr);
if (!isValid && !strict) {
if (chrs == opts.radixPoint) {
isValid = opts.regex.number(opts).test("0" + bufferStr + "0");
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
}
}
}
if (isValid != false && !strict && chrs != opts.radixPoint) {
var newPos = opts.postFormat(buffer, pos, false, opts);
return { "pos": newPos };
}
return isValid;
},
cardinality: 1,
prevalidator: null
}
},
insertMode: true,
autoUnmask: false
},
'integer': {
regex: {
number: function (opts) {
var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
var signedExpression = "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?";
return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$");
}
},
alias: "decimal"
}
});
})(jQuery);
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) 2010 - 2013 Robin Herbots
Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
Version: 2.3.0
Regex extensions on the jquery.inputmask base
Allows for using regular expressions as a mask
*/
(function ($) {
$.extend($.inputmask.defaults.aliases, { // $(selector).inputmask("Regex", { regex: "[0-9]*"}
'Regex': {
mask: "r",
greedy: false,
repeat: "*",
regex: null,
regexSplit: null,
definitions: {
'r': {
validator: function (chrs, buffer, pos, strict, opts) {
function analyseRegex() { //ENHANCE ME
var regexSplitRegex = "\\[.*?\]\\*";
opts.regexSplit = opts.regex.match(new RegExp(regexSplitRegex, "g"));
//if (opts.regex.indexOf("*") != (opts.regex.length - 1)) {
// opts.regex += "{1}";
//}
//opts.regexSplit.push(opts.regex);
}
if (opts.regexSplit == null) {
analyseRegex();
}
var cbuffer = buffer.slice(), regexPart = "", isValid = false;
cbuffer.splice(pos, 0, chrs);
var bufferStr = cbuffer.join('');
for (var i = 0; i < opts.regexSplit.length; i++) {
regexPart += opts.regexSplit[i];
var exp = new RegExp("^" + regexPart + "$");
isValid = exp.test(bufferStr);
console.log(bufferStr + ' ' + isValid + ' ' + regexPart);
if (isValid) break;
}
return isValid;
},
cardinality: 1
}
}
}
});
})(jQuery);
/*
Copyright 2012 Igor Vaynberg
Version: 3.4.5 Timestamp: Mon Nov 4 08:22:42 PST 2013
This software is licensed under the Apache License, Version 2.0 (the "Apache License") or the GNU
General Public License version 2 (the "GPL License"). You may choose either license to govern your
use of this software only upon the condition that you accept all of the terms of either the Apache
License or the GPL License.
You may obtain a copy of the Apache License and the GPL License at:
http://www.apache.org/licenses/LICENSE-2.0
http://www.gnu.org/licenses/gpl-2.0.html
Unless required by applicable law or agreed to in writing, software distributed under the
Apache License or the GPL Licesnse is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the Apache License and the GPL License for
the specific language governing permissions and limitations under the Apache License and the GPL License.
*/
(function ($) {
if(typeof $.fn.each2 == "undefined") {
$.extend($.fn, {
/*
* 4-10 times faster .each replacement
* use it carefully, as it overrides jQuery context of element on each iteration
*/
each2 : function (c) {
var j = $([0]), i = -1, l = this.length;
while (
++i < l
&& (j.context = j[0] = this[i])
&& c.call(j[0], i, j) !== false //"this"=DOM, i=index, j=jQuery object
);
return this;
}
});
}
})(jQuery);
(function ($, undefined) {
"use strict";
/*global document, window, jQuery, console */
if (window.Select2 !== undefined) {
return;
}
var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer,
lastMousePosition={x:0,y:0}, $document, scrollBarDimensions,
KEY = {
TAB: 9,
ENTER: 13,
ESC: 27,
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
SHIFT: 16,
CTRL: 17,
ALT: 18,
PAGE_UP: 33,
PAGE_DOWN: 34,
HOME: 36,
END: 35,
BACKSPACE: 8,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
},
isControl: function (e) {
var k = e.which;
switch (k) {
case KEY.SHIFT:
case KEY.CTRL:
case KEY.ALT:
return true;
}
if (e.metaKey) return true;
return false;
},
isFunctionKey: function (k) {
k = k.which ? k.which : k;
return k >= 112 && k <= 123;
}
},
MEASURE_SCROLLBAR_TEMPLATE = "",
DIACRITICS = {"\u24B6":"A","\uFF21":"A","\u00C0":"A","\u00C1":"A","\u00C2":"A","\u1EA6":"A","\u1EA4":"A","\u1EAA":"A","\u1EA8":"A","\u00C3":"A","\u0100":"A","\u0102":"A","\u1EB0":"A","\u1EAE":"A","\u1EB4":"A","\u1EB2":"A","\u0226":"A","\u01E0":"A","\u00C4":"A","\u01DE":"A","\u1EA2":"A","\u00C5":"A","\u01FA":"A","\u01CD":"A","\u0200":"A","\u0202":"A","\u1EA0":"A","\u1EAC":"A","\u1EB6":"A","\u1E00":"A","\u0104":"A","\u023A":"A","\u2C6F":"A","\uA732":"AA","\u00C6":"AE","\u01FC":"AE","\u01E2":"AE","\uA734":"AO","\uA736":"AU","\uA738":"AV","\uA73A":"AV","\uA73C":"AY","\u24B7":"B","\uFF22":"B","\u1E02":"B","\u1E04":"B","\u1E06":"B","\u0243":"B","\u0182":"B","\u0181":"B","\u24B8":"C","\uFF23":"C","\u0106":"C","\u0108":"C","\u010A":"C","\u010C":"C","\u00C7":"C","\u1E08":"C","\u0187":"C","\u023B":"C","\uA73E":"C","\u24B9":"D","\uFF24":"D","\u1E0A":"D","\u010E":"D","\u1E0C":"D","\u1E10":"D","\u1E12":"D","\u1E0E":"D","\u0110":"D","\u018B":"D","\u018A":"D","\u0189":"D","\uA779":"D","\u01F1":"DZ","\u01C4":"DZ","\u01F2":"Dz","\u01C5":"Dz","\u24BA":"E","\uFF25":"E","\u00C8":"E","\u00C9":"E","\u00CA":"E","\u1EC0":"E","\u1EBE":"E","\u1EC4":"E","\u1EC2":"E","\u1EBC":"E","\u0112":"E","\u1E14":"E","\u1E16":"E","\u0114":"E","\u0116":"E","\u00CB":"E","\u1EBA":"E","\u011A":"E","\u0204":"E","\u0206":"E","\u1EB8":"E","\u1EC6":"E","\u0228":"E","\u1E1C":"E","\u0118":"E","\u1E18":"E","\u1E1A":"E","\u0190":"E","\u018E":"E","\u24BB":"F","\uFF26":"F","\u1E1E":"F","\u0191":"F","\uA77B":"F","\u24BC":"G","\uFF27":"G","\u01F4":"G","\u011C":"G","\u1E20":"G","\u011E":"G","\u0120":"G","\u01E6":"G","\u0122":"G","\u01E4":"G","\u0193":"G","\uA7A0":"G","\uA77D":"G","\uA77E":"G","\u24BD":"H","\uFF28":"H","\u0124":"H","\u1E22":"H","\u1E26":"H","\u021E":"H","\u1E24":"H","\u1E28":"H","\u1E2A":"H","\u0126":"H","\u2C67":"H","\u2C75":"H","\uA78D":"H","\u24BE":"I","\uFF29":"I","\u00CC":"I","\u00CD":"I","\u00CE":"I","\u0128":"I","\u012A":"I","\u012C":"I","\u0130":"I","\u00CF":"I","\u1E2E":"I","\u1EC8":"I","\u01CF":"I","\u0208":"I","\u020A":"I","\u1ECA":"I","\u012E":"I","\u1E2C":"I","\u0197":"I","\u24BF":"J","\uFF2A":"J","\u0134":"J","\u0248":"J","\u24C0":"K","\uFF2B":"K","\u1E30":"K","\u01E8":"K","\u1E32":"K","\u0136":"K","\u1E34":"K","\u0198":"K","\u2C69":"K","\uA740":"K","\uA742":"K","\uA744":"K","\uA7A2":"K","\u24C1":"L","\uFF2C":"L","\u013F":"L","\u0139":"L","\u013D":"L","\u1E36":"L","\u1E38":"L","\u013B":"L","\u1E3C":"L","\u1E3A":"L","\u0141":"L","\u023D":"L","\u2C62":"L","\u2C60":"L","\uA748":"L","\uA746":"L","\uA780":"L","\u01C7":"LJ","\u01C8":"Lj","\u24C2":"M","\uFF2D":"M","\u1E3E":"M","\u1E40":"M","\u1E42":"M","\u2C6E":"M","\u019C":"M","\u24C3":"N","\uFF2E":"N","\u01F8":"N","\u0143":"N","\u00D1":"N","\u1E44":"N","\u0147":"N","\u1E46":"N","\u0145":"N","\u1E4A":"N","\u1E48":"N","\u0220":"N","\u019D":"N","\uA790":"N","\uA7A4":"N","\u01CA":"NJ","\u01CB":"Nj","\u24C4":"O","\uFF2F":"O","\u00D2":"O","\u00D3":"O","\u00D4":"O","\u1ED2":"O","\u1ED0":"O","\u1ED6":"O","\u1ED4":"O","\u00D5":"O","\u1E4C":"O","\u022C":"O","\u1E4E":"O","\u014C":"O","\u1E50":"O","\u1E52":"O","\u014E":"O","\u022E":"O","\u0230":"O","\u00D6":"O","\u022A":"O","\u1ECE":"O","\u0150":"O","\u01D1":"O","\u020C":"O","\u020E":"O","\u01A0":"O","\u1EDC":"O","\u1EDA":"O","\u1EE0":"O","\u1EDE":"O","\u1EE2":"O","\u1ECC":"O","\u1ED8":"O","\u01EA":"O","\u01EC":"O","\u00D8":"O","\u01FE":"O","\u0186":"O","\u019F":"O","\uA74A":"O","\uA74C":"O","\u01A2":"OI","\uA74E":"OO","\u0222":"OU","\u24C5":"P","\uFF30":"P","\u1E54":"P","\u1E56":"P","\u01A4":"P","\u2C63":"P","\uA750":"P","\uA752":"P","\uA754":"P","\u24C6":"Q","\uFF31":"Q","\uA756":"Q","\uA758":"Q","\u024A":"Q","\u24C7":"R","\uFF32":"R","\u0154":"R","\u1E58":"R","\u0158":"R","\u0210":"R","\u0212":"R","\u1E5A":"R","\u1E5C":"R","\u0156":"R","\u1E5E":"R","\u024C":"R","\u2C64":"R","\uA75A":"R","\uA7A6":"R","\uA782":"R","\u24C8":"S","\uFF33":"S","\u1E9E":"S","\u015A":"S","\u1E64":"S","\u015C":"S","\u1E60":"S","\u0160":"S","\u1E66":"S","\u1E62":"S","\u1E68":"S","\u0218":"S","\u015E":"S","\u2C7E":"S","\uA7A8":"S","\uA784":"S","\u24C9":"T","\uFF34":"T","\u1E6A":"T","\u0164":"T","\u1E6C":"T","\u021A":"T","\u0162":"T","\u1E70":"T","\u1E6E":"T","\u0166":"T","\u01AC":"T","\u01AE":"T","\u023E":"T","\uA786":"T","\uA728":"TZ","\u24CA":"U","\uFF35":"U","\u00D9":"U","\u00DA":"U","\u00DB":"U","\u0168":"U","\u1E78":"U","\u016A":"U","\u1E7A":"U","\u016C":"U","\u00DC":"U","\u01DB":"U","\u01D7":"U","\u01D5":"U","\u01D9":"U","\u1EE6":"U","\u016E":"U","\u0170":"U","\u01D3":"U","\u0214":"U","\u0216":"U","\u01AF":"U","\u1EEA":"U","\u1EE8":"U","\u1EEE":"U","\u1EEC":"U","\u1EF0":"U","\u1EE4":"U","\u1E72":"U","\u0172":"U","\u1E76":"U","\u1E74":"U","\u0244":"U","\u24CB":"V","\uFF36":"V","\u1E7C":"V","\u1E7E":"V","\u01B2":"V","\uA75E":"V","\u0245":"V","\uA760":"VY","\u24CC":"W","\uFF37":"W","\u1E80":"W","\u1E82":"W","\u0174":"W","\u1E86":"W","\u1E84":"W","\u1E88":"W","\u2C72":"W","\u24CD":"X","\uFF38":"X","\u1E8A":"X","\u1E8C":"X","\u24CE":"Y","\uFF39":"Y","\u1EF2":"Y","\u00DD":"Y","\u0176":"Y","\u1EF8":"Y","\u0232":"Y","\u1E8E":"Y","\u0178":"Y","\u1EF6":"Y","\u1EF4":"Y","\u01B3":"Y","\u024E":"Y","\u1EFE":"Y","\u24CF":"Z","\uFF3A":"Z","\u0179":"Z","\u1E90":"Z","\u017B":"Z","\u017D":"Z","\u1E92":"Z","\u1E94":"Z","\u01B5":"Z","\u0224":"Z","\u2C7F":"Z","\u2C6B":"Z","\uA762":"Z","\u24D0":"a","\uFF41":"a","\u1E9A":"a","\u00E0":"a","\u00E1":"a","\u00E2":"a","\u1EA7":"a","\u1EA5":"a","\u1EAB":"a","\u1EA9":"a","\u00E3":"a","\u0101":"a","\u0103":"a","\u1EB1":"a","\u1EAF":"a","\u1EB5":"a","\u1EB3":"a","\u0227":"a","\u01E1":"a","\u00E4":"a","\u01DF":"a","\u1EA3":"a","\u00E5":"a","\u01FB":"a","\u01CE":"a","\u0201":"a","\u0203":"a","\u1EA1":"a","\u1EAD":"a","\u1EB7":"a","\u1E01":"a","\u0105":"a","\u2C65":"a","\u0250":"a","\uA733":"aa","\u00E6":"ae","\u01FD":"ae","\u01E3":"ae","\uA735":"ao","\uA737":"au","\uA739":"av","\uA73B":"av","\uA73D":"ay","\u24D1":"b","\uFF42":"b","\u1E03":"b","\u1E05":"b","\u1E07":"b","\u0180":"b","\u0183":"b","\u0253":"b","\u24D2":"c","\uFF43":"c","\u0107":"c","\u0109":"c","\u010B":"c","\u010D":"c","\u00E7":"c","\u1E09":"c","\u0188":"c","\u023C":"c","\uA73F":"c","\u2184":"c","\u24D3":"d","\uFF44":"d","\u1E0B":"d","\u010F":"d","\u1E0D":"d","\u1E11":"d","\u1E13":"d","\u1E0F":"d","\u0111":"d","\u018C":"d","\u0256":"d","\u0257":"d","\uA77A":"d","\u01F3":"dz","\u01C6":"dz","\u24D4":"e","\uFF45":"e","\u00E8":"e","\u00E9":"e","\u00EA":"e","\u1EC1":"e","\u1EBF":"e","\u1EC5":"e","\u1EC3":"e","\u1EBD":"e","\u0113":"e","\u1E15":"e","\u1E17":"e","\u0115":"e","\u0117":"e","\u00EB":"e","\u1EBB":"e","\u011B":"e","\u0205":"e","\u0207":"e","\u1EB9":"e","\u1EC7":"e","\u0229":"e","\u1E1D":"e","\u0119":"e","\u1E19":"e","\u1E1B":"e","\u0247":"e","\u025B":"e","\u01DD":"e","\u24D5":"f","\uFF46":"f","\u1E1F":"f","\u0192":"f","\uA77C":"f","\u24D6":"g","\uFF47":"g","\u01F5":"g","\u011D":"g","\u1E21":"g","\u011F":"g","\u0121":"g","\u01E7":"g","\u0123":"g","\u01E5":"g","\u0260":"g","\uA7A1":"g","\u1D79":"g","\uA77F":"g","\u24D7":"h","\uFF48":"h","\u0125":"h","\u1E23":"h","\u1E27":"h","\u021F":"h","\u1E25":"h","\u1E29":"h","\u1E2B":"h","\u1E96":"h","\u0127":"h","\u2C68":"h","\u2C76":"h","\u0265":"h","\u0195":"hv","\u24D8":"i","\uFF49":"i","\u00EC":"i","\u00ED":"i","\u00EE":"i","\u0129":"i","\u012B":"i","\u012D":"i","\u00EF":"i","\u1E2F":"i","\u1EC9":"i","\u01D0":"i","\u0209":"i","\u020B":"i","\u1ECB":"i","\u012F":"i","\u1E2D":"i","\u0268":"i","\u0131":"i","\u24D9":"j","\uFF4A":"j","\u0135":"j","\u01F0":"j","\u0249":"j","\u24DA":"k","\uFF4B":"k","\u1E31":"k","\u01E9":"k","\u1E33":"k","\u0137":"k","\u1E35":"k","\u0199":"k","\u2C6A":"k","\uA741":"k","\uA743":"k","\uA745":"k","\uA7A3":"k","\u24DB":"l","\uFF4C":"l","\u0140":"l","\u013A":"l","\u013E":"l","\u1E37":"l","\u1E39":"l","\u013C":"l","\u1E3D":"l","\u1E3B":"l","\u017F":"l","\u0142":"l","\u019A":"l","\u026B":"l","\u2C61":"l","\uA749":"l","\uA781":"l","\uA747":"l","\u01C9":"lj","\u24DC":"m","\uFF4D":"m","\u1E3F":"m","\u1E41":"m","\u1E43":"m","\u0271":"m","\u026F":"m","\u24DD":"n","\uFF4E":"n","\u01F9":"n","\u0144":"n","\u00F1":"n","\u1E45":"n","\u0148":"n","\u1E47":"n","\u0146":"n","\u1E4B":"n","\u1E49":"n","\u019E":"n","\u0272":"n","\u0149":"n","\uA791":"n","\uA7A5":"n","\u01CC":"nj","\u24DE":"o","\uFF4F":"o","\u00F2":"o","\u00F3":"o","\u00F4":"o","\u1ED3":"o","\u1ED1":"o","\u1ED7":"o","\u1ED5":"o","\u00F5":"o","\u1E4D":"o","\u022D":"o","\u1E4F":"o","\u014D":"o","\u1E51":"o","\u1E53":"o","\u014F":"o","\u022F":"o","\u0231":"o","\u00F6":"o","\u022B":"o","\u1ECF":"o","\u0151":"o","\u01D2":"o","\u020D":"o","\u020F":"o","\u01A1":"o","\u1EDD":"o","\u1EDB":"o","\u1EE1":"o","\u1EDF":"o","\u1EE3":"o","\u1ECD":"o","\u1ED9":"o","\u01EB":"o","\u01ED":"o","\u00F8":"o","\u01FF":"o","\u0254":"o","\uA74B":"o","\uA74D":"o","\u0275":"o","\u01A3":"oi","\u0223":"ou","\uA74F":"oo","\u24DF":"p","\uFF50":"p","\u1E55":"p","\u1E57":"p","\u01A5":"p","\u1D7D":"p","\uA751":"p","\uA753":"p","\uA755":"p","\u24E0":"q","\uFF51":"q","\u024B":"q","\uA757":"q","\uA759":"q","\u24E1":"r","\uFF52":"r","\u0155":"r","\u1E59":"r","\u0159":"r","\u0211":"r","\u0213":"r","\u1E5B":"r","\u1E5D":"r","\u0157":"r","\u1E5F":"r","\u024D":"r","\u027D":"r","\uA75B":"r","\uA7A7":"r","\uA783":"r","\u24E2":"s","\uFF53":"s","\u00DF":"s","\u015B":"s","\u1E65":"s","\u015D":"s","\u1E61":"s","\u0161":"s","\u1E67":"s","\u1E63":"s","\u1E69":"s","\u0219":"s","\u015F":"s","\u023F":"s","\uA7A9":"s","\uA785":"s","\u1E9B":"s","\u24E3":"t","\uFF54":"t","\u1E6B":"t","\u1E97":"t","\u0165":"t","\u1E6D":"t","\u021B":"t","\u0163":"t","\u1E71":"t","\u1E6F":"t","\u0167":"t","\u01AD":"t","\u0288":"t","\u2C66":"t","\uA787":"t","\uA729":"tz","\u24E4":"u","\uFF55":"u","\u00F9":"u","\u00FA":"u","\u00FB":"u","\u0169":"u","\u1E79":"u","\u016B":"u","\u1E7B":"u","\u016D":"u","\u00FC":"u","\u01DC":"u","\u01D8":"u","\u01D6":"u","\u01DA":"u","\u1EE7":"u","\u016F":"u","\u0171":"u","\u01D4":"u","\u0215":"u","\u0217":"u","\u01B0":"u","\u1EEB":"u","\u1EE9":"u","\u1EEF":"u","\u1EED":"u","\u1EF1":"u","\u1EE5":"u","\u1E73":"u","\u0173":"u","\u1E77":"u","\u1E75":"u","\u0289":"u","\u24E5":"v","\uFF56":"v","\u1E7D":"v","\u1E7F":"v","\u028B":"v","\uA75F":"v","\u028C":"v","\uA761":"vy","\u24E6":"w","\uFF57":"w","\u1E81":"w","\u1E83":"w","\u0175":"w","\u1E87":"w","\u1E85":"w","\u1E98":"w","\u1E89":"w","\u2C73":"w","\u24E7":"x","\uFF58":"x","\u1E8B":"x","\u1E8D":"x","\u24E8":"y","\uFF59":"y","\u1EF3":"y","\u00FD":"y","\u0177":"y","\u1EF9":"y","\u0233":"y","\u1E8F":"y","\u00FF":"y","\u1EF7":"y","\u1E99":"y","\u1EF5":"y","\u01B4":"y","\u024F":"y","\u1EFF":"y","\u24E9":"z","\uFF5A":"z","\u017A":"z","\u1E91":"z","\u017C":"z","\u017E":"z","\u1E93":"z","\u1E95":"z","\u01B6":"z","\u0225":"z","\u0240":"z","\u2C6C":"z","\uA763":"z"};
$document = $(document);
nextUid=(function() { var counter=1; return function() { return counter++; }; }());
function stripDiacritics(str) {
var ret, i, l, c;
if (!str || str.length < 1) return str;
ret = "";
for (i = 0, l = str.length; i < l; i++) {
c = str.charAt(i);
ret += DIACRITICS[c] || c;
}
return ret;
}
function indexOf(value, array) {
var i = 0, l = array.length;
for (; i < l; i = i + 1) {
if (equal(value, array[i])) return i;
}
return -1;
}
function measureScrollbar () {
var $template = $( MEASURE_SCROLLBAR_TEMPLATE );
$template.appendTo('body');
var dim = {
width: $template.width() - $template[0].clientWidth,
height: $template.height() - $template[0].clientHeight
};
$template.remove();
return dim;
}
/**
* Compares equality of a and b
* @param a
* @param b
*/
function equal(a, b) {
if (a === b) return true;
if (a === undefined || b === undefined) return false;
if (a === null || b === null) return false;
// Check whether 'a' or 'b' is a string (primitive or object).
// The concatenation of an empty string (+'') converts its argument to a string's primitive.
if (a.constructor === String) return a+'' === b+''; // a+'' - in case 'a' is a String object
if (b.constructor === String) return b+'' === a+''; // b+'' - in case 'b' is a String object
return false;
}
/**
* Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
* strings
* @param string
* @param separator
*/
function splitVal(string, separator) {
var val, i, l;
if (string === null || string.length < 1) return [];
val = string.split(separator);
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
return val;
}
function getSideBorderPadding(element) {
return element.outerWidth(false) - element.width();
}
function installKeyUpChangeEvent(element) {
var key="keyup-change-value";
element.on("keydown", function () {
if ($.data(element, key) === undefined) {
$.data(element, key, element.val());
}
});
element.on("keyup", function () {
var val= $.data(element, key);
if (val !== undefined && element.val() !== val) {
$.removeData(element, key);
element.trigger("keyup-change");
}
});
}
$document.on("mousemove", function (e) {
lastMousePosition.x = e.pageX;
lastMousePosition.y = e.pageY;
});
/**
* filters mouse events so an event is fired only if the mouse moved.
*
* filters out mouse events that occur when mouse is stationary but
* the elements under the pointer are scrolled.
*/
function installFilteredMouseMove(element) {
element.on("mousemove", function (e) {
var lastpos = lastMousePosition;
if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) {
$(e.target).trigger("mousemove-filtered", e);
}
});
}
/**
* Debounces a function. Returns a function that calls the original fn function only if no invocations have been made
* within the last quietMillis milliseconds.
*
* @param quietMillis number of milliseconds to wait before invoking fn
* @param fn function to be debounced
* @param ctx object to be used as this reference within fn
* @return debounced version of fn
*/
function debounce(quietMillis, fn, ctx) {
ctx = ctx || undefined;
var timeout;
return function () {
var args = arguments;
window.clearTimeout(timeout);
timeout = window.setTimeout(function() {
fn.apply(ctx, args);
}, quietMillis);
};
}
/**
* A simple implementation of a thunk
* @param formula function used to lazily initialize the thunk
* @return {Function}
*/
function thunk(formula) {
var evaluated = false,
value;
return function() {
if (evaluated === false) { value = formula(); evaluated = true; }
return value;
};
};
function installDebouncedScroll(threshold, element) {
var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);});
element.on("scroll", function (e) {
if (indexOf(e.target, element.get()) >= 0) notify(e);
});
}
function focus($el) {
if ($el[0] === document.activeElement) return;
/* set the focus in a 0 timeout - that way the focus is set after the processing
of the current event has finished - which seems like the only reliable way
to set focus */
window.setTimeout(function() {
var el=$el[0], pos=$el.val().length, range;
$el.focus();
/* make sure el received focus so we do not error out when trying to manipulate the caret.
sometimes modals or others listeners may steal it after its set */
if ($el.is(":visible") && el === document.activeElement) {
/* after the focus is set move the caret to the end, necessary when we val()
just before setting focus */
if(el.setSelectionRange)
{
el.setSelectionRange(pos, pos);
}
else if (el.createTextRange) {
range = el.createTextRange();
range.collapse(false);
range.select();
}
}
}, 0);
}
function getCursorInfo(el) {
el = $(el)[0];
var offset = 0;
var length = 0;
if ('selectionStart' in el) {
offset = el.selectionStart;
length = el.selectionEnd - offset;
} else if ('selection' in document) {
el.focus();
var sel = document.selection.createRange();
length = document.selection.createRange().text.length;
sel.moveStart('character', -el.value.length);
offset = sel.text.length - length;
}
return { offset: offset, length: length };
}
function killEvent(event) {
event.preventDefault();
event.stopPropagation();
}
function killEventImmediately(event) {
event.preventDefault();
event.stopImmediatePropagation();
}
function measureTextWidth(e) {
if (!sizer){
var style = e[0].currentStyle || window.getComputedStyle(e[0], null);
sizer = $(document.createElement("div")).css({
position: "absolute",
left: "-10000px",
top: "-10000px",
display: "none",
fontSize: style.fontSize,
fontFamily: style.fontFamily,
fontStyle: style.fontStyle,
fontWeight: style.fontWeight,
letterSpacing: style.letterSpacing,
textTransform: style.textTransform,
whiteSpace: "nowrap"
});
sizer.attr("class","select2-sizer");
$("body").append(sizer);
}
sizer.text(e.val());
return sizer.width();
}
function syncCssClasses(dest, src, adapter) {
var classes, replacements = [], adapted;
classes = dest.attr("class");
if (classes) {
classes = '' + classes; // for IE which returns object
$(classes.split(" ")).each2(function() {
if (this.indexOf("select2-") === 0) {
replacements.push(this);
}
});
}
classes = src.attr("class");
if (classes) {
classes = '' + classes; // for IE which returns object
$(classes.split(" ")).each2(function() {
if (this.indexOf("select2-") !== 0) {
adapted = adapter(this);
if (adapted) {
replacements.push(adapted);
}
}
});
}
dest.attr("class", replacements.join(" "));
}
function markMatch(text, term, markup, escapeMarkup) {
var match=stripDiacritics(text.toUpperCase()).indexOf(stripDiacritics(term.toUpperCase())),
tl=term.length;
if (match<0) {
markup.push(escapeMarkup(text));
return;
}
markup.push(escapeMarkup(text.substring(0, match)));
markup.push("");
markup.push(escapeMarkup(text.substring(match, match + tl)));
markup.push("");
markup.push(escapeMarkup(text.substring(match + tl, text.length)));
}
function defaultEscapeMarkup(markup) {
var replace_map = {
'\\': '\',
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
"/": '/'
};
return String(markup).replace(/[&<>"'\/\\]/g, function (match) {
return replace_map[match];
});
}
/**
* Produces an ajax-based query function
*
* @param options object containing configuration paramters
* @param options.params parameter map for the transport ajax call, can contain such options as cache, jsonpCallback, etc. see $.ajax
* @param options.transport function that will be used to execute the ajax request. must be compatible with parameters supported by $.ajax
* @param options.url url for the data
* @param options.data a function(searchTerm, pageNumber, context) that should return an object containing query string parameters for the above url.
* @param options.dataType request data type: ajax, jsonp, other datatatypes supported by jQuery's $.ajax function or the transport function if specified
* @param options.quietMillis (optional) milliseconds to wait before making the ajaxRequest, helps debounce the ajax function if invoked too often
* @param options.results a function(remoteData, pageNumber) that converts data returned form the remote request to the format expected by Select2.
* The expected format is an object containing the following keys:
* results array of objects that will be used as choices
* more (optional) boolean indicating whether there are more results available
* Example: {results:[{id:1, text:'Red'},{id:2, text:'Blue'}], more:true}
*/
function ajax(options) {
var timeout, // current scheduled but not yet executed request
handler = null,
quietMillis = options.quietMillis || 100,
ajaxUrl = options.url,
self = this;
return function (query) {
window.clearTimeout(timeout);
timeout = window.setTimeout(function () {
var data = options.data, // ajax data function
url = ajaxUrl, // ajax url string or function
transport = options.transport || $.fn.select2.ajaxDefaults.transport,
// deprecated - to be removed in 4.0 - use params instead
deprecated = {
type: options.type || 'GET', // set type of request (GET or POST)
cache: options.cache || false,
jsonpCallback: options.jsonpCallback||undefined,
dataType: options.dataType||"json"
},
params = $.extend({}, $.fn.select2.ajaxDefaults.params, deprecated);
data = data ? data.call(self, query.term, query.page, query.context) : null;
url = (typeof url === 'function') ? url.call(self, query.term, query.page, query.context) : url;
if (handler) { handler.abort(); }
if (options.params) {
if ($.isFunction(options.params)) {
$.extend(params, options.params.call(self));
} else {
$.extend(params, options.params);
}
}
$.extend(params, {
url: url,
dataType: options.dataType,
data: data,
success: function (data) {
// TODO - replace query.page with query so users have access to term, page, etc.
var results = options.results(data, query.page);
query.callback(results);
}
});
handler = transport.call(self, params);
}, quietMillis);
};
}
/**
* Produces a query function that works with a local array
*
* @param options object containing configuration parameters. The options parameter can either be an array or an
* object.
*
* If the array form is used it is assumed that it contains objects with 'id' and 'text' keys.
*
* If the object form is used ti is assumed that it contains 'data' and 'text' keys. The 'data' key should contain
* an array of objects that will be used as choices. These objects must contain at least an 'id' key. The 'text'
* key can either be a String in which case it is expected that each element in the 'data' array has a key with the
* value of 'text' which will be used to match choices. Alternatively, text can be a function(item) that can extract
* the text.
*/
function local(options) {
var data = options, // data elements
dataText,
tmp,
text = function (item) { return ""+item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
if ($.isArray(data)) {
tmp = data;
data = { results: tmp };
}
if ($.isFunction(data) === false) {
tmp = data;
data = function() { return tmp; };
}
var dataItem = data();
if (dataItem.text) {
text = dataItem.text;
// if text is not a function we assume it to be a key name
if (!$.isFunction(text)) {
dataText = dataItem.text; // we need to store this in a separate variable because in the next step data gets reset and data.text is no longer available
text = function (item) { return item[dataText]; };
}
}
return function (query) {
var t = query.term, filtered = { results: [] }, process;
if (t === "") {
query.callback(data());
return;
}
process = function(datum, collection) {
var group, attr;
datum = datum[0];
if (datum.children) {
group = {};
for (attr in datum) {
if (datum.hasOwnProperty(attr)) group[attr]=datum[attr];
}
group.children=[];
$(datum.children).each2(function(i, childDatum) { process(childDatum, group.children); });
if (group.children.length || query.matcher(t, text(group), datum)) {
collection.push(group);
}
} else {
if (query.matcher(t, text(datum), datum)) {
collection.push(datum);
}
}
};
$(data().results).each2(function(i, datum) { process(datum, filtered.results); });
query.callback(filtered);
};
}
// TODO javadoc
function tags(data) {
var isFunc = $.isFunction(data);
return function (query) {
var t = query.term, filtered = {results: []};
$(isFunc ? data() : data).each(function () {
var isObject = this.text !== undefined,
text = isObject ? this.text : this;
if (t === "" || query.matcher(t, text)) {
filtered.results.push(isObject ? this : {id: this, text: this});
}
});
query.callback(filtered);
};
}
/**
* Checks if the formatter function should be used.
*
* Throws an error if it is not a function. Returns true if it should be used,
* false if no formatting should be performed.
*
* @param formatter
*/
function checkFormatter(formatter, formatterName) {
if ($.isFunction(formatter)) return true;
if (!formatter) return false;
throw new Error(formatterName +" must be a function or a falsy value");
}
function evaluate(val) {
return $.isFunction(val) ? val() : val;
}
function countResults(results) {
var count = 0;
$.each(results, function(i, item) {
if (item.children) {
count += countResults(item.children);
} else {
count++;
}
});
return count;
}
/**
* Default tokenizer. This function uses breaks the input on substring match of any string from the
* opts.tokenSeparators array and uses opts.createSearchChoice to create the choice object. Both of those
* two options have to be defined in order for the tokenizer to work.
*
* @param input text user has typed so far or pasted into the search field
* @param selection currently selected choices
* @param selectCallback function(choice) callback tho add the choice to selection
* @param opts select2's opts
* @return undefined/null to leave the current input unchanged, or a string to change the input to the returned value
*/
function defaultTokenizer(input, selection, selectCallback, opts) {
var original = input, // store the original so we can compare and know if we need to tell the search to update its text
dupe = false, // check for whether a token we extracted represents a duplicate selected choice
token, // token
index, // position at which the separator was found
i, l, // looping variables
separator; // the matched separator
if (!opts.createSearchChoice || !opts.tokenSeparators || opts.tokenSeparators.length < 1) return undefined;
while (true) {
index = -1;
for (i = 0, l = opts.tokenSeparators.length; i < l; i++) {
separator = opts.tokenSeparators[i];
index = input.indexOf(separator);
if (index >= 0) break;
}
if (index < 0) break; // did not find any token separator in the input string, bail
token = input.substring(0, index);
input = input.substring(index + separator.length);
if (token.length > 0) {
token = opts.createSearchChoice.call(this, token, selection);
if (token !== undefined && token !== null && opts.id(token) !== undefined && opts.id(token) !== null) {
dupe = false;
for (i = 0, l = selection.length; i < l; i++) {
if (equal(opts.id(token), opts.id(selection[i]))) {
dupe = true; break;
}
}
if (!dupe) selectCallback(token);
}
}
}
if (original!==input) return input;
}
/**
* Creates a new class
*
* @param superClass
* @param methods
*/
function clazz(SuperClass, methods) {
var constructor = function () {};
constructor.prototype = new SuperClass;
constructor.prototype.constructor = constructor;
constructor.prototype.parent = SuperClass.prototype;
constructor.prototype = $.extend(constructor.prototype, methods);
return constructor;
}
AbstractSelect2 = clazz(Object, {
// abstract
bind: function (func) {
var self = this;
return function () {
func.apply(self, arguments);
};
},
// abstract
init: function (opts) {
var results, search, resultsSelector = ".select2-results";
// prepare options
this.opts = opts = this.prepareOpts(opts);
this.id=opts.id;
// destroy if called on an existing component
if (opts.element.data("select2") !== undefined &&
opts.element.data("select2") !== null) {
opts.element.data("select2").destroy();
}
this.container = this.createContainer();
this.containerId="s2id_"+(opts.element.attr("id") || "autogen"+nextUid());
this.containerSelector="#"+this.containerId.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
this.container.attr("id", this.containerId);
// cache the body so future lookups are cheap
this.body = thunk(function() { return opts.element.closest("body"); });
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
this.container.attr("style", opts.element.attr("style"));
this.container.css(evaluate(opts.containerCss));
this.container.addClass(evaluate(opts.containerCssClass));
this.elementTabIndex = this.opts.element.attr("tabindex");
// swap container for the element
this.opts.element
.data("select2", this)
.attr("tabindex", "-1")
.before(this.container)
.on("click.select2", killEvent); // do not leak click events
this.container.data("select2", this);
this.dropdown = this.container.find(".select2-drop");
syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
this.dropdown.addClass(evaluate(opts.dropdownCssClass));
this.dropdown.data("select2", this);
this.dropdown.on("click", killEvent);
this.results = results = this.container.find(resultsSelector);
this.search = search = this.container.find("input.select2-input");
this.queryCount = 0;
this.resultsPage = 0;
this.context = null;
// initialize the container
this.initContainer();
this.container.on("click", killEvent);
installFilteredMouseMove(this.results);
this.dropdown.on("mousemove-filtered touchstart touchmove touchend", resultsSelector, this.bind(this.highlightUnderEvent));
installDebouncedScroll(80, this.results);
this.dropdown.on("scroll-debounced", resultsSelector, this.bind(this.loadMoreIfNeeded));
// do not propagate change event from the search field out of the component
$(this.container).on("change", ".select2-input", function(e) {e.stopPropagation();});
$(this.dropdown).on("change", ".select2-input", function(e) {e.stopPropagation();});
// if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel
if ($.fn.mousewheel) {
results.mousewheel(function (e, delta, deltaX, deltaY) {
var top = results.scrollTop();
if (deltaY > 0 && top - deltaY <= 0) {
results.scrollTop(0);
killEvent(e);
} else if (deltaY < 0 && results.get(0).scrollHeight - results.scrollTop() + deltaY <= results.height()) {
results.scrollTop(results.get(0).scrollHeight - results.height());
killEvent(e);
}
});
}
installKeyUpChangeEvent(search);
search.on("keyup-change input paste", this.bind(this.updateResults));
search.on("focus", function () { search.addClass("select2-focused"); });
search.on("blur", function () { search.removeClass("select2-focused");});
this.dropdown.on("mouseup", resultsSelector, this.bind(function (e) {
if ($(e.target).closest(".select2-result-selectable").length > 0) {
this.highlightUnderEvent(e);
this.selectHighlighted(e);
}
}));
// trap all mouse events from leaving the dropdown. sometimes there may be a modal that is listening
// for mouse events outside of itself so it can close itself. since the dropdown is now outside the select2's
// dom it will trigger the popup close, which is not what we want
this.dropdown.on("click mouseup mousedown", function (e) { e.stopPropagation(); });
if ($.isFunction(this.opts.initSelection)) {
// initialize selection based on the current value of the source element
this.initSelection();
// if the user has provided a function that can set selection based on the value of the source element
// we monitor the change event on the element and trigger it, allowing for two way synchronization
this.monitorSource();
}
if (opts.maximumInputLength !== null) {
this.search.attr("maxlength", opts.maximumInputLength);
}
var disabled = opts.element.prop("disabled");
if (disabled === undefined) disabled = false;
this.enable(!disabled);
var readonly = opts.element.prop("readonly");
if (readonly === undefined) readonly = false;
this.readonly(readonly);
// Calculate size of scrollbar
scrollBarDimensions = scrollBarDimensions || measureScrollbar();
this.autofocus = opts.element.prop("autofocus");
opts.element.prop("autofocus", false);
if (this.autofocus) this.focus();
this.nextSearchTerm = undefined;
},
// abstract
destroy: function () {
var element=this.opts.element, select2 = element.data("select2");
this.close();
if (this.propertyObserver) { delete this.propertyObserver; this.propertyObserver = null; }
if (select2 !== undefined) {
select2.container.remove();
select2.dropdown.remove();
element
.removeClass("select2-offscreen")
.removeData("select2")
.off(".select2")
.prop("autofocus", this.autofocus || false);
if (this.elementTabIndex) {
element.attr({tabindex: this.elementTabIndex});
} else {
element.removeAttr("tabindex");
}
element.show();
}
},
// abstract
optionToData: function(element) {
if (element.is("option")) {
return {
id:element.prop("value"),
text:element.text(),
element: element.get(),
css: element.attr("class"),
disabled: element.prop("disabled"),
locked: equal(element.attr("locked"), "locked") || equal(element.data("locked"), true)
};
} else if (element.is("optgroup")) {
return {
text:element.attr("label"),
children:[],
element: element.get(),
css: element.attr("class")
};
}
},
// abstract
prepareOpts: function (opts) {
var element, select, idKey, ajaxUrl, self = this;
element = opts.element;
if (element.get(0).tagName.toLowerCase() === "select") {
this.select = select = opts.element;
}
if (select) {
// these options are not allowed when attached to a select because they are picked up off the element itself
$.each(["id", "multiple", "ajax", "query", "createSearchChoice", "initSelection", "data", "tags"], function () {
if (this in opts) {
throw new Error("Option '" + this + "' is not allowed for Select2 when attached to a