<commit_before>/**
 *  Plugin para carregar comboboxes via ajax.
 *  @author: <NAME> (@igorbrites)
 *  @license: MIT Licence
 */

;(function ($, window, undefined) {
	$.isChosen = function(obj) {
		return document.getElementById(obj.id.replace('-', '_') + '_chosen') !== null;
	};

	var
        defaults = {
            url: '',
            args: {},
            selectedValue: false,
            comboLabel: 'Selecione',
            callback: false,
            cache: null
        },

        setFirstOption = function($obj, message) {
	        var isChosen = $.isChosen($obj[0]);
            $obj[0].options.length = 0;

            if (isChosen) {
	            $obj[0].options[0] = new Option('', '');
	            $obj.attr('data-placeholder', message).trigger("chosen:updated");
            } else {
                $obj[0].options[0] = new Option(message, '');
            }
        },

        doPopulate = function(json, parameters, $obj) {
            var opt = $obj[0].options,
                isChosen = $.isChosen($obj[0]),
                node,
                value,
                label,
                option,
                k;

            if (typeof (json) !== 'object') {
                setFirstOption($obj, 'Erro na resposta');
            } else if (json.error === true) {
                setFirstOption($obj, json.errorMessage);
            } else {
                setFirstOption($obj, parameters.comboLabel);

                for (k in json.options) {
                    node = json.options[k];
                    value = node.value;
                    label = node.label;
                    option = new Option(label, value);

                    for (var i in node) {
                        if(i !== 'value' && i !== 'label') {
                            option.setAttribute('data-' + i, node[i]);
                        }
                    }

                    opt[opt.length] = option;
                }

                if (parameters.selectedValue) {
                    $obj[0].value = parameters.selectedValue;
                } else if ($obj.data('value')) {
                    $obj[0].value = $obj.data('value');
                }

                isChosen && $obj.trigger("chosen:updated");

                typeof parameters.callback === 'function' && parameters.callback();
            }
        };

	$.populateCombo = function (obj, params) {
		params = $.extend({}, defaults, params);

        if (params.cache !== null) {
            if (params.cache.object.has(params.cache.key)) {
                doPopulate(params.cache.object.get(params.cache.key), params, $(obj));
                return true;
            }
        }

		var comboOptions = obj.options,
            $obj = $(obj),
			isChosen = $.isChosen($obj[0]);

		$.ajax({
			type: 'post',
			url: params.url,
			dataType: 'json',
			data: params.args,

			beforeSend: function () {
				comboOptions.length = 0;

				if (isChosen) {
					$obj.attr('data-placeholder', 'Carregando...').trigger("chosen:updated");
				} else {
					comboOptions[0] = new Option('Carregando...', '');
				}
			},

			/**
			 * @param {Object} json
			 * @example {
			 *      error: false,
			 *      errorMessage: '',
			 *      options: {
			 *          {label: 'Option 01', value: 1},
			 *          {label: 'Option 02', value: 2},
			 *          {label: 'Option 03', value: 3},
			 *          {label: 'Option 04', value: 4},
			 *          {label: 'Option 05', value: 5},
			 *          {label: 'Option 06', value: 6}
			 *      }
			 *  }
			 */
			success: function (json) {
                params.cache.object.set(params.cache.key, json);
                doPopulate(json, params, $obj);
			},

			error: function (xhr, textStatus, errorThrown) {
				comboOptions.length = 0;

				if (isChosen) {
					$obj.attr('data-placeholder', 'Erro no servidor').trigger("chosen:updated");
				} else {
					comboOptions[0] = new Option('Erro no servidor', '');
				}
			}
		});<commit_msg>Correção do cache<commit_after>/**
 *  Plugin para carregar comboboxes via ajax.
 *  @author: <NAME> (@igorbrites)
 *  @license: MIT Licence
 */

;(function ($, window, undefined) {
	$.isChosen = function(obj) {
		return document.getElementById(obj.id.replace('-', '_') + '_chosen') !== null;
	};

	var
        defaults = {
            url: '',
            args: {},
            selectedValue: false,
            comboLabel: 'Selecione',
            callback: false,
            cache: null
        },

        setFirstOption = function($obj, message) {
	        var isChosen = $.isChosen($obj[0]);
            $obj[0].options.length = 0;

            if (isChosen) {
	            $obj[0].options[0] = new Option('', '');
	            $obj.attr('data-placeholder', message).trigger("chosen:updated");
            } else {
                $obj[0].options[0] = new Option(message, '');
            }
        },

        doPopulate = function(json, parameters, $obj) {
            var opt = $obj[0].options,
                isChosen = $.isChosen($obj[0]),
                node,
                value,
                label,
                option,
                k;

            if (typeof (json) !== 'object') {
                setFirstOption($obj, 'Erro na resposta');
            } else if (json.error === true) {
                setFirstOption($obj, json.errorMessage);
            } else {
                setFirstOption($obj, parameters.comboLabel);

                for (k in json.options) {
                    node = json.options[k];
                    value = node.value;
                    label = node.label;
                    option = new Option(label, value);

                    for (var i in node) {
                        if(i !== 'value' && i !== 'label') {
                            option.setAttribute('data-' + i, node[i]);
                        }
                    }

                    opt[opt.length] = option;
                }

                if (parameters.selectedValue) {
                    $obj[0].value = parameters.selectedValue;
                } else if ($obj.data('value')) {
                    $obj[0].value = $obj.data('value');
                }

                isChosen && $obj.trigger("chosen:updated");

                typeof parameters.callback === 'function' && parameters.callback();
            }
        };

	$.populateCombo = function (obj, params) {
		params = $.extend({}, defaults, params);

        if (params.cache !== null) {
            if (params.cache.object.has(params.cache.key)) {
                doPopulate(params.cache.object.get(params.cache.key), params, $(obj));
                return true;
            }
        }

		var comboOptions = obj.options,
            $obj = $(obj),
			isChosen = $.isChosen($obj[0]);

		$.ajax({
			type: 'post',
			url: params.url,
			dataType: 'json',
			data: params.args,

			beforeSend: function () {
				comboOptions.length = 0;

				if (isChosen) {
					$obj.attr('data-placeholder', 'Carregando...').trigger("chosen:updated");
				} else {
					comboOptions[0] = new Option('Carregando...', '');
				}
			},

			/**
			 * @param {Object} json
			 * @example {
			 *      error: false,
			 *      errorMessage: '',
			 *      options: {
			 *          {label: 'Option 01', value: 1},
			 *          {label: 'Option 02', value: 2},
			 *          {label: 'Option 03', value: 3},
			 *          {label: 'Option 04', value: 4},
			 *          {label: 'Option 05', value: 5},
			 *          {label: 'Option 06', value: 6}
			 *      }
			 *  }
			 */
			success: function (json) {
                params.cache && params.cache.object.set(params.cache.key, json);
                doPopulate(json, params, $obj);
			},

			error: function (xhr, textStatus, errorThrown) {
				comboOptions.length = 0;

				if (isChosen) {
					$obj.attr('data-placeholder', 'Erro no servidor').trigger("chosen:updated");
				} else {
					comboOptions[0] = new Option('Erro no servidor', '');
				}
			}
		});
{'fraction_non_alphanumeric': 0.15522425457278877, 'fraction_numerical': 0.00851916812828865, 'mean_word_length': 1.4884663341645885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '81865', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2248, 'n_words': 479}