UNPKG

doushio

Version:
134 lines (110 loc) 2.82 kB
(function () { var pubKey = config.RECAPTCHA_PUBLIC_KEY; var REPORTS = {}; if (pubKey) menuOptions.push('Report'); var Report = Backbone.Model.extend({ defaults: { status: 'setup', }, request_new: function () { var self = this; this.set('status', 'requesting'); Recaptcha.create(pubKey, 'captcha', { theme: 'clean', callback: function () { self.set('status', 'ready'); } }); if (this.get('timeout')) clearTimeout(this.get('timeout')); this.set('timeout', setTimeout(function () { if (REPORTS[num] === self) { self.set({status: 'timeout', timeout: 0}); delete REPORTS[num]; } }, 5*60*1000)); }, }); var ReportPanel = Backbone.View.extend({ id: 'report-panel', tagName: 'form', className: 'modal', events: { submit: 'submit', 'click .close': 'remove', }, initialize: function () { this.$el .append('Reporting post >>' + this.model.id) .append('<a class="close">[x]</a>') .append('<div id="captcha"/>') .append($('<div class="captcha-error"/>').hide()) .append($('<input>', { type: 'submit', val: 'Report', "class": 'submit-report' })); this.listenTo(this.model, { change: this.render, 'change:status': this.statusDispatch, }); }, render: function () { var status = this.model.get('status'); this.$('.submit-report').prop('disabled', status != 'ready'); this.$('.captcha-error').toggle(status == 'error') .text(this.model.get('error')); return this; }, submit: function () { if (this.model.get('status') != 'ready') return; send([REPORT_POST, this.model.id, Recaptcha.get_challenge(), Recaptcha.get_response()]); this.model.set('status', 'reporting'); return false; }, statusDispatch: function () { var status = this.model.get('status'); if (status == 'ready') { Recaptcha.focus_response_field(); } else if (status == 'timeout') { this.model.request_new(); } else if (status == 'reported') { Recaptcha.destroy(); } }, remove: function () { Backbone.View.prototype.remove.call(this); return false; }, }); var ajaxJs = 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'; menuHandlers.Report = function (num) { var model = REPORTS[num]; if (!model) REPORTS[num] = model = new Report({id: num}); var view = new ReportPanel({model: model}); view.render().$el.appendTo('body'); yepnope({load: ajaxJs, callback: function () { if (!window.Recaptcha) return alert("Couldn't load reCAPTCHA."); model.request_new(); }}); }; dispatcher[REPORT_POST] = function (msg, op) { var num = msg[0], etc = msg[1]; var report = REPORTS[num]; if (!report) return; report.set(msg[1] || {status: 'reported'}); delete REPORTS[num]; if (report.get('timeout')) { clearTimeout(report.get('timeout')); report.set('timeout', 0); } }; })();