Committer: szhirkov
LJSUP-7848: temp fallback to prev versionU trunk/htdocs/js/commentmanage.js
Modified: trunk/htdocs/js/commentmanage.js =================================================================== --- trunk/htdocs/js/commentmanage.js 2011-02-16 08:51:42 UTC (rev 18319) +++ trunk/htdocs/js/commentmanage.js 2011-02-16 09:11:33 UTC (rev 18320) @@ -60,16 +60,6 @@ } } -function getLocalizedStr( key, username ) { - var str = ""; - if( key in Site.ml_text ) { - str = Site.ml_text[ key ]; - str = str.replace( '%username%', username ); - } - - return str; -} - // hsv to rgb // h, s, v = [0, 1), [0, 1], [0, 1] // r, g, b = [0, 255], [0, 255], [0, 255] @@ -100,16 +90,15 @@ return [v,p,q]; } -window.curPopup = null; -window.curPopup_id = 0; +var curPopup = null; +var curPopup_id = 0; function killPopup () { - if (!window.curPopup) { - return true; - } + if (!curPopup) + return true; - var popup = window.curPopup; - window.curPopup = null; + var popup = curPopup; + curPopup = null; var opp = 1.0; @@ -125,159 +114,10 @@ } }; fade(); - - jQuery(document).unbind('.commentManagePopup'); return true; } -/** - * Create popup element, insert content and show it with animation - * - * @param {String} contentHtml - * @param {DOM} targetControl - * @param {Object} e - * @param {String} id - */ -function createPopup(contentHtml, targetControl, e, id) { - targetControl = jQuery(targetControl).find('img'); - - var popupElem = jQuery('<div class="ljcmtmanage b-popup" style="text-align:left;position:absolute;visibility:hidden;width:250px;left:0;top:0;z-index:3"><div class="b-popup-outer"><div class="b-popup-inner"><div class="ljcmtmanage-content"></div><i class="i-popup-arr i-popup-arrtl"></i><i class="i-popup-close"></i></div></div></div>'), - - popupCloseControlSelector = '.i-popup-close', - popupArrowSelector = '.i-popup-arr', - popupContentSelector = '.ljcmtmanage-content', - - popupContent = popupElem.find(popupContentSelector), - popupArrow = popupElem.find(popupArrowSelector), - - targetOffset = targetControl.offset(); - - // popup already exist - if (window.curPopup) { - if (window.curPopup_id == id) { - e.stopPropagation(); - return false; - } - - killPopup(); - } - - popupContent.html(contentHtml); - - popupElem - .delegate(popupCloseControlSelector, 'click', killPopup) - .click(function (e) { e.stopPropagation(); }) - - .appendTo('body') - - .css({ - visibility: 'visible', - display: 'block' - }); - - - placeElemNear(popupElem, targetControl); - - popupElem.fadeIn('fast'); - - window.curPopup = popupElem[0]; - window.curPopup_id = id; - - jQuery(document).bind('click.commentManagePopup keydown.commentManagePopup', function (e) { - if ((e.type == 'keydown' && e.keyCode == 27) || e.type != 'keydown') { - killPopup(); - } - }); - - function placeElemNear(elem, target) { - /** - * Popup types - * - * * -^--- * - * tl | | - * * ----- * - * - * * ---^- * - * tr | | - * * ----- * - * - * * ----- * - * bl | | - * * -V--- * - * - * * ----- * - * br | | - * * ---V- * - */ - - var classNamePrefix = 'i-popup-arr', - - viewport = jQuery(window), - viewportWidth = viewport.width(), - viewportHeight = viewport.height(), - - elemWidth = elem.width(), - elemHeight = elem.height(), - - positionType = { - x: 'l', // left - y: 't' // top - }, - positionTypes = { - 'tl': function () { - return { - x: targetOffset.left - popupArrow.position().left - (popupArrow.width() / 2) + (targetControl.width() / 2), - y: targetOffset.top + popupArrow.height() - popupArrow.position().top + (targetControl.height() / 2) - }; - }, - 'tr': function () { - return { - x: targetOffset.left - popupArrow.position().left - (popupArrow.width() / 2) + (targetControl.width() / 2), - y: targetOffset.top + popupArrow.height() - popupArrow.position().top + (targetControl.height() / 2) - }; - }, - 'bl': function () { - return { - x: targetOffset.left - popupArrow.position().left - (popupArrow.width() / 2) + (targetControl.width() / 2), - y: targetOffset.top - popupArrow.height() - elemHeight - }; - }, - 'br': function () { - return { - x: targetOffset.left - popupArrow.position().left - (popupArrow.width() / 2) + (targetControl.width() / 2), - y: targetOffset.top - popupArrow.height() - elemHeight - }; - } - }, - position, - - checkAngle = { - x: positionTypes.tl().x + elemWidth, - y: positionTypes.tl().y + elemHeight - }; - - if (checkAngle.x > viewportWidth) { - positionType.x = 'r'; // right - } - - if (checkAngle.y > viewportHeight + viewport.scrollTop()) { - positionType.y = 'b'; // bottom - } - - positionType = positionType.y + positionType.x; - popupArrow.removeClass('i-popup-arrtl').addClass(classNamePrefix + positionType); - position = positionTypes[positionType](); - - elem.css({ - 'left': Math.floor(position.x) + 'px', - 'top': Math.floor(position.y) + 'px' - }); - } - - return popupElem; -} - function deleteComment (ditemid, isS1) { killPopup(); @@ -374,11 +214,19 @@ } } -function createDeleteFunction(ae, dItemid, isS1) { +function docClicked () { + killPopup(); + + // we didn't handle anything, who are we kidding +} + +function createDeleteFunction (ae, dItemid, isS1) { return function (e) { e = jQuery.event.fix(e || window.event); - if (e.shiftKey || (window.curPopup && window.curPopup_id != dItemid)) { + var finalHeight = 115; + + if (e.shiftKey || (curPopup && curPopup_id != dItemid)) { killPopup(); } @@ -388,7 +236,7 @@ doIT = 1; deleteComment(dItemid, isS1); } else { - if (!LJ_cmtinfo) + if (! LJ_cmtinfo) return true; var com = LJ_cmtinfo[dItemid]; @@ -397,30 +245,90 @@ return true; var canAdmin = LJ_cmtinfo["canAdmin"]; - var inHTML = [ "<form style='display: inline' id='ljdelopts" + dItemid + "'><span style='font-face: Arial; font-size: 8pt'><strong>" + getLocalizedStr( 'comment.delete.q', com.u ) + "</strong><br />" ]; + var pos_offset = jQuery(ae).position(), + offset = jQuery(ae).offset(), + pos_x = e.pageX + pos_offset.left - offset.left, + top = e.pageY + pos_offset.top - offset.top + 5, + left = Math.max(pos_x + 5 - 250, 5), + $window = jQuery(window); + + //calc with viewport + if ($window.scrollLeft() > left + offset.left - pos_offset.left) { + left = $window.scrollLeft() + pos_offset.left - offset.left; + } + + if (curPopup && curPopup_id == dItemid) { + //calc with viewport + top = Math.min(top, $window.height() + $window.scrollTop() - jQuery(curPopup).outerHeight() + pos_offset.top - offset.top); + curPopup.style.left = left + 'px'; + curPopup.style.top = top + 'px'; + + return Event.stop(e); + } + + var de = jQuery('<div class="ljcmtmanage" style="text-align:left;position:absolute;visibility:hidden;width:250px;left:0;top:0;z-index:3"></div>') + .click(function(e){ + e.stopPropagation() + }); + + var inHTML = "<form style='display: inline' id='ljdelopts" + dItemid + "'><span style='font-face: Arial; font-size: 8pt'><b>Delete comment?</b><br />"; var lbl; if (com.username != "" && com.username != remoteUser && canAdmin) { lbl = "ljpopdel" + dItemid + "ban"; - inHTML.push("<input type='checkbox' name='ban' id='" + lbl + "'> <label for='" + lbl + "'>" + getLocalizedStr( 'comment.ban.user', com.u ) + "</label><br />"); + inHTML += "<input type='checkbox' name='ban' id='" + lbl + "'> <label for='" + lbl + "'>Ban <b>" + com.u + "</b> from commenting</label><br />"; + } else { + finalHeight -= 15; } if (remoteUser != com.username) { lbl = "ljpopdel" + dItemid + "spam"; - inHTML.push("<input type='checkbox' name='spam' id='" + lbl + "'> <label for='" + lbl + "'>" + getLocalizedStr( 'comment.mark.spam', com.u ) + "</label><br />"); + inHTML += "<input type='checkbox' name='spam' id='" + lbl + "'> <label for='" + lbl + "'>Mark this comment as spam</label><br />"; + } else { + finalHeight -= 15; } if (com.rc && com.rc.length && canAdmin) { lbl = "ljpopdel" + dItemid + "thread"; - inHTML.push("<input type='checkbox' name='delthread' id='" + lbl + "'> <label for='" + lbl + "'>" + getLocalizedStr( 'comment.delete.all.sub', com.u ) + "</label><br />"); + inHTML += "<input type='checkbox' name='delthread' id='" + lbl + "'> <label for='" + lbl + "'>Delete thread (all subcomments)</label><br />"; + } else { + finalHeight -= 15; } if (canAdmin&&com.username) { lbl = "ljpopdel" + dItemid + "author"; - inHTML.push("<input type='checkbox' name='delauthor' id='" + lbl + "'> <label for='" + lbl + "'>" + getLocalizedStr( 'comment.delete.all', "<b>" + ( (com.username == remoteUser ? 'my' : com.u) ) + "</b>" ) + "</label><br />"); + inHTML += "<input type='checkbox' name='delauthor' id='" + lbl + "'> <label for='" + lbl + "'>Delete all <b>" + (com.username == remoteUser ? 'my' : com.u) + "</b> comments in this post</label><br />"; + } else { + finalHeight -= 15; } - inHTML.push("<input type='button' value='" + getLocalizedStr( 'comment.delete', com.u ) + "' onclick='deleteComment(" + dItemid + ", " + isS1.toString() + ");' /></span><br /><div class='b-bubble b-bubble-alert b-bubble-noarrow'><i class='i-bubble-arrow-border'></i><i class='i-bubble-arrow'></i>" + getLocalizedStr( 'comment.delete.no.options', com.u ) + "</div></form>"); + inHTML += "<input type='button' value='Delete' onclick='deleteComment(" + dItemid + ", " + isS1.toString() + ");' /> <input type='button' value='Cancel' onclick='killPopup()' /></span><br /><span style='font-face: Arial; font-size: 8pt'><i>shift-click to delete without options</i></span></form>"; - createPopup(inHTML.join(' '), ae, e, 'deletePopup' + dItemid); + de.html(inHTML).insertAfter(ae); + + //calc with viewport + top = Math.min(top, $window.height() + $window.scrollTop() - de.outerHeight() + pos_offset.top - offset.top); + + de.css({ + left: left, + top: top, + height: 10, + visibility: 'visible', + overflow: 'hidden' + }); + + curPopup = de[0]; + curPopup_id = dItemid; + + var height = 10; + var grow = function () { + height += 7; + if (height > finalHeight) { + de.height('auto'); + } else { + de.height(height); + window.setTimeout(grow, 20); + } + } + grow(); } Event.stop(e); } @@ -487,195 +395,146 @@ } var tsInProg = {} // dict of { ditemid => 1 } -function createModerationFunction(control, dItemid, isS1, action) { - var action = action || 'screen'; // "screen" action by default - - return function (e) { - var e = jQuery.event.fix(e || window.event), +function createModerationFunction(ae, dItemid, isS1) +{ + return function(e) + { + var e = jQuery.event.fix(e || window.event); pos = { x: e.pageX, y: e.pageY }, - modeParam = LiveJournal.parseGetArgs(location.href).mode, - hourglass; - - e.stopPropagation(); - e.preventDefault(); - - if (action == 'spam' && !modeParam) { - showDialogPopup(); - } else { - sendModerateRequest(); - } - - - function showDialogPopup() { - var popupElem = createPopup('Do you really want to mark comment as a spam? All comments of this author will be deleted and we will ban author.<button class="spam-comment-control">OK</button>', control, e, 'spamComment' + dItemid); - - if (popupElem) { - popupElem.delegate('.spam-comment-control', 'click', function (e) { - e.preventDefault(); - sendModerateRequest(); - killPopup(); - }); - } - } - - function sendModerateRequest() { - var bmlName = { 'screen': 'talkscreen', 'spam': 'spamcomment' }[action], - postUrl = control.href.replace(new RegExp('.+' + bmlName + '\.bml'), LiveJournal.getAjaxUrl(bmlName)) + '&jsmode=1', - postParams = { 'confirm': 'Y', lj_form_auth: LJ_cmtinfo.form_auth }; - + postUrl = ae.href.replace(/.+talkscreen\.bml/, LiveJournal.getAjaxUrl('talkscreen')), hourglass = jQuery(e).hourglass()[0]; - - if (!isS1 && action == 'spam') { - postUrl = postUrl.replace('id', 'talkid'); - } - - jQuery.post(postUrl, postParams, function (json) { + + var xhr = jQuery.post(postUrl + '&jsmode=1', + { + confirm: 'Y', + lj_form_auth: LJ_cmtinfo.form_auth + }, + function(json) + { tsInProg[dItemid] = 0; + var comms_ary = [dItemid] + var map_comms = function(id) + { + var i = -1, new_id; + while(new_id = LJ_cmtinfo[id].rc[++i]) + { + if (LJ_cmtinfo[new_id].full) { + comms_ary.push(new_id); + map_comms(String(new_id)); + } + } + } - if (isS1) { - handleS1(); + // check rc for no comments page + if (LJ_cmtinfo[dItemid].rc) { + if (/mode=(un)?freeze/.test(ae.href)) { + map_comms(dItemid); + } + var ids = '#ljcmt' + comms_ary.join(',#ljcmt'); } else { - var ids = checkRcForNoCommentsPage(json); - handleS2(ids); + var rpcRes; + eval(json); + updateLink(ae, rpcRes, ae.getElementsByTagName('img')[0]); + // /tools/recent_comments.bml + if (document.getElementById('ljcmtbar'+dItemid)) { + var ids = '#ljcmtbar'+dItemid; + } + // ex.: portal/ + else { + hourglass.hide(); + poofAt(pos); + return; + } } - }); - } - - function handleS1() { - var newNode, showExpand, j, children, - threadId = dItemid, - threadExpanded = !!(LJ_cmtinfo[ threadId ].oldvars && LJ_cmtinfo[ threadId ].full); - populateComments = function (result) { - for( var i = 0; i < result.length; ++i ){ - if( LJ_cmtinfo[ result[i].thread ].full ){ - showExpand = !( 'oldvars' in LJ_cmtinfo[ result[i].thread ]); - - //still show expand button if children comments are folded - if( !showExpand ) { - children = LJ_cmtinfo[ result[i].thread ].rc; - - for( j = 0; j < children.length; ++j ) { - if( !( 'oldvars' in LJ_cmtinfo[ children[j] ] ) ) { - showExpand = true; + + if(isS1){ + var newNode, showExpand, j, children; + var threadId = dItemid, + threadExpanded = !!(LJ_cmtinfo[ threadId ].oldvars && LJ_cmtinfo[ threadId ].full); + populateComments = function(result){ + for( var i = 0; i < result.length; ++i ){ + if( LJ_cmtinfo[ result[i].thread ].full ){ + showExpand = !( 'oldvars' in LJ_cmtinfo[ result[i].thread ]); + + //still show expand button if children comments are folded + if( !showExpand ) { + children = LJ_cmtinfo[ result[i].thread ].rc; + + for( j = 0; j < children.length; ++j ) { + if( !( 'oldvars' in LJ_cmtinfo[ children[j] ] ) ) { + showExpand = true; + } + } } + + newNode = ExpanderEx.prepareCommentBlock( + result[i].html, + result[ i ].thread, + showExpand + ); + + setupAjax( newNode[0], isS1 ); + jQuery("#ljcmtxt" + result[i].thread).replaceWith( newNode ); } } - - if (!result[i].html) { - removeEmptyMarkup(result[i].thread); - } - - newNode = ExpanderEx.prepareCommentBlock( - result[i].html || '', - result[i].thread || '', - showExpand - ); - - setupAjax( newNode[0], isS1 ); - - jQuery("#ljcmtxt" + result[i].thread).replaceWith( newNode ); + hourglass.hide(); + poofAt(pos); + }; + + getThreadJSON(threadId, function(result) { + //if comment is expanded we need to fetch it's collapsed state additionally + if( threadExpanded && LJ_cmtinfo[ threadId ].oldvars.full ) + { + getThreadJSON( threadId, function(result2){ + ExpanderEx.Collection[ threadId ] = ExpanderEx.prepareCommentBlock( result2[0].html, threadId, true ).html(); + populateComments( result ); + }, true, true ); } - } - hourglass.hide(); - poofAt(pos); - }; - - getThreadJSON(threadId, function (result) { - //if comment is expanded we need to fetch it's collapsed state additionally - if( threadExpanded && LJ_cmtinfo[ threadId ].oldvars.full ) - { - getThreadJSON( threadId, function (result2) { - ExpanderEx.Collection[ threadId ] = ExpanderEx.prepareCommentBlock( result2[0].html, threadId, true ).html(); - populateComments( result ); - }, true, true ); + else + populateComments( result ); + }, false, !threadExpanded); } - else - populateComments( result ); - }, false, !threadExpanded); - } - - function handleS2(ids) { - // modified jQuery.fn.load - jQuery.ajax({ - url: location.href, - type: 'GET', - dataType: 'html', - complete: function (res, status) { - // If successful, inject the HTML into all the matched elements - if (status == 'success' || status == 'notmodified') { - // Create a dummy div to hold the results - var nodes = jQuery('<div/>') - // inject the contents of the document in, removing the scripts - // to avoid any 'Permission Denied' errors in IE - .append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, '')) - // Locate the specified elements - .find(ids) - .each(function () { - var id = this.id.replace(/[^0-9]/g, ''); - if (LJ_cmtinfo[id].expanded) { - var expand = this.innerHTML.match(/Expander\.make\(.+?\)/)[0]; - (function(){ - eval(expand); - }).apply(document.createElement('a')); - } else { - jQuery('#' + this.id).replaceWith(this); - setupAjax(this, isS1); - } - }); - hourglass.hide(); - poofAt(pos); - } - } - }); - } - - function checkRcForNoCommentsPage() { - var commsArray = [ dItemid ], ids; - - // check rc for no comments page - if (LJ_cmtinfo[dItemid].rc) { - if (/mode=(un)?freeze/.test(control.href)) { - mapComms(dItemid); - } - ids = '#ljcmt' + commsArray.join(',#ljcmt'); - } else { - var rpcRes; - eval(json); - updateLink(control, rpcRes, control.getElementsByTagName('img')[0]); - // /tools/recent_comments.bml - if (document.getElementById('ljcmtbar'+dItemid)) { - ids = '#ljcmtbar' + dItemid; - } - // ex.: portal/ else { - hourglass.hide(); - poofAt(pos); - return; + // modified jQuery.fn.load + jQuery.ajax({ + url: location.href, + type: 'GET', + dataType: 'html', + complete: function(res, status) { + // If successful, inject the HTML into all the matched elements + if (status == 'success' || status == 'notmodified') { + // Create a dummy div to hold the results + var nodes = jQuery('<div/>') + // inject the contents of the document in, removing the scripts + // to avoid any 'Permission Denied' errors in IE + .append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, '')) + // Locate the specified elements + .find(ids) + .each(function(){ + var id = this.id.replace(/[^0-9]/g, ''); + if (LJ_cmtinfo[id].expanded) { + var expand = this.innerHTML.match(/Expander\.make\(.+?\)/)[0]; + (function(){ + eval(expand); + }).apply(document.createElement('a')); + } else { + jQuery('#'+this.id).replaceWith(this); + setupAjax(this, isS1); + } + }); + hourglass.hide(); + poofAt(pos); + } + } + }); } } - - return ids; - - function mapComms(id) { - var i = -1, newId; - - while (newId = LJ_cmtinfo[id].rc[++i]) { - if (LJ_cmtinfo[newId].full) { - commsArray.push(newId); - mapComms(String(newId)); - } - } - } - } + ); return false; } } -function removeEmptyMarkup(threadId) { - jQuery('#ljcmt' + threadId).remove(); -} - function setupAjax (node, isS1) { var links = node ? node.getElementsByTagName('a') : document.links, rex_id = /id=(\d+)/, @@ -691,7 +550,7 @@ var id = reMatch[1]; if (!document.getElementById('ljcmt' + id)) continue; - ae.onclick = createModerationFunction(ae, id, isS1, 'screen'); + ae.onclick = createModerationFunction(ae, id, isS1); } else if (ae.href.indexOf('delcomment.bml') != -1) { if (LJ_cmtinfo && LJ_cmtinfo.disableInlineDelete) continue; @@ -702,37 +561,27 @@ if (!document.getElementById('ljcmt' + id)) continue; ae.onclick = createDeleteFunction(ae, id, isS1); - } else if (ae.href.indexOf('spamcomment.bml') != -1) { - var reMatch = rex_id.exec(ae.href); - if (!reMatch) continue; - - var id = reMatch[1]; - if (!document.getElementById('ljcmt' + id)) continue; - - ae.onclick = createModerationFunction(ae, id, isS1, 'spam'); - } + } } } function getThreadJSON(threadId, success, getSingle) { var postid = location.href.match(/\/(\d+).html/)[1], - modeParam = LiveJournal.parseGetArgs(location.href).mode, params = [ 'journal=' + Site.currentJournal, 'itemid=' + postid, 'thread=' + threadId, 'depth=' + LJ_cmtinfo[ threadId ].depth ]; - - if (getSingle) + if( getSingle) params.push( 'single=1' ); - - if (modeParam) - params.push( 'mode=' + modeParam ) var url = LiveJournal.getAjaxUrl('get_thread') + '?' + params.join( '&' ); jQuery.get( url, success, 'json' ); } jQuery(function(){setupAjax( false, ("is_s1" in LJ_cmtinfo ) )}); + +DOM.addEventListener(document, 'click', docClicked); +document.write("<style> div.ljcmtmanage { color: #000; background: #e0e0e0; border: 2px solid #000; padding: 3px; }</style>");