/** * Yobi, Project Hosting SW * * Copyright 2013 NAVER Corp. * http://yobi.io * * @author Yi EungJun * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ (function(ns){ var oNS = $yobi.createNamespace(ns); oNS.container[oNS.name] = function(htOptions){ var htVar = {}; var htElement = {}; /** * initialize */ function _init(htOptions){ _initVar(htOptions); _initElement(htOptions); _attachEvent(); _render(); _initFileUploader(); _initFileDownloader(); _initToggleCommentsButton(); _initMiniMap(); } /** * initialize variables except element */ function _initVar(htOptions) { htVar.bCommentable = htOptions.bCommentable; htVar.sWatchUrl = htOptions.sWatchUrl; htVar.sUnwatchUrl = htOptions.sUnwatchUrl; htVar.sParentCommitId = htOptions.sParentCommitId; htVar.sCommitId = htOptions.sCommitId; htVar.sCodeURL = htOptions.sCodeURL; htVar.sTplFileURL = htOptions.sTplFileURL; htVar.rxSlashes = /\//g; // 미니맵 htVar.sQueryMiniMap = htOptions.sQueryMiniMap || "li.comment"; htVar.sTplMiniMapLink = ''; // yobi.Attachments htVar.sTplFileItem = $('#tplAttachedFile').text(); } /** * initialize element */ function _initElement(htOptions){ htElement.welUploader = $("#upload"); htElement.welTextarea = $('textarea[data-editor-mode="comment-body"]'); var welHidden = $('').attr('type', 'hidden'); htElement.welDiff = $('#commit'); htElement.welEmptyCommentForm = $('#comment-form') .append(welHidden.clone().attr('name', 'path')) .append(welHidden.clone().attr('name', 'line')) .append(welHidden.clone().attr('name', 'side')); htElement.welComments = $('ul.comments'); if (htVar.bCommentable) { htElement.welIcon = $('#comment-icon-template').tmpl(); } htElement.welEmptyLineNumColumn = $('#linenum-column-template').tmpl(); htElement.welEmptyCommentButton = $('#comment-button-template').tmpl(); htElement.welBtnWatch = $('#watch-button'); htElement.welMiniMap = $("#minimap"); // .minimap-outer htElement.welMiniMapWrap = htElement.welMiniMap.find(".minimap-wrap"); htElement.welMiniMapCurr = htElement.welMiniMapWrap.find(".minimap-curr"); htElement.welMiniMapLinks = htElement.welMiniMapWrap.find(".minimap-links"); } /** * attach event handler */ function _attachEvent(){ htElement.welBtnWatch.click(function(weEvt) { var welTarget = $(weEvt.target); var bWatched = welTarget.hasClass("active"); $yobi.sendForm({ "sURL": bWatched ? htVar.sUnwatchUrl : htVar.sWatchUrl, "fOnLoad": function(){ welTarget.toggleClass("active"); } }); }); $(window).on("resize", _initMiniMap); $(window).on("scroll", _updateMiniMapCurr); $('.diff-wrap').on('click','td.linenum',_onClickLineNumA); $('.diff-wrap').on('click','[data-toggle="commentBoxToggle"]',_onClickCommentBoxToggleBtn); } /** * Render diff and comments */ function _render() { var sDiff = htElement.welDiff.text(); htElement.welDiff.text(""); htElement.welDiff.append(_renderDiff(sDiff)); htElement.welDiff.show(); htElement.welComments.show(); // Show the remain comments if(document.location.hash){ var sTargetId = document.location.hash.substr(1).replace(htVar.rxSlashes, "-"); var welTarget = $(document.getElementById(sTargetId)); if(welTarget.length > 0){ window.scrollTo(0, welTarget.offset().top); } } $('[data-commit-origin="true"]').removeClass("hide"); } /** * initialize fileUploader */ function _initFileUploader(){ var oUploader = yobi.Files.getUploader(htElement.welUploader, htElement.welTextarea); if(oUploader){ (new yobi.Attachments({ "elContainer" : htElement.welUploader, "elTextarea" : htElement.welTextarea, "sTplFileItem" : htVar.sTplFileItem, "sUploaderId" : oUploader.attr("data-namespace") })); } } /** * initialize fileDownloader */ function _initFileDownloader(){ $(".attachments").each(function(i, elContainer){ if(!$(elContainer).data("isYobiAttachment")){ (new yobi.Attachments({"elContainer": elContainer})); } }); } /** * initialize toggle comments button */ function _initToggleCommentsButton() { $('#toggle-comments').on('click',function() { $('.diff-container').toggleClass('show-comments'); $("#minimap").toggle(); }); } /** * @param {Object} welTr */ function _appendCommentThreadOnLine(welTr,sPath) { var welUl = _createCommentThreadOnLine(welTr,sPath); if (welUl.children().length > 0) { return _appendCommentToggle(welTr, welUl); } } /** * @param {Object} welTr * @param {Object} welUl */ function _appendCommentToggle(welTr, welUl) { var welTd = $('') .data("line", welTr.data("line")) .data("side", welTr.data("side")) .data("path", welTr.data("path")); if (htVar.bCommentable) { var welCommentBoxToggleButton = htElement.welEmptyCommentButton.clone() .text(Messages("code.openCommentBox")) .attr('data-toggle','commentBoxToggle') .attr('data-type','open'); welUl.append(welCommentBoxToggleButton); } return $('',{class:'comments board-comment-wrap'}) .append($('').append(welUl)); } function _onClickCommentBoxToggleBtn(weEvt) { var welCommentTr = $(this).closest('tr'); var welCodeTr = welCommentTr.prev('tr'); var welPath = welCodeTr.closest('table'); var sType = $(this).data('type'); if(sType=='open') { _showCommentBox(welCommentTr,welPath.data('filePath'),welCodeTr.data('line'),welCodeTr.data('type')); $(this).data('type','close').text(Messages("code.closeCommentBox")); } else { _hideCommentBox(); $(this).data('type','open').text(Messages("code.openCommentBox")); } } function _hideCommentBox() { htElement.welCommentTr.remove(); htElement.welEmptyCommentForm.find('[name=path]').removeAttr('value'); htElement.welEmptyCommentForm.find('[name=line]').removeAttr('value'); htElement.welEmptyCommentForm.find('[name=side]').removeAttr('value'); htElement.welComments.after(htElement.welEmptyCommentForm); _updateMiniMap(); } /** * @param {Event} weEvt */ function _onClickLineNumA(weEvt) { var commentForm = $(weEvt.target).closest('tr').next().find('#comment-form'); if (commentForm.length > 0) { _hideCommentBox(); } else { var welRow = $(this).closest('tr'); var welPath = welRow.closest('table'); if(welRow.data('type')=='add' || welRow.data('type')=='context' || welRow.data('type')=='remove') { _showCommentBox(welRow, welPath.data('filePath'), welRow.data('line'),welRow.data('type')); } } } /** * @param {Object} welTr * @param {Object} welUl */ function _createCommentThreadOnLine(welTr,sPath) { var waComments = htElement.welComments.children('li.comment'); var welUl = $('