search_form.js 1.93 KB
Newer Older
1 2
;(function (define) {

3
define(['jquery', 'backbone', 'gettext'], function ($, Backbone, gettext) {
4 5 6 7 8 9
   'use strict';

    return Backbone.View.extend({

        el: '#discovery-form',
        events: {
10
            'submit form': 'submitForm'
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
        },

        initialize: function () {
            this.$searchField = this.$el.find('input');
            this.$searchButton = this.$el.find('button');
            this.$message = this.$el.find('#discovery-message');
            this.$loadingIndicator = this.$el.find('#loading-indicator');
        },

        submitForm: function (event) {
            event.preventDefault();
            this.doSearch();
        },

        doSearch: function (term) {
26
            if (term !== undefined) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
                this.$searchField.val(term);
            }
            else {
                term = this.$searchField.val();
            }
            this.trigger('search', $.trim(term));
        },

        clearSearch: function () {
            this.$searchField.val('');
        },

        showLoadingIndicator: function () {
            this.$loadingIndicator.removeClass('hidden');
        },

        hideLoadingIndicator: function () {
            this.$loadingIndicator.addClass('hidden');
        },

47 48 49 50 51 52 53 54 55
        showFoundMessage: function (count) {
            var msg = ngettext(
                'Viewing %s course',
                'Viewing %s courses',
                count
            );
            this.$message.html(interpolate(msg, [count]));
        },

56 57 58 59 60 61
        showNotFoundMessage: function (term) {
            var msg = interpolate(
                gettext('We couldn\'t find any results for "%s".'),
                [_.escape(term)]
            );
            this.$message.html(msg);
62
            this.clearSearch();
63 64 65 66 67 68 69 70 71 72 73
        },

        showErrorMessage: function () {
            this.$message.html(gettext('There was an error, try searching again.'));
        }

    });

});

})(define || RequireJS.define);