!function( $ ){
"use strict";
var Translatable = function( element, options ) {
this.$element = $(element);
this.options = $.extend({}, $.fn.translatable.defaults, options);
if (!this.$element.data('translateTag'))
return;
this.shown = false;
this.populated = false;
this.fetch('ajax.php/i18n/langs').then($.proxy(function(json) {
this.langs = json;
if (Object.keys(this.langs).length) this.decorate();
}, this));
},
// Class-static variables
urlcache = {};
Translatable.prototype = {
constructor: Translatable,
fetch: function( url, data, callback ) {
if ( !urlcache[ url ] ) {
urlcache[ url ] = $.Deferred(function( defer ) {
$.ajax( url, { data: data, dataType: 'json', global: false } )
.then( defer.resolve, defer.reject );
}).promise();
}
return urlcache[ url ].done( callback );
},
decorate: function() {
this.$translations = $('
');
this.$status = $(' '+__('Loading')+' ...')
.appendTo(this.$translations);
this.$footer = $('');
this.$select = $('');
this.$menu = $(this.options.menu).appendTo('body');
this.$element.wrap('');
this.$container = this.$element.parent();
if (this.$element.width() > 100)
this.$element.width(this.$element.width()-35);
this.$container.wrap('');
this.$button = $(this.options.button).appendTo(this.$container);
this.$menu.append($('')
.on('click', $.proxy(this.hide, this)));
if (this.$element.is('textarea')) {
this.$container.addClass('textarea');
}
this.$menu.append(this.$translations).append(this.$footer);
this.$button.on('click', $.proxy(this.toggle, this));
this.$element
.addClass('translatable')
.focus($.proxy(function() { this.addClass('focus'); }, this.$container))
.blur($.proxy(function() { this.removeClass('focus'); }, this.$container));
getConfig().then($.proxy(function(c) {
this.attr({'spellcheck': 'true', 'lang': c.primary_language})
$('')
.addClass('flag-' + c.primary_lang_flag)
.insertAfter(this);
}, this.$element));
},
buildAdd: function() {
var self=this;
this.$footer
.append($('')
.append(this.$select)
.append($('')
.on('click', $.proxy(this.define, this))
)
);
this.fetch('ajax.php/i18n/langs').then(function(langs) {
$.each(langs, function(k, v) {
self.$select.append($('