(function (ns) { ns.HeaderForm = function () { this.construct.apply(this, arguments); }; ns.HeaderForm.prototype.construct = function (options) { this.options = $.extend({ id: undefined, queryId: undefined, submitId: undefined, resetId: undefined }, options); if (!this.initElements()) { return; } this.initActions(); }; ns.HeaderForm.prototype.initElements = function () { this.form = $('#' + this.options.id); if (!this.form.length) { return false; } this.query = $('#' + this.options.queryId, this.form); if (!this.query.length) { return false; } this.submit = $('#' + this.options.submitId, this.form); if (!this.submit.length) { return false; } this.reset = $('#' + this.options.resetId, this.form); if (!this.reset.length) { return false; } return true; }; ns.HeaderForm.prototype.initActions = function () { this.query.bind('keyup', _.bind(this.onQueryChange, this)); this.query.bind('change', _.bind(this.onQueryChange, this)); this.onQueryChange(); this.reset.bind('click', _.bind(this.onResetClick, this)); this.submit.bind('click', _.bind(this.onSubmitClick, this)); $(document).bind('click', _.bind(this.onDocumentClick, this)); }; ns.HeaderForm.prototype.onQueryChange = function () { if ('' != this.query.val()) { this.showReset(); } else { this.hideReset(); } }; ns.HeaderForm.prototype.showReset = function () { this.reset.removeAttr('disabled'); this.reset.addClass('show'); }; ns.HeaderForm.prototype.hideReset = function () { this.reset.attr('disabled', 'didabled'); this.reset.removeClass('show'); }; ns.HeaderForm.prototype.onResetClick = function () { this.query.val(''); this.hideReset(); }; ns.HeaderForm.prototype.onSubmitClick = function () { if (this.form.hasClass('active')) { if ('' != _.str.trim(this.query.val())) { this.form.submit(); } } else { this.form.addClass('active'); this.query.focus(); } }; ns.HeaderForm.prototype.onDocumentClick = function (ev) { if (!$(ev.target).closest('form#' + this.options.id).length) { this.form.removeClass('active'); } }; })(qs.defineNS('app.search'));