|
$(document).ready(function () { |
|
var selectizeOptions = { |
|
maxItems: null, |
|
delimiter: ',', |
|
preload: true, |
|
valueField: 'id', |
|
labelField: 'tag_name', |
|
searchField: 'tag_name', |
|
options: [], |
|
create: function (input) { |
|
return { |
|
id: '_NEW:' + input, |
|
tag_name: input, |
|
} |
|
}, |
|
load: function (query, callback) { |
|
var data = {}; |
|
data.query = query; |
|
|
|
// Controllo se tra gli attributi c'è scope (o se non è valorizzato a 'all') |
|
if (($(this)[0].$input.attr('scope') != 'all') && (typeof $(this)[0].$input.attr('scope') !== "undefined") && ($(this)[0].$input.attr('scope'))) { |
|
data.scope = $(this)[0].$input.attr('scope'); |
|
} |
|
|
|
$.ajax({ |
|
url: '/Tags/getDataAjax.json', |
|
type: "GET", |
|
dataType: "json", |
|
beforeSend: function (xhr) { |
|
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
|
}, |
|
data: data, |
|
error: function (e) { |
|
callback(); |
|
}, |
|
success: function (data) { |
|
callback(data.data); |
|
} |
|
}); |
|
}, |
|
render: { |
|
option_create: function (data, escape) { |
|
var addString = 'Aggiungi'; |
|
return '<div class="create">' + addString + ' <strong>' + escape(data.input) + '</strong>…</div>'; |
|
} |
|
}, |
|
plugins: ['restore_on_backspace', 'remove_button'] |
|
|
|
}; |
|
|
|
var notCreate = { |
|
render: { |
|
option_create: function (data, escape) { |
|
return ''; |
|
} |
|
}, |
|
create: function (input) { |
|
return false; |
|
}, |
|
}; |
|
|
|
/** |
|
* Aggiungo le opzioni specifiche al selectize |
|
* elem: elemento a cui aggiungere il selectize |
|
* extendedOptions: opzioni specifiche da estendere a quelle generiche |
|
*/ |
|
function initSelectize(elem, extendedOptions) { |
|
var specificOptions = $.extend(true, {}, selectizeOptions); |
|
if ((typeof extendedOptions !== "undefined") && (extendedOptions)) { |
|
specificOptions = $.extend({}, specificOptions, extendedOptions); |
|
} |
|
if ((typeof elem.attr('plugin') !== "undefined") && (elem.attr('plugin'))) { |
|
specificOptions = $.extend({}, specificOptions, { |
|
plugins: ['restore_on_backspace', 'remove_button', elem.attr('plugin')] |
|
}); |
|
} |
|
$(elem).selectize(specificOptions); |
|
} |
|
|
|
initSelectize($('.selectize[scope=1]'), notCreate); |
|
initSelectize($('.selectize[scope=2]'), notCreate); |
|
initSelectize($('.selectize[scope=3]'), notCreate); |
|
initSelectize($('.selectize[scope=4]'), null); |
|
initSelectize($('.selectize[scope=5]'), null); |
|
initSelectize($('.selectize[scope=all]'), null); |
|
}); |