{"size": 2511, "ext": "js", "max_stars_repo_path": "jquery.xtr.js", "max_stars_repo_name": "melbon/jquery.xtr", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "jquery.xtr.js", "max_issues_repo_name": "melbon/jquery.xtr", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "jquery.xtr.js", "max_forks_repo_name": "melbon/jquery.xtr", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.2134831461, "max_line_length": 101, "alphanum_fraction": 0.4743130227}
/*!
 * July 2017
 * xtr 1.0.0
 * @author Mario Vidov
 * @url http://vidov.it
 * @twitter MarioVidov
 * MIT license
*/

(function($) {
    var pluginName = 'xtr';

    var settings = {
        default: 'en',
        lang: 'en',
        regExp: {
            '': /\s/g,
            '_': /[&<>"'`\/=]/g
        }
    };

    var config = {
        defaultClass: 'data-xtr-default',
        langClass: 'data-xtr-lang',
        langKey  : 'data-xtr-key',
        langOrig : 'data-xtr-original'
    };

    function Plugin(element, options) {
        options = options || {};
        this.$element = $(element);
        this.options  = $.extend(true, {}, settings, options);
        this.selector = '[xtr]';
        this.regExp = this.options.regExp;
        this.default = this.options.default;
        this.t = this.options.t;
        this.l = this.options.lang;

        this.$element.attr(config.langClass, this.l)
                 .attr(config.defaultClass, this.default);
        this.init();
    }

    Plugin.prototype.lang = function(l) {
        if (l && l === 'reset') {
            this.l = this.default;
        } else if (l) {
            this.l = l;
        }
        this.init();
    };

    Plugin.prototype.set = function(index) {
        var $el = $('[' + config.langKey + '="' + index + '"]');
        var original = $el && $el.attr(config.langOrig) || index;
        return (this.t && this.t[index] && this.t[index][this.l]) ? this.t[index][this.l] : original;
    };

    Plugin.prototype.init = function() {
        var self = this;
        this.$element.attr(config.langClass, this.l);
        $(self.selector).add('[' + config.langKey + ']').each(function () {
            var $this = $(this);
            var key = $this.attr(config.langKey);
            var original = $this.attr(config.langOrig);
            var text = $this.text();
            if (!key) {
                key = text;
                for (var i in self.regExp) {
                    key = key.replace(self.regExp[i], i);
                }
                $this.attr(config.langKey, key);
            }
            if (!original) {
                $this.attr(config.langOrig, text);
            }
            $this.html(self.set(key));
        });
    };

    $.fn[pluginName] = function (options) {
        return this.each(function () {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
            }
        });
    };
})(jQuery);
