(function (ns) { /** * @alias lib.form.element.Rating */ ns.Rating = function (options) { this.construct.apply(this, arguments); }; ns.Rating.prototype.construct = function (options) { this.options = { elementId: 'rating', multiOptions: [ {'id': 'rating-1', value: '1'}, {'id': 'rating-2', value: '2'}, {'id': 'rating-3', value: '3'}, {'id': 'rating-4', value: '4'}, {'id': 'rating-5', value: '5'} ] }; this.options = $.extend(this.options, options); this.stars = null; this.initStars(); }; ns.Rating.prototype.initStars = function () { var element = $('dd#' + this.options.elementId + '-element'); $('label', element).hide(); element.prepend(this.renderStars()); this.stars = $('#stars-' + this.options.elementId); $('span.rating span.ratingReview', this.stars).mouseover(_.bind(this.onMouseOver, this)); $('span.rating', this.stars).mouseout(_.bind(this.onMouseOut, this)); $('span.ratingReview', this.stars).click(_.bind(this.onClick, this)); var checkedId = $('input:checked', element).attr('id'); if (checkedId) { $('span[data-id=' + checkedId + ']').trigger('click'); } }; ns.Rating.prototype.renderStars = function () { var html = '
'; for (var i in this.options.multiOptions) { if (!this.options.multiOptions.hasOwnProperty(i)) { continue; } var option = this.options.multiOptions[i]; var checked = 'checked' == $('#' + option.id).attr('checked'); html += ''; } html += '
'; return html; }; ns.Rating.prototype.onMouseOver = function (ev) { var current = $(ev.target); while (current.prev('span.empty').length) { current = current.prev('span.empty'); current.addClass('hover').removeClass('empty'); } }; ns.Rating.prototype.onMouseOut = function (ev) { $('span.ratingReview', this.stars).removeClass('hover'); $('span.ratingReview:not(".active")', this.stars).addClass('empty'); }; ns.Rating.prototype.onClick = function (ev) { var dataId = $(ev.target).data('id'); $('input#' + dataId).prop('checked', true); $('span.ratingReview', this.stars).removeClass('active').addClass('empty'); for (var i in this.options.multiOptions) { if (!this.options.multiOptions.hasOwnProperty(i)) { continue; } var option = this.options.multiOptions[i]; $('span.ratingReview[data-id="' + option.id + '"]', this.stars).addClass('active').removeClass('empty'); if (dataId == option.id) { break; } } }; })(qs.defineNS('lib.form.element'));