$.fn.extend({
chosen: function (data, options) {
var createdInstances = [];
$( this ).each( function (input_field) {
if (!($( this )).hasClass( "chzn-done" )) {
createdInstances.push( new Chosen( this , data, options));
}
});
return createdInstances;
}
});
* This source code was highlighted with Source Code Highlighter .
$.fn.extend({
chosen: function (data, options) {
return $( this ).each( function (input_field) {
if (!($( this )).hasClass( "chzn-done" )) {
return $( this ).data( 'chosenInstance' , new Chosen( this , data, options));
}
});
}
});
* This source code was highlighted with Source Code Highlighter .
var createdChosenInstance = $ ( '#bears_multiple' ) .chosen (). data ( 'chosenInstance' );
* This source code was highlighted with Source Code Highlighter .
$.fn.extend({
chosenClass: function () {
return Chosen;
}
});
* This source code was highlighted with Source Code Highlighter .
( function ($, ChosenClass) {
var dynamicItemInstance;
function DynamicItem(chosenInstance) {
$(( this .chosen = chosenInstance).search_results).parent().prepend(
this .elContainer = $( document .createElement( 'div' )));
this .elContainer.addClass( 'chzn-results-additemcontainer' );
this .elContainer.append( this .elButton = $( document .createElement( 'button' )));
this .elButton.click($.proxy( this .addNewItem, this ));
}
DynamicItem.prototype = {
constructor: DynamicItem,
show: function () {
var data = this .chosen.results_data,
text = this .text,
isNotSelected = true ;
if ( this .chosen.choices) {
( this .chosen.search_choices.find( "li.search-choice" ).each( function (el) {
var itemIdx = this .id.substr( this .id.lastIndexOf( "_" ) + 1),
item = data[itemIdx];
if (item.value === text) {
isNotSelected = !isNotSelected;
return false ;
}
}));
}
this .elContainer[isNotSelected ? 'show' : 'hide' ]();
},
update: function (terms) {
if (( this .text = terms).length) {
this .elButton.text( 'Add new item "' + this .text + '"' );
this .show();
} else {
this .elContainer.hide();
}
},
addNewItem: function (terms) {
this .chosen.form_field.options.add( new Option( this .text, this .text));
this .chosen.form_field_jq.trigger( 'liszt:updated' );
this .chosen.result_highlight = this .chosen.search_results.children().last();
return this .chosen.result_select();
}
};
$.extend(ChosenClass.prototype, {
no_results: ( function (fnSuper) {
return function (terms) {
(dynamicItemInstance || (dynamicItemInstance = new DynamicItem( this ))).update(terms);
return fnSuper.call( this , terms);
};
})(ChosenClass.prototype.no_results),
results_hide: ( function (fnSuper) {
return function () {
dynamicItemInstance && dynamicItemInstance.elContainer.hide();
return fnSuper.call( this );
};
})(ChosenClass.prototype.results_hide),
winnow_results_set_highlight: ( function (fnSuper) {
return function () {
dynamicItemInstance && dynamicItemInstance.elContainer.hide();
return fnSuper.apply( this , arguments);
};
})(ChosenClass.prototype.winnow_results_set_highlight)
});
})(jQuery, jQuery.fn.chosenClass());
* This source code was highlighted with Source Code Highlighter .
( function (Chosen) {
var dynamicItemInstance;
function DynamicItem(chosenInstance) {
( this .chosen = chosenInstance).search_results.up().insert({
top: this .elContainer = $( document .createElement( 'div' ))
});
this .elContainer.addClassName( 'chzn-results-additemcontainer' );
this .elContainer.insert( this .elButton = $( document .createElement( 'button' )));
Event.observe( this .elButton, 'click' , this .addNewItem.bind( this ));
}
DynamicItem.prototype = {
constructor: DynamicItem,
show: function () {
var data = this .chosen.results_data,
text = this .text,
isNotSelected = true ;
if ( this .chosen.choices) {
( this .chosen.search_choices.select( "li.search-choice" ).each( function (el) {
var itemIdx = el.id.substr(el.id.lastIndexOf( "_" ) + 1),
item = data[itemIdx];
if (item.value === text) {
isNotSelected = !isNotSelected;
return false ;
}
}));
}
this .elContainer[isNotSelected ? 'show' : 'hide' ]();
},
update: function (terms) {
if (( this .text = terms).length) {
this .elButton.update( 'Add new item "' + this .text + '"' );
this .show();
} else {
this .elContainer.hide();
}
},
addNewItem: function (terms) {
this .chosen.form_field.options.add( new Option( this .text, this .text));
Event.fire( this .chosen.form_field, "liszt:updated" );
this .chosen.result_highlight = this .chosen.search_results.childElements().pop();
return this .chosen.result_select();
}
};
Chosen.prototype.no_results = ( function (fnSuper) {
return function (terms) {
(dynamicItemInstance || (dynamicItemInstance = new DynamicItem( this ))).update(terms);
return fnSuper.call( this , terms);
};
})(Chosen.prototype.no_results);
Chosen.prototype.results_hide = ( function (fnSuper) {
return function () {
dynamicItemInstance && dynamicItemInstance.elContainer.hide();
return fnSuper.call( this );
};
})(Chosen.prototype.results_hide);
Chosen.prototype.winnow_results_set_highlight = ( function (fnSuper) {
return function () {
dynamicItemInstance && dynamicItemInstance.elContainer.hide();
return fnSuper.apply( this , arguments);
};
})(Chosen.prototype.winnow_results_set_highlight);
})(window.Chosen);
new Chosen($( 'bears_multiple' ));
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/124912/
All Articles