Sfoglia il codice sorgente

Ticket #3987 - Timeline: Auto-attach links doesn't work in Edit form.

AntonLV 4 anni fa
parent
commit
6126923f59

+ 1 - 0
modules/boonex/timeline/classes/BxTimelineConfig.php

@@ -307,6 +307,7 @@ class BxTimelineConfig extends BxBaseModNotificationsConfig
         $this->_aHtmlIds = array(
             'view' => array(
                 'edit_form' => $sHp . '-edit-',
+                'attach_link_form_field' => $sHp . '-attach-link-form-field-',
 
                 'menu_popup' => $sHp . '-menu-popup-',
 

+ 10 - 7
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -581,30 +581,33 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
     {
         $sUrl = bx_process_input(bx_get('url'));
         if(empty($sUrl))
-            return echoJson(array());
+            return echoJson([]);
         
         $sUrl = htmlspecialchars_decode($sUrl);
 
+        $aHeaders = @get_headers($sUrl, 1);
+        if($aHeaders === false)
+            return echoJson([]);
+
         $sHeader = 'Content-Type';
-        $aHeaders = get_headers($sUrl, 1);
-        if(!empty($aHeaders) && is_array($aHeaders) && !empty($aHeaders[$sHeader])) {
+        if(!empty($aHeaders[$sHeader])) {
             $mixedContentType = $aHeaders[$sHeader];
             if(!is_array($mixedContentType))
-                $mixedContentType = array($mixedContentType);
+                $mixedContentType = [$mixedContentType];
 
             foreach($mixedContentType as $sContentType)
                 if(strpos($sContentType, 'image') !== false) 
-                    return echoJson(array());
+                    return echoJson([]);
         }
 
         $iEventId = 0;
         if(bx_get('event_id') !== false)
             $iEventId = (int)bx_get('event_id');
 
-        echoJson($this->addAttachLink(array(
+        echoJson($this->addAttachLink([
             'event_id' => $iEventId,
             'url' => $sUrl
-        )));
+        ]));
     }
 
     public function actionDeleteAttachLink()

+ 3 - 0
modules/boonex/timeline/classes/BxTimelineTemplate.php

@@ -128,6 +128,9 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
             'sObjNameMenuFeeds' => $this->_oConfig->getObject('menu_feeds'),
             'bInfScroll' => $this->_oConfig->isInfiniteScroll(),
             'iInfScrollAutoPreloads' => $this->_oConfig->getAutoPreloads(),
+            'iLimitAttachLinks' => $this->_oConfig->getLimitAttachLinks(),
+            'sLimitAttachLinksErr' => bx_js_string(_t('_bx_timeline_txt_err_attach_links')),
+            'oAttachedLinks' => []
         ], $aParams);
 
         return parent::getJsCode('view', $aParams, $bWrap, $bDynamic);

+ 111 - 0
modules/boonex/timeline/js/main.js

@@ -37,6 +37,14 @@ function BxTimelineMain() {
     this.bViewTimeline = false;
     this.bViewOutline = false;
     this.bViewItem = false;
+
+    this._sPregTag = "(<([^>]+bx-tag[^>]+)>)";
+    this._sPregMention = "(<([^>]+bx-mention[^>]+)>)";
+    this._sPregUrl = "(([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:\\/[\\+~%#\\/\\.\\w\\-_!\\(\\)]*)?\\??(?:[\\-\\+=&;%@\\.\\w_]*)#?(?:[\\.\\!\\/\\w]*))?";
+
+    this._iLimitAttachLinks = 0;
+    this._sLimitAttachLinksErr = '';
+    this._oAttachedLinks = {};
 }
 
 BxTimelineMain.prototype.initView = function() 
@@ -257,6 +265,109 @@ BxTimelineMain.prototype.initFlickityByItem = function(oItem) {
         this.initFlickityAttachments(oGalleryAttachments);
 };
 
+BxTimelineMain.prototype.initTrackerInsertSpace = function(sFormId, iEventId)
+{
+    var $this = this;
+    var oForm = $('#' + sFormId);
+    var oTextarea = oForm.find('textarea');
+
+    this._oAttachedLinks = [];
+
+    if (typeof window.glOnSpaceEnterInEditor === 'undefined')
+        window.glOnSpaceEnterInEditor = [];    
+
+    window.glOnSpaceEnterInEditor.push(function(sData, sSelector) {
+        if(!oTextarea.is(sSelector))
+            return;
+
+        $this.parseContent(oForm, iEventId, sData, true);
+    });
+};
+
+BxTimelineMain.prototype.parseContent = function(oForm, iId, sData, bPerformAttach)
+{
+    var oExp, aMatch = null;
+
+    oExp = new RegExp(this._sPregTag , "ig");
+    sData = sData.replace(oExp, '');
+
+    oExp = new RegExp(this._sPregMention , "ig");
+    sData = sData.replace(oExp, '');
+
+    oExp = new RegExp(this._sPregUrl , "ig");
+    while(aMatch = oExp.exec(sData)) {
+        var sUrl = aMatch[0].replace(/^(\s|(&nbsp;))+|(\s|(&nbsp;))+$/gm,'');        
+        if(!sUrl.length || this._oAttachedLinks[sUrl] != undefined || (this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length >= this._iLimitAttachLinks))
+            continue;
+
+        //--- Mark that 'attach link' process was started.
+        this._oAttachedLinks[sUrl] = 0;
+
+        if(bPerformAttach) {
+            this.lockForm(oForm);
+
+            this.addAttachLink(oForm, iId, sUrl);
+        }
+    }
+};
+
+BxTimelineMain.prototype.lockForm = function(oForm)
+{
+    if(this.isLockedForm(oForm))
+        return;
+
+    oForm.attr('bx_form_locked', 1).find('input[type="submit"],button[type="submit"]').addClass('bx-btn-disabled');
+};
+
+BxTimelineMain.prototype.unlockForm = function(oForm)
+{
+    if(!this.isLockedForm(oForm))
+        return;
+
+    oForm.removeAttr('bx_form_locked').find('input[type="submit"],button[type="submit"]').removeClass('bx-btn-disabled');
+};
+
+BxTimelineMain.prototype.isLockedForm = function(oForm)
+{
+    return oForm.attr('bx_form_locked') == 1;
+};
+
+BxTimelineMain.prototype.addAttachLink = function(oElement, iId, sUrl)
+{
+    if(!sUrl || (this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length > this._iLimitAttachLinks))
+        return;
+
+    var $this = this;
+    var oData = this._getDefaultData();
+    oData['url'] = sUrl;
+    if(iId != undefined)
+        oData['event_id'] = iId;
+
+    jQuery.post (
+        this._sActionsUrl + 'add_attach_link/',
+        oData,
+        function(oData) {
+            var iEventId = 0;
+            if(oData && oData.event_id != undefined)
+                iEventId = parseInt(oData.event_id);
+            
+            if(!oData.id || !oData.item || !$.trim(oData.item).length){
+                $this.unlockForm($('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).parents('form:first'));
+                return;
+            }
+
+            //--- Mark that 'attach link' process was finished.
+            $this._oAttachedLinks[sUrl] = oData.id;
+
+            var oItem = $(oData.item).hide();
+            $('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed);
+
+            $this.unlockForm($('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).parents('form:first'));
+        },
+        'json'
+    );
+};
+
 BxTimelineMain.prototype.onFindOverflow = function(oElement) {
     $(oElement).after($(oElement).parents('.' + this.sClassView + ':first').find('.' + this.sSP + '-content-show-more:hidden:first').clone().show());
 };

+ 1 - 90
modules/boonex/timeline/js/post.js

@@ -26,10 +26,6 @@ function BxTimelinePost(oOptions) {
     this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
     this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
 
-    this._sPregTag = "(<([^>]+bx-tag[^>]+)>)";
-    this._sPregMention = "(<([^>]+bx-mention[^>]+)>)";
-    this._sPregUrl = "(([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:\\/[\\+~%#\\/\\.\\w\\-_!\\(\\)]*)?\\??(?:[\\-\\+=&;%@\\.\\w_]*)#?(?:[\\.\\!\\/\\w]*))?";
-
     var $this = this;
     if (typeof window.glOnInitEditor === 'undefined')
         window.glOnInitEditor = [];
@@ -86,8 +82,6 @@ BxTimelinePost.prototype.initFormPost = function(sFormId)
     var oTextarea = oForm.find('textarea');
     autosize(oTextarea);
 
-    this._oAttachedLinks = [];
-
     oForm.ajaxForm({
         dataType: "json",
         beforeSubmit: function (formData, jqForm, options) {
@@ -98,35 +92,7 @@ BxTimelinePost.prototype.initFormPost = function(sFormId)
         }
     });
 
-    if (typeof window.glOnSpaceEnterInEditor === 'undefined')
-        window.glOnSpaceEnterInEditor = [];    
-
-    window.glOnSpaceEnterInEditor.push(function (sData, sSelector) {
-        if(!oTextarea.is(sSelector))
-            return;
-
-        var oExp, aMatch = null;
-
-        oExp = new RegExp($this._sPregTag , "ig");
-        sData = sData.replace(oExp, '');
-
-        oExp = new RegExp($this._sPregMention , "ig");
-        sData = sData.replace(oExp, '');
-
-        oExp = new RegExp($this._sPregUrl , "ig");
-        while(aMatch = oExp.exec(sData)) {
-            var sUrl = aMatch[0].replace(/^(\s|(&nbsp;))+|(\s|(&nbsp;))+$/gm,'');
-            if(!sUrl.length || $this._oAttachedLinks[sUrl] != undefined || ($this._iLimitAttachLinks != 0 && Object.keys($this._oAttachedLinks).length >= $this._iLimitAttachLinks))
-                continue;
-
-            $this.lockForm($('#' + sFormId));
-
-            //--- Mark that 'attach link' process was started.
-            $this._oAttachedLinks[sUrl] = 0;
-
-            $this.addAttachLink(oForm, sUrl);
-        }
-    });
+    this.initTrackerInsertSpace(sFormId);
 
     if(this._bAutoAttach) {
         if (typeof window.glOnInsertImageInEditor === 'undefined')
@@ -192,27 +158,6 @@ BxTimelinePost.prototype.afterFormPostSubmit = function (oForm, oData)
     else
         fContinue();
 };
-
-BxTimelinePost.prototype.lockForm = function(oForm)
-{
-    if(this.isLockedForm(oForm))
-        return;
-
-    oForm.attr('bx_form_locked', 1).find('input[type="submit"],button[type="submit"]').addClass('bx-btn-disabled');
-};
-
-BxTimelinePost.prototype.unlockForm = function(oForm)
-{
-    if(!this.isLockedForm(oForm))
-        return;
-
-    oForm.removeAttr('bx_form_locked').find('input[type="submit"],button[type="submit"]').removeClass('bx-btn-disabled');
-};
-
-BxTimelinePost.prototype.isLockedForm = function(oForm)
-{
-    return oForm.attr('bx_form_locked') == 1;
-};
     
 BxTimelinePost.prototype.initFormAttachLink = function(sFormId)
 {
@@ -312,40 +257,6 @@ BxTimelinePost.prototype.deleteAttachLink = function(oLink, iId)
     return false;
 };
 
-BxTimelinePost.prototype.addAttachLink = function(oElement, sUrl)
-{
-    if(!sUrl || (this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length > this._iLimitAttachLinks))
-        return;
-
-    var $this = this;
-    var oData = this._getDefaultData();
-    oData['url'] = sUrl;
-
-    jQuery.post (
-        this._sActionsUrl + 'add_attach_link/',
-        oData,
-        function(oData) {
-            var iEventId = 0;
-            if(oData && oData.event_id != undefined)
-                iEventId = parseInt(oData.event_id);
-            
-            if(!oData.id || !oData.item || !$.trim(oData.item).length){
-                $this.unlockForm($('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).parents('form:first'));
-                return;
-            }
-
-            //--- Mark that 'attach link' process was finished.
-            $this._oAttachedLinks[sUrl] = oData.id;
-
-            var oItem = $(oData.item).hide();
-            $('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed);
-
-            $this.unlockForm($('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).parents('form:first'));
-        },
-        'json'
-    );
-};
-
 BxTimelinePost.prototype.showAttachLink = function(oLink, iEventId)
 {
     if(this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length >= this._iLimitAttachLinks) {

+ 21 - 12
modules/boonex/timeline/js/view.js

@@ -24,6 +24,9 @@ function BxTimelineView(oOptions) {
     this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
     this._sVideosAutoplay = oOptions.sVideosAutoplay == undefined ? 'off' : oOptions.sVideosAutoplay;
     this._bEventsToLoad = oOptions.bEventsToLoad == undefined ? false : oOptions.bEventsToLoad;
+    this._iLimitAttachLinks = oOptions.iLimitAttachLinks == undefined ? 0 : oOptions.iLimitAttachLinks;
+    this._sLimitAttachLinksErr = oOptions.sLimitAttachLinksErr == undefined ? '' : oOptions.sLimitAttachLinksErr;
+    this._oAttachedLinks = oOptions.oAttachedLinks == undefined ? {} : oOptions.oAttachedLinks;
     this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
     this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
 
@@ -791,12 +794,14 @@ BxTimelineView.prototype.muteAuthor = function(oLink, iId)
     );
 };
 
-BxTimelineView.prototype.initFormEdit = function(sFormId)
+BxTimelineView.prototype.initFormEdit = function(sFormId, iEventId)
 {
     var $this = this;
     var oForm = $('#' + sFormId);
+    var oTextarea = oForm.find('textarea');
+
+    autosize(oTextarea);
 
-    autosize(oForm.find('textarea'));
     oForm.ajaxForm({
         dataType: "json",
         beforeSubmit: function (formData, jqForm, options) {
@@ -806,6 +811,12 @@ BxTimelineView.prototype.initFormEdit = function(sFormId)
             window[$this._sObjName].afterFormEditSubmit(oForm, oData);
         }
     });
+
+    this.initTrackerInsertSpace(sFormId, iEventId);
+
+    var sContent = oTextarea.val();
+    if(sContent && sContent.length > 0)
+        this.parseContent(oForm, iEventId, sContent, false);
 };
 
 BxTimelineView.prototype.beforeFormEditSubmit = function(oForm)
@@ -817,21 +828,19 @@ BxTimelineView.prototype.afterFormEditSubmit = function (oForm, oData)
 {
     var $this = this;
     var fContinue = function() {
-        if(oData && oData.id != undefined) {
-            var iId = parseInt(oData.id);
-            if(iId <= 0) 
-                return;
-
-            $this._getPost($this.oView, iId, $this._oRequestParams);
-            return;
-        }
+        var iId = 0;
+        if(oData && oData.id != undefined)
+            iId = parseInt(oData.id);
 
         if(oData && oData.form != undefined && oData.form_id != undefined) {
             $('#' + oData.form_id).replaceWith(oData.form);
-            $this.initFormEdit(oData.form_id);
+            $this.initFormEdit(oData.form_id, iId);
 
             return;
         }
+
+        if(iId > 0) 
+            $this._getPost($this.oView, iId, $this._oRequestParams);
     };
 
     this.loadingInButton($(oForm).children().find(':submit'), false);
@@ -888,7 +897,7 @@ BxTimelineView.prototype.onEditPost = function(oData)
     if(oData && oData.form != undefined && oData.form_id != undefined) {
         oItem.find('.' + this.sClassItemContent).bx_anim('hide', this._sAnimationEffect, this._iAnimationSpeed, function() {
             $(this).html(oData.form).bx_anim('show', $this._sAnimationEffect, $this._iAnimationSpeed, function() {
-                $this.initFormEdit(oData.form_id);
+                $this.initFormEdit(oData.form_id, oData.id);
             });
         });
     }