From 4176b9dfaa2b5a6db44a99011977695c81ea9d57 Mon Sep 17 00:00:00 2001 From: Colin Megill Date: Wed, 21 Aug 2024 18:20:49 -0700 Subject: [PATCH] fix Constants capitalization bug in participationview (#1802) --- client-participation/js/views/comment-form.js | 250 ++++++++++-------- 1 file changed, 138 insertions(+), 112 deletions(-) diff --git a/client-participation/js/views/comment-form.js b/client-participation/js/views/comment-form.js index 39147a91c..4aa052f51 100644 --- a/client-participation/js/views/comment-form.js +++ b/client-participation/js/views/comment-form.js @@ -1,21 +1,21 @@ // Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . var autosize = require("autosize"); -var constants = require("../util/constants"); +var Constants = require("../util/constants"); var CurrentUserModel = require("../stores/currentUser"); var template = require("../templates/comment-form.handlebars"); var display = require("../util/display"); var eb = require("../eventBus"); var Handlebones = require("handlebones"); var M = require("../util/metrics"); -var PolisFacebookUtils = require('../util/facebookButton'); -var ProfilePicView = require('../views/profilePicView'); +var PolisFacebookUtils = require("../util/facebookButton"); +var ProfilePicView = require("../views/profilePicView"); var serialize = require("../util/serialize"); var Strings = require("../strings"); var Utils = require("../util/utils"); var $ = require("jquery"); -var CHARACTER_LIMIT = constants.CHARACTER_LIMIT; +var CHARACTER_LIMIT = Constants.CHARACTER_LIMIT; // var CommentsByMeView = Handlebones.CollectionView.extend({ // modelView: CommentView @@ -32,15 +32,16 @@ module.exports = Handlebones.ModelView.extend({ name: "comment-form", template: template, - // needed to prevent double submissions, which are annoying because they trigger a duplicate alert buttonActive: true, - context: function() { + context: function () { var ctx = Handlebones.ModelView.prototype.context.apply(this, arguments); - ctx = _.extend(ctx, this, this.model&&this.model.attributes); + ctx = _.extend(ctx, this, this.model && this.model.attributes); ctx.is_active = this.parent.model.get("is_active"); - ctx.shouldAutofocusOnTextarea = this.shouldAutofocusOnTextarea || Utils.shouldFocusOnTextareaWhenWritePaneShown(); + ctx.shouldAutofocusOnTextarea = + this.shouldAutofocusOnTextarea || + Utils.shouldFocusOnTextareaWhenWritePaneShown(); ctx.hasTwitter = userObject.hasTwitter; ctx.hasFacebook = userObject.hasFacebook && Constants.FB_APP_ID; ctx.auth_opt_tw = preload.firstConv.auth_opt_tw; @@ -48,7 +49,7 @@ module.exports = Handlebones.ModelView.extend({ ctx.s = Strings; ctx.desktop = !display.xs(); ctx.hideHelp = !Utils.userCanSeeHelp() || preload.firstConv.help_type === 0; - ctx.direction = Strings.direction ? Strings.direction : 'ltr' + ctx.direction = Strings.direction ? Strings.direction : "ltr"; ctx.no_write_hint = false; //preload.firstConv.write_hint_type === 0; @@ -56,38 +57,41 @@ module.exports = Handlebones.ModelView.extend({ if (btnBg) { ctx.customBtnStyles = "background-color: " + btnBg + ";"; } - ctx.charLimitString = Strings.tipCharLimit.replace("{{char_limit}}", constants.CHARACTER_LIMIT); + ctx.charLimitString = Strings.tipCharLimit.replace( + "{{char_limit}}", + Constants.CHARACTER_LIMIT + ); ctx.is_anon = window.preload.firstConv.is_anon; return ctx; }, - hideMessage: function(id) { + hideMessage: function (id) { this.$(id).hide(); }, - showMessage: function(id) { + showMessage: function (id) { // since there are now other warnings/tips showing, hide the default one this.$(".low_priority_tip").hide(); this.$(id).show(); }, - updateOneIdeaPerCommentMessage: function(formText) { + updateOneIdeaPerCommentMessage: function (formText) { // TODO I18N // Tests to see if there is non-punctuation that follows the end of a sentence. - if ((formText||"").match(/[\?\.\!].*[a-zA-Z0-9]+/)) { + if ((formText || "").match(/[\?\.\!].*[a-zA-Z0-9]+/)) { this.showMessage("#one_idea_per_comment_message"); } else { this.hideMessage("#one_idea_per_comment_message"); this.maybeShowBasicTip(); } }, - updateCharLimitExceededMessage: function(formText) { - if (formText.length > constants.CHARACTER_LIMIT) { + updateCharLimitExceededMessage: function (formText) { + if (formText.length > Constants.CHARACTER_LIMIT) { this.showMessage("#char_limit_exceeded_message"); } else { this.hideMessage("#char_limit_exceeded_message"); this.maybeShowBasicTip(); } }, - updateCommentNotQuestionAlert: function(formText) { + updateCommentNotQuestionAlert: function (formText) { if (formText.indexOf("?") >= 0) { this.showMessage("#commentNotQuestionAlert"); } else { @@ -95,14 +99,14 @@ module.exports = Handlebones.ModelView.extend({ this.maybeShowBasicTip(); } }, - maybeShowBasicTip: function() { + maybeShowBasicTip: function () { // if there are no other warnings/tips showing, show the default one if (this.$(".protip:visible").length === 0) { this.chooseBasicTip(); } }, - chooseBasicTip: function() { - var form = $("#comment_form_textarea"); + chooseBasicTip: function () { + var form = $("#comment_form_textarea"); var formText = form.val(); if (formText.length) { this.showMessage("#not_a_reply_message"); @@ -112,15 +116,14 @@ module.exports = Handlebones.ModelView.extend({ this.hideMessage("#not_a_reply_message"); } }, - textChange: function() { - + textChange: function () { this.hideMessage("#comment_sent_message"); this.hideMessage("#comment_send_failed_message"); this.hideMessage("#comment_send_failed_empty_message"); this.hideMessage("#comment_send_failed_too_long_message"); this.hideMessage("#comment_send_failed_duplicate_message"); this.hideMessage("#comment_send_failed_conversation_closed_message"); - var form = $(arguments[0].target); + var form = $(arguments[0].target); var formText = form.val(); var len = formText.length; var remaining = CHARACTER_LIMIT - len; @@ -136,7 +139,9 @@ module.exports = Handlebones.ModelView.extend({ // this.buttonActive = false; this.$("#commentCharCount").text(""); this.$("#commentCharCount").hide(); - this.$("#commentCharCountExceeded").text(txt.replace("{{CHARACTERS_COUNT}}", num)); + this.$("#commentCharCountExceeded").text( + txt.replace("{{CHARACTERS_COUNT}}", num) + ); this.$("#commentCharCountExceeded").show(); } else { num = remaining; @@ -148,7 +153,9 @@ module.exports = Handlebones.ModelView.extend({ // if (remaining > 0) { // } else { // } - this.$("#commentCharCount").text(txt.replace("{{CHARACTERS_COUNT}}", num)); + this.$("#commentCharCount").text( + txt.replace("{{CHARACTERS_COUNT}}", num) + ); this.$("#commentCharCount").show(); this.$("#commentCharCountExceeded").text(""); this.$("#commentCharCountExceeded").hide(); @@ -160,15 +167,15 @@ module.exports = Handlebones.ModelView.extend({ this.updateCommentNotQuestionAlert(formText); eb.trigger(eb.interacted); }, - showFormControls: function() { + showFormControls: function () { // this.$(".alert").hide(); this.$(".comment_form_control_hideable").show(); }, - hideFormControls: function() { + hideFormControls: function () { // this.$(".comment_form_control_hideable").hide(); // this.$("#commentCharCount").text(""); }, - reloadPagePreservingCommentText: function() { + reloadPagePreservingCommentText: function () { var wipCommentFormText = $("#comment_form_textarea").val(); var params = {}; if (wipCommentFormText.length) { @@ -177,10 +184,11 @@ module.exports = Handlebones.ModelView.extend({ eb.trigger(eb.reloadWithMoreParams, params); }, events: { - "focus #comment_form_textarea": function(e) { // maybe on keyup ? + "focus #comment_form_textarea": function (e) { + // maybe on keyup ? this.showFormControls(); }, - "blur #comment_form_textarea": function(e) { + "blur #comment_form_textarea": function (e) { var txt = this.$("#comment_form_textarea").val(); if (!txt || !txt.length) { this.hideFormControls(); @@ -189,15 +197,15 @@ module.exports = Handlebones.ModelView.extend({ "change #comment_form_textarea": "textChange", "keyup #comment_form_textarea": "textChange", "paste #comment_form_textarea": "textChange", - "click #facebookButtonCommentForm" : "facebookClicked", - "click #twitterButtonCommentForm" : "twitterClicked", + "click #facebookButtonCommentForm": "facebookClicked", + "click #twitterButtonCommentForm": "twitterClicked", "click #comment_button": "onSubmitClicked", }, - onSubmitClicked: function(e) { + onSubmitClicked: function (e) { e.preventDefault(); this.submitComment(); }, - submitComment: function(e){ + submitComment: function (e) { var that = this; this.hideMessage("#comment_sent_message"); this.hideMessage("#comment_send_failed_message"); @@ -206,11 +214,10 @@ module.exports = Handlebones.ModelView.extend({ this.hideMessage("#comment_send_failed_duplicate_message"); this.hideMessage("#comment_send_failed_conversation_closed_message"); - function doSubmitComment() { if (that.buttonActive) { that.buttonActive = false; - serialize(that, function(attrs){ + serialize(that, function (attrs) { if (attrs.txt.length === 0) { that.showMessage("#comment_send_failed_empty_message"); that.buttonActive = true; @@ -221,21 +228,29 @@ module.exports = Handlebones.ModelView.extend({ that.buttonActive = true; return; } - that.participantCommented(attrs).then(function() { - that.$("#comment_form_textarea").val(""); - that.hideFormControls(); - that.showMessage("#comment_sent_message"); - - }, function(err) { - // that.showMessage("#comment_send_failed_message"); - }).always(function() { - that.buttonActive = true; - }); + that + .participantCommented(attrs) + .then( + function () { + that.$("#comment_form_textarea").val(""); + that.hideFormControls(); + that.showMessage("#comment_sent_message"); + }, + function (err) { + // that.showMessage("#comment_send_failed_message"); + } + ) + .always(function () { + that.buttonActive = true; + }); }); } } var xid = Utils.getXid(); - var hasSocial = window.userObject.hasFacebook || window.userObject.hasTwitter || !_.isUndefined(xid); + var hasSocial = + window.userObject.hasFacebook || + window.userObject.hasTwitter || + !_.isUndefined(xid); var needsSocial = preload.firstConv.auth_needed_to_write; M.add(M.COMMENT_SUBMIT_CLICK); if (hasSocial || !needsSocial) { @@ -246,35 +261,38 @@ module.exports = Handlebones.ModelView.extend({ this.showSocialAuthChoices(); } }, - onAuthSuccess: function() { + onAuthSuccess: function () { this.reloadPagePreservingCommentText(); // $("#socialButtonsCommentForm").hide(); // $("#comment_form_controls").show(); }, - facebookClicked: function(e) { + facebookClicked: function (e) { e.preventDefault(); var that = this; M.addAndSend(M.COMMENT_SUBMIT_FB_INIT); - PolisFacebookUtils.connect().then(function() { - M.addAndSend(M.COMMENT_SUBMIT_FB_OK); - // wait a bit for new cookies to be ready, or something, then submit comment. - setTimeout(function() { - that.onAuthSuccess(); - // CurrentUserModel.update(); - }, 100); - }, function(err) { - M.addAndSend(M.COMMENT_SUBMIT_FB_ERR); - // alert("facebook error"); - }); + PolisFacebookUtils.connect().then( + function () { + M.addAndSend(M.COMMENT_SUBMIT_FB_OK); + // wait a bit for new cookies to be ready, or something, then submit comment. + setTimeout(function () { + that.onAuthSuccess(); + // CurrentUserModel.update(); + }, 100); + }, + function (err) { + M.addAndSend(M.COMMENT_SUBMIT_FB_ERR); + // alert("facebook error"); + } + ); }, - twitterClicked: function(e) { + twitterClicked: function (e) { var that = this; e.preventDefault(); - eb.on(eb.twitterConnectedCommentForm, function() { + eb.on(eb.twitterConnectedCommentForm, function () { M.addAndSend(M.COMMENT_SUBMIT_TW_OK); // wait a bit for new cookies to be ready, or something, then submit comment. - setTimeout(function() { + setTimeout(function () { that.onAuthSuccess(); // CurrentUserModel.update(); }, 100); @@ -284,19 +302,24 @@ module.exports = Handlebones.ModelView.extend({ // open a new window where the twitter auth screen will show. // that window will redirect back to a simple page that calls window.opener.twitterStatus("ok") - var params = 'location=0,status=0,width=800,height=400'; - window.open(document.location.origin + "/api/v3/twitterBtn?owner=false&dest=/twitterAuthReturn/CommentForm", 'twitterWindow', params); + var params = "location=0,status=0,width=800,height=400"; + window.open( + document.location.origin + + "/api/v3/twitterBtn?owner=false&dest=/twitterAuthReturn/CommentForm", + "twitterWindow", + params + ); }, - showSocialAuthChoices: function() { + showSocialAuthChoices: function () { $("#comment_form_controls").hide(); $("#socialButtonsCommentForm").show(); $("#socialButtonsUnderReadReact").hide(); }, - participantCommented: function(attrs) { + participantCommented: function (attrs) { var that = this; //that = the view attrs.pid = "mypid"; attrs.conversation_id = this.conversation_id; - attrs.vote = constants.REACTIONS.AGREE; // participants' comments are automatically agreed to. Needed for now since math assumes every comment has at least one vote. + attrs.vote = Constants.REACTIONS.AGREE; // participants' comments are automatically agreed to. Needed for now since math assumes every comment has at least one vote. if (/^\s*$/.exec(attrs.txt)) { alert(Strings.commentIsEmpty); @@ -320,47 +343,49 @@ module.exports = Handlebones.ModelView.extend({ if (!promise) { return reject(); } else { - promise.then(function() { - that.trigger("commentSubmitted"); // view.trigger - // $("#comment_form_textarea").hide(); - // $("#commentSentAlert").fadeIn(300); - // setTimeout(function() { - // $("#commentSentAlert").fadeOut(500, function() { - // $("#comment_form_textarea").fadeIn(400); - // }); - // }, 1500); - }, function(err) { - if (err.status === 409) { - - // that.model.set({ - // error: "Duplicate!", - // errorExtra: "That comment already exists.", - // }); - // alert(Strings.commentErrorDuplicate); - that.showMessage("#comment_send_failed_duplicate_message"); - } else if (err.responseText === "polis_err_conversation_is_closed"){ - - // that.model.set({ - // error: "This conversation is closed.", - // errorExtra: "No further commenting is allowed.", - // }); - // alert(Strings.commentErrorConversationClosed); - that.showMessage("#comment_send_failed_conversation_closed_message"); - } else { - - // that.model.set({ - // error: "Error sending comment.", - // errorExtra: "Please try again later.", - // }); - // alert(Strings.commentSendFailed); - that.showMessage("#comment_send_failed_message"); - // this.showMessage("#comment_send_failed_message"); + promise.then( + function () { + that.trigger("commentSubmitted"); // view.trigger + // $("#comment_form_textarea").hide(); + // $("#commentSentAlert").fadeIn(300); + // setTimeout(function() { + // $("#commentSentAlert").fadeOut(500, function() { + // $("#comment_form_textarea").fadeIn(400); + // }); + // }, 1500); + }, + function (err) { + if (err.status === 409) { + // that.model.set({ + // error: "Duplicate!", + // errorExtra: "That comment already exists.", + // }); + // alert(Strings.commentErrorDuplicate); + that.showMessage("#comment_send_failed_duplicate_message"); + } else if (err.responseText === "polis_err_conversation_is_closed") { + // that.model.set({ + // error: "This conversation is closed.", + // errorExtra: "No further commenting is allowed.", + // }); + // alert(Strings.commentErrorConversationClosed); + that.showMessage( + "#comment_send_failed_conversation_closed_message" + ); + } else { + // that.model.set({ + // error: "Error sending comment.", + // errorExtra: "Please try again later.", + // }); + // alert(Strings.commentSendFailed); + that.showMessage("#comment_send_failed_message"); + // this.showMessage("#comment_send_failed_message"); + } } - }); + ); return promise; } }, - initialize: function(options) { + initialize: function (options) { Handlebones.ModelView.prototype.initialize.apply(this, arguments); this.model = options.model; this.conversation_id = options.conversation_id; @@ -370,25 +395,26 @@ module.exports = Handlebones.ModelView.extend({ // })); this.serverClient = options.serverClient; - this.profilePicView = this.addChild(new ProfilePicView({ - model: CurrentUserModel, - })); + this.profilePicView = this.addChild( + new ProfilePicView({ + model: CurrentUserModel, + }) + ); if (options.wipCommentFormText) { this.shouldAutofocusOnTextarea = true; } var that = this; - this.listenTo(this, "render", function(){ - setTimeout(function() { + this.listenTo(this, "render", function () { + setTimeout(function () { if (!_.isUndefined(options.wipCommentFormText)) { $("#comment_form_textarea").val(options.wipCommentFormText); eb.trigger(eb.doneUsingWipCommentFormText); that.submitComment(); } autosize($("#comment_form_textarea")); - },100); + }, 100); }); - }, });