(function ($){
if (typeof $.fn.each2 == "undefined") {
$.fn.extend({
each2: function (c){
var j = $([0] ), i = -1, l = _AN_Read_length("length", this);
while (++i < l && (j.context = j[0] = this[i]) && c.call(j[0], i, j) !== false );
return this;
}
}
);
}
}
)(jQuery);
(function ($, undefined){
"use strict";
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 = "
";
$document = $(document);
nextUid = (function (){
var counter = 1;
return function (){
return counter++ ;
}
;
}
());
function indexOf(value, array){
var i = 0, l = _AN_Read_length("length", array);
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;
}
function equal(a, b){
if (a === b) return true ;
if (a === undefined || b === undefined) return false ;
if (a === null || b === null ) return false ;
if (a.constructor === String) return a + '' === b + '';
if (b.constructor === String) return b + '' === a + '';
return false ;
}
function splitVal(string, separator){
var val, i, l;
if (string === null || _AN_Read_length('length', string) < 1) return [] ;
val = string.split(separator);
for (i = 0, l = _AN_Read_length('length', val); 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;
}
);
function installFilteredMouseMove(element){
element.on("mousemove", function (e){
var lastpos = lastMousePosition;
if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) {
$(_AN_Read_target("target", e)).trigger("mousemove-filtered", e);
}
}
);
}
function debounce(quietMillis, fn, ctx){
ctx = ctx || undefined;
var timeout;
return function (){
var args = arguments;
window.clearTimeout(timeout);
timeout = _AN_Call_settimeout("setTimeout", window, function (){
fn.apply(ctx, args);
}
, quietMillis);
}
;
}
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(_AN_Read_target("target", e), element.get()) >= 0) notify(e);
}
);
}
function focus($el){
if ($el[0] === document.activeElement) return ;
_AN_Call_settimeout("setTimeout", window, function (){
var el = $el[0], pos = _AN_Read_length("length", $el.val()), range;
$el.focus();
if ($el.is(":visible") && el === document.activeElement) {
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 = _AN_Read_length('length', document.selection.createRange().text);
sel.moveStart('character', - _AN_Read_length('length', el.value));
offset = _AN_Read_length('length', sel.text) - 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 = $(_AN_Call_createelement('createElement', document, "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;
$(classes.split(" ")).each2(function (){
if (this.indexOf("select2-") === 0) {
replacements.push(this);
}
}
);
}
classes = src.attr("class");
if (classes) {
classes = '' + classes;
$(classes.split(" ")).each2(function (){
if (this.indexOf("select2-") !== 0) {
adapted = adapter(this);
if (adapted) {
replacements.push(this);
}
}
}
);
}
dest.attr("class", replacements.join(" "));
}
function markMatch(text, term, markup, escapeMarkup){
var match = text.toUpperCase().indexOf(term.toUpperCase()), tl = _AN_Read_length("length", term);
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, _AN_Read_length("length", text))));
}
function defaultEscapeMarkup(markup){
var replace_map = {
'\\': '\',
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
"/": '/'}
;
return _AN_Call_replace('replace', String(markup), /[&<>"'\/\\]/g, function (match){
return replace_map[match];
}
);
}
function ajax(options){
var timeout, requestSequence = 0, handler = null , quietMillis = options.quietMillis || 100, ajaxUrl = _AN_Read_url('url', options), self = this;
return function (query){
window.clearTimeout(timeout);
timeout = _AN_Call_settimeout('setTimeout', window, function (){
requestSequence += 1;
var requestNumber = requestSequence, data = options.data, url = ajaxUrl, transport = options.transport || $.fn.select2.ajaxDefaults.transport, deprecated = {
type: options.type || 'GET',
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){
if (requestNumber < requestSequence) {
return ;
}
var results = options.results(data, query.page);
query.callback(results);
}
}
);
handler = transport.call(self, params);
}
, quietMillis);
}
;
}
function local(options){
var data = options, dataText, tmp, text = function (item){
return "" + item.text;
}
;
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 (!$.isFunction(text)) {
dataText = dataItem.text;
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 (_AN_Read_length("length", group.children) || 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);
}
;
}
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);
}
;
}
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;
}
function defaultTokenizer(input, selection, selectCallback, opts){
var original = input, dupe = false , token, index, i, l, separator;
if (!opts.createSearchChoice || !opts.tokenSeparators || _AN_Read_length("length", opts.tokenSeparators) < 1) return undefined;
while (true ){
index = -1;
for (i = 0, l = _AN_Read_length("length", opts.tokenSeparators); i < l; i++ ){
separator = opts.tokenSeparators[i];
index = input.indexOf(separator);
if (index >= 0) break ;
}
if (index < 0) break ;
token = input.substring(0, index);
input = input.substring(index + _AN_Read_length("length", separator));
if (_AN_Read_length("length", token) > 0) {
token = opts.createSearchChoice(token, selection);
if (token !== undefined && token !== null && opts.id(token) !== undefined && opts.id(token) !== null ) {
dupe = false ;
for (i = 0, l = _AN_Read_length("length", selection); i < l; i++ ){
if (equal(opts.id(token), opts.id(selection[i]))) {
dupe = true ;
break ;
}
}
if (!dupe) selectCallback(token);
}
}
}
if (original !== input) return input;
}
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, {
bind: function (func){
var self = this;
return function (){
func.apply(self, arguments);
}
;
}
,
init: function (opts){
var results, search, resultsSelector = ".select2-results", disabled, readonly;
this.opts = opts = this.prepareOpts(opts);
this.id = opts.id;
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 = "#" + _AN_Call_replace("replace", this.containerId, /([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
this.container.attr("id", this.containerId);
this.body = thunk(function (){
return opts.element.closest("body");
}
);
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
this.container.css(evaluate(opts.containerCss));
this.container.addClass(evaluate(opts.containerCssClass));
this.elementTabIndex = this.opts.element.attr("tabindex");
this.opts.element.data("select2", this).attr("tabindex", "-1").before(this.container);
this.container.data("select2", this);
this.dropdown = this.container.find(".select2-drop");
this.dropdown.addClass(evaluate(opts.dropdownCssClass));
this.dropdown.data("select2", this);
this.results = results = this.container.find(resultsSelector);
_AN_Write_search("search", this, false , search = this.container.find("input.select2-input"));
this.resultsPage = 0;
this.context = null ;
this.initContainer();
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));
$(this.container).on("change", ".select2-input", function (e){
e.stopPropagation();
}
);
$(this.dropdown).on("change", ".select2-input", function (e){
e.stopPropagation();
}
);
if ($.fn.mousewheel) {
results.mousewheel(function (e, delta, deltaX, deltaY){
var top = results.scrollTop(), height;
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 (_AN_Read_length("length", $(_AN_Read_target("target", e)).closest(".select2-result-selectable")) > 0) {
this.highlightUnderEvent(e);
this.selectHighlighted(e);
}
}
));
this.dropdown.on("click mouseup mousedown", function (e){
e.stopPropagation();
}
);
if ($.isFunction(this.opts.initSelection)) {
this.initSelection();
this.monitorSource();
}
if (opts.maximumInputLength !== null ) {
_AN_Read_search("search", this).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);
scrollBarDimensions = scrollBarDimensions || measureScrollbar();
this.autofocus = opts.element.prop("autofocus");
opts.element.prop("autofocus", false );
if (this.autofocus) this.focus();
}
,
destroy: function (){
var element = this.opts.element, select2 = element.data("select2");
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");
}
_AN_Call_show("show", element);
}
}
,
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")}
;
}
}
,
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) {
$.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