/*jslint vars: true, browser: true, white: true */
/*global jQuery, IQVOC */
IQVOC.onebox = (function($) {
"use strict";
function OneBox(container) {
this.container = container.jquery ? container : $(container);
this.form = this.container.find('form');
this.input = $(".onebox-input", this.form); // TODO: document
this.results = $('
');
this.indicator = $('').
appendTo(this.container).hide();
this.form.on("submit", $.proxy(this, "onSubmit"));
this.input.on("keyup", debounce($.proxy(this, "onInput"), 300));
}
OneBox.prototype.reset = function() {
this.container.children().not(this.form).not(this.indicator).remove();
};
OneBox.prototype.renderResults = function(html) {
var doc = $("").append(html);
var items = $(".concept-items > li", doc);
var pagination = $(".pagination", doc);
this.reset();
this.results.empty().append(items).appendTo(this.container);
pagination.appendTo(this.container);
};
OneBox.prototype.onSubmit = function(ev) {
ev.preventDefault(); // handled by #onInput
};
OneBox.prototype.onInput = function(ev) {
var val = this.input.val();
this.reset();
if(val.length > 0 && val !== this.initialValue) {
this.getConcepts();
}
};
OneBox.prototype.getConcepts = function() {
this.indicator.show();
$.ajax({
type: this.form.attr("method"),
url: this.form.attr("action"),
data: this.form.serialize(),
success: $.proxy(this, "onResults")
});
};
OneBox.prototype.onResults = function(html, status, xhr) {
// TODO request resource with layout=0
// disable scripts (adapted from jQuery#load)
var rscript = /