From b2a578dd7dbeec8122749f25ecf254bb769ebd0e Mon Sep 17 00:00:00 2001 From: adimachmali <1895435+adimachmali@users.noreply.github.com> Date: Sun, 14 Jun 2020 09:28:12 +0300 Subject: [PATCH] fix(PSVAMB-13273): getting qna container when offline (fix undefined messages) (#4160) --- modules/QnA/resources/qna.js | 70 ++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/modules/QnA/resources/qna.js b/modules/QnA/resources/qna.js index fc32c0a0bf..bdb4e478d9 100644 --- a/modules/QnA/resources/qna.js +++ b/modules/QnA/resources/qna.js @@ -250,15 +250,7 @@ var embedPlayer = this.getPlayer(); if (!this.$qnaListContainer && this.getPlayer().isLive()) { - // for unfriendly iFrames, where we can't access window['parent'] we set on page to false - if ( this.getConfig( 'onPage' ) ) { - try{ - var parent = window['parent'].document; - }catch(e){ - this.setConfig('onPage', false); - mw.log("cant access window['parent'] - setting to false"); - } - } + this.markIfCanAccessToIframesParent(); if ( this.getConfig( 'onPage' ) ) { // Inject external CSS files @@ -268,13 +260,7 @@ this.injectCssToPage(this.getConfig('qnaThreadsListCssFileName')); this.injectCssToPage(this.getConfig('qnaMainCssFileName')); //should be last, since we we it to test css was loaded - try{ - var iframeParent = $('#'+this.embedPlayer.id, window['parent'].document)[0]; - $(iframeParent).parents().find("#" + this.getConfig('qnaTargetId')).html("
"); - this.$qnaListContainer = $(iframeParent).parents().find(".qnaInterface"); - }catch(e){ - mw.log("failed to access window['parent'] for creating $qnaListContainer"); - } + this.getQnaInterfaceInsideIframe(); } else { // wrap the .mwPlayerContainer element with our qnaInterface div var floatDirection = this.getConfig( 'containerPosition' ) ? this.getConfig( 'containerPosition' ) : "right"; @@ -320,12 +306,45 @@ } } else if ( !this.getPlayer().isLive() ) { - this.$qnaListContainer = $( ".qnaInterface"); + this.markIfCanAccessToIframesParent(); + + if ( this.getConfig( 'onPage' ) ) { + this.getQnaInterfaceInsideIframe(); + } else { + this.$qnaListContainer = $(".qnaInterface"); + } } return this.$qnaListContainer; }, + markIfCanAccessToIframesParent: function() { + // for unfriendly iFrames, where we can't access window['parent'] we set on page to false + if ( this.getConfig( 'onPage' ) ) { + try { + var parent = window['parent'].document; + } catch(e) { + this.setConfig('onPage', false); + mw.log("cant access window['parent'] - setting to false"); + } + } + }, + + getQnaInterfaceInsideIframe: function() { + try { + var iframeParent = $('#'+this.embedPlayer.id, window['parent'].document)[0]; + this.$qnaListContainer = $(iframeParent).parents().find(".qnaInterface"); + + if (!this.$qnaListContainer.length){ + $(iframeParent).parents().find("#" + this.getConfig('qnaTargetId')).html("
"); + this.$qnaListContainer = $(iframeParent).parents().find(".qnaInterface"); + } + + } catch(e){ + mw.log("failed to access window['parent'] for creating $qnaListContainer"); + } + }, + positionQAButtonOnVideoContainer : function(){ var onVideoTogglePluginButton = $('.qna-on-video-btn'); var videoHeight = this.getPlayer().getInterface().height(); @@ -361,9 +380,14 @@ submitQuestion : function(){ var _this = this; - var textArea = _this.getQnaContainer().find('.qnaQuestionTextArea'); + var qnaContainer$ = _this.getQnaContainer(); + var textArea = qnaContainer$.find('.qnaQuestionTextArea'); var question = textArea.val(); - var questionCharCounter = _this.getQnaContainer().find('.qnaQuestionCharCounter'); + var questionCharCounter = qnaContainer$.find('.qnaQuestionCharCounter'); + + if (!question) { + mw.log("question is not defined"); + } // protection from empty string if (!(/\S/.test(question))){ @@ -400,8 +424,8 @@ .on('focus', function(){ if (textArea.val() === gM('qna-default-question-box-text')) { textArea.val(''); - textArea.removeClass("qnaInterface qnaQuestionTextAreaNotTyping"); - textArea.addClass("qnaInterface qnaQuestionTextAreaTyping"); + textArea.removeClass("qnaQuestionTextAreaNotTyping"); + textArea.addClass("qnaQuestionTextAreaTyping"); } }); @@ -462,8 +486,8 @@ resetTextArea : function(textArea, textCounter){ textArea.val(gM('qna-default-question-box-text')); - textArea.removeClass("qnaInterface qnaQuestionTextAreaTyping"); - textArea.addClass("qnaInterface qnaQuestionTextAreaNotTyping"); + textArea.removeClass("qnaQuestionTextAreaTyping"); + textArea.addClass("qnaQuestionTextAreaNotTyping"); if (textCounter) { textCounter.text('0/' + NUM_OF_MAX_CHAR);