Sha256: 83ded678823a8a8d7905feab602e8029a7a1e2be1faed78d705254e2acad9a3a

Contents?: true

Size: 1.78 KB

Versions: 23

Compression:

Stored size: 1.78 KB

Contents

/*jslint vars: true, unparam: true, browser: true */
/*global jQuery, IQVOC */

IQVOC.LanguageSelector = (function($) {

"use strict";

var getSelection, setSelection;

// namespace is used for both localStorage and the event being triggered
var LanguageSelector = function(container, namespace) {
	this.container = container;
	this.namespace = namespace;
	this.langs = getSelection(namespace);
	this.checkboxes = $("input:checkbox", container)
		.live("change", this.onChange);

	$(container).addClass("widget").data("widget", this);
	this.init();
};
$.extend(LanguageSelector.prototype, {
	onChange: function(ev) {
		var el = $(this),
			widget = el.closest(".widget").data("widget");
		if(this.checked) {
			widget.add(el.val());
		} else {
			widget.remove(el.val());
		}
	},
	init: function() {
		if(this.langs === null) {
			this.langs = $.map(this.checkboxes, function(node, i) {
				return $(node).val();
			});
		}
		var self = this;
		this.checkboxes.each(function(i, node) {
			node.checked = $.inArray($(node).val(), self.langs) !== -1;
		});
		this.notify();
	},
	notify: function() {
		$(document).trigger(this.namespace, { langs: this.langs });
	},
	add: function(value) {
		if($.inArray(value, this.langs) === -1) {
			this.langs.push(value);
			setSelection(this.langs, this.namespace, this);
		}
	},
	remove: function(value) {
		var pos = $.inArray(value, this.langs);
		if(pos !== -1) {
			this.langs.splice(pos, 1);
			setSelection(this.langs, this.namespace, this);
		}
	}
});

getSelection = function(namespace) {
	var langs = IQVOC.Storage.getItem(namespace);
	return langs === null ? null : (langs ? langs.split(",") : []);
};

setSelection = function(langs, namespace, context) {
	IQVOC.Storage.setItem(namespace, langs.join(","));
	context.notify();
};

return LanguageSelector;

}(jQuery));

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
iqvoc-3.2.11 public/javascripts/iqvoc/langselect.js
iqvoc-3.2.10 public/javascripts/iqvoc/langselect.js
iqvoc-3.2.9 public/javascripts/iqvoc/langselect.js
iqvoc-3.2.8 public/javascripts/iqvoc/langselect.js
iqvoc-3.5.7 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.5.6 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.5.5 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.5.4 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.2.7 public/javascripts/iqvoc/langselect.js
iqvoc-3.5.3 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.5.2 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.5.1 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.5.0 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.4.0 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.3.4 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.3.3 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.3.2 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.3.1 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.3.0 app/assets/javascripts/iqvoc/langselect.js
iqvoc-3.2.6 public/javascripts/iqvoc/langselect.js