From 51bdcb90a94b18bc9b195e6c14e03ebe764a2e51 Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Mon, 1 Oct 2018 23:55:34 +0200 Subject: [PATCH 1/2] Display source lines of code. Will attach number of lines of code near each file in the list. Fixes: https://github.com/plone/plone.app.discussion/issue/999 --- src/inject.js | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/inject.js b/src/inject.js index 0b93749a..6a95f625 100755 --- a/src/inject.js +++ b/src/inject.js @@ -94,6 +94,20 @@ var utils = { [].forEach.call(document.querySelectorAll(selector), function (el) { el.parentNode.removeChild(el); }); + }, + getNumberOfLines: function (data, elemCurrent, fn) { + // Get content path for given file + var result = data.html_url.match(/.*[bt][lr][oe][be]\/[^//]+\/(.*)/); + var contentPath = result && result.length && result[1]; + + apiUtils.getRepoContent( + function (dataFile) { + if (!dataFile) { + return; + } + var numOfLines = atob(dataFile.content).split(/\r\n|\r|\n/).length + ' lines'; + fn(numOfLines, elemCurrent); + }, contentPath); } }; @@ -286,20 +300,27 @@ var domUtils = { } for (var i = 0; i < elems.length; i++) { - if (data[i].type === 'file') { - var formattedFileSize = utils.getFileSizeAndUnit(data[i]); - - var html = '' + - '' + formattedFileSize + '' + - '' + + var dataCurrent = data[i]; + var elemCurrent = elems[i]; + + if (dataCurrent.type === 'file') { + var formattedFileSize = utils.getFileSizeAndUnit(dataCurrent); + + // As this is async function, pass variables to avoid mutation: + utils.getNumberOfLines(dataCurrent, elemCurrent, function (numberOfLines, element) { + var html = '' + + '' + numberOfLines + '' + + '' + formattedFileSize + '' + + '' + '' + - '' + - ''; - elems[i].insertAdjacentHTML('afterend', html); + '' + + ''; + element.insertAdjacentHTML('afterend', html); + }); } else { - elems[i].insertAdjacentHTML('afterend', ''); + elemCurrent.insertAdjacentHTML('afterend', ''); } } }); From e12a428d43659655e2a3fcc40eeb9378c5dc1367 Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Mon, 1 Oct 2018 23:58:30 +0200 Subject: [PATCH 2/2] Display source lines of code. Will attach number of lines of code near each file in the list. Fixes: https://github.com/softvar/enhanced-github/issues/19 --- src/inject.js | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/src/inject.js b/src/inject.js index 6a95f625..0b93749a 100755 --- a/src/inject.js +++ b/src/inject.js @@ -94,20 +94,6 @@ var utils = { [].forEach.call(document.querySelectorAll(selector), function (el) { el.parentNode.removeChild(el); }); - }, - getNumberOfLines: function (data, elemCurrent, fn) { - // Get content path for given file - var result = data.html_url.match(/.*[bt][lr][oe][be]\/[^//]+\/(.*)/); - var contentPath = result && result.length && result[1]; - - apiUtils.getRepoContent( - function (dataFile) { - if (!dataFile) { - return; - } - var numOfLines = atob(dataFile.content).split(/\r\n|\r|\n/).length + ' lines'; - fn(numOfLines, elemCurrent); - }, contentPath); } }; @@ -300,27 +286,20 @@ var domUtils = { } for (var i = 0; i < elems.length; i++) { - var dataCurrent = data[i]; - var elemCurrent = elems[i]; - - if (dataCurrent.type === 'file') { - var formattedFileSize = utils.getFileSizeAndUnit(dataCurrent); - - // As this is async function, pass variables to avoid mutation: - utils.getNumberOfLines(dataCurrent, elemCurrent, function (numberOfLines, element) { - var html = '' + - '' + numberOfLines + '' + - '' + formattedFileSize + '' + - '' + + if (data[i].type === 'file') { + var formattedFileSize = utils.getFileSizeAndUnit(data[i]); + + var html = '' + + '' + formattedFileSize + '' + + '' + '' + - '' + - ''; - element.insertAdjacentHTML('afterend', html); - }); + '' + + ''; + elems[i].insertAdjacentHTML('afterend', html); } else { - elemCurrent.insertAdjacentHTML('afterend', ''); + elems[i].insertAdjacentHTML('afterend', ''); } } });