Skip to contentSkip to conversation

gitweb: Split gitweb.js, improve JavaScript

PATCH In review Jakub Narebski 2 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover gitweb: Split gitweb.js, improve JavaScript

This patch series is inspired by patch by John 'Warthog9' Hawley

[PATCH 1/1] gitweb: javascript ability to adjust time based on timezone
Message-Id: <[email protected]>
http://thread.gmane.org/gmane.comp.version-control.git/169384/focus=169882

that added a few new JavaScript files for the new feature that this patch implemented.

This is marked as RFC mainly because other patches in series, especially the last one fixing bug in gitweb's JavaScript code, should be send independently on gitweb.js splitting.

Jakub Narebski (3):
gitweb: Split JavaScript for maintability; concatenate on build
gitweb: Update and improve comments in JavaScript files
gitweb: Fix parsing of negative fractional timezones in JavaScript

 .gitignore                                         |    1 +
 gitweb/Makefile                                    |   12 +-
 .../static/{gitweb.js => js/blame_incremental.js}  |  216 ++------------------
 gitweb/static/js/common-lib.js                     |  191 +++++++++++++++++
 gitweb/static/js/detect-javascript.js              |   44 ++++
 5 files changed, 261 insertions(+), 203 deletions(-)
 rename gitweb/static/{gitweb.js => js/blame_incremental.js} (75%)
 create mode 100644 gitweb/static/js/common-lib.js
 create mode 100644 gitweb/static/js/detect-javascript.js
Signature · 1 line
1.7.3
#1 lore raw

PATCH 1/3 gitweb: Split JavaScript for maintability; concatenate on build

Split single gitweb.js file into smaller files, each dealing with single thing. This should make gitweb's JavaScript code easier to maintain.

For better webapp performance it is recommended[1][2][3] to combine JavaScript files. Do it during build time (in gitweb/Makefile), by straight concatenation of files into gitweb.js file (which is now ignored as being generated). This means that there are no changes to gitweb script itself - it still uses gitweb.js or gitweb.min.js, but now generated.

[1]: http://developer.yahoo.com/performance/rules.html
"Minimize HTTP Requests" section
[2]: http://code.google.com/speed/articles/include-scripts-properly.html
"1. Combine external JavaScript files"
[3]: http://javascript-reference.info/speed-up-your-javascript-load-time.htm
"Combine Your Files" section.

---
 .gitignore                                         |    1 +
 gitweb/Makefile                                    |   12 +-
 .../static/{gitweb.js => js/blame_incremental.js}  |  196 +-------------------
 gitweb/static/js/common-lib.js                     |  173 +++++++++++++++++
 gitweb/static/js/detect-javascript.js              |   44 +++++
 5 files changed, 233 insertions(+), 193 deletions(-)
 rename gitweb/static/{gitweb.js => js/blame_incremental.js} (77%)
 create mode 100644 gitweb/static/js/common-lib.js
 create mode 100644 gitweb/static/js/detect-javascript.js
.gitignore +1 -0
index c460c66..601c71a 100644
@@ -160,6 +160,7 @@
 /gitk-git/gitk-wish
 /gitweb/GITWEB-BUILD-OPTIONS
 /gitweb/gitweb.cgi
+/gitweb/static/gitweb.js
 /gitweb/static/gitweb.min.*
 /test-chmtime
 /test-ctype
gitweb/Makefile +11 -1
index 0a6ac00..230b1b9 100644
@@ -86,7 +86,7 @@ ifndef V
 endif
 endif
 
-all:: gitweb.cgi
+all:: gitweb.cgi static/gitweb.js
 
 GITWEB_PROGRAMS = gitweb.cgi
 
@@ -112,6 +112,11 @@ endif
 
 GITWEB_FILES += static/git-logo.png static/git-favicon.png
 
+GITWEB_JSLIB_FILES += static/js/common-lib.js
+GITWEB_JSLIB_FILES += static/js/detect-javascript.js
+GITWEB_JSLIB_FILES += static/js/blame_incremental.js
+
+
 GITWEB_REPLACE = \
 	-e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \
 	-e 's|++GIT_BINDIR++|$(bindir)|g' \
@@ -146,6 +151,11 @@ gitweb.cgi: gitweb.perl GITWEB-BUILD-OPTIONS
 	chmod +x $@+ && \
 	mv $@+ $@
 
+static/gitweb.js: $(GITWEB_JSLIB_FILES)
+	$(QUIET_GEN)$(RM) $@ $@+ && \
+	cat $^ >$@+ && \
+	mv $@+ $@
+
 ### Testing rules
 
 test:
gitweb/static/js/blame_incremental.js renamed from gitweb/static/gitweb.js +4 -192

221 lines not on this page · raw message

gitweb/static/js/common-lib.js new file +173 -0

173 lines not on this page · raw message

gitweb/static/js/detect-javascript.js new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
1.7.3
#2 lore raw

PATCH 2/3 gitweb: Update and improve comments in JavaScript files

In reply to Jakub Narebski

Spell 'neighbor' using American spelling (and not 'neighbour').

---
 gitweb/static/js/blame_incremental.js |   22 +++++++++++-----------
 gitweb/static/js/common-lib.js        |   12 ++++++++----
 2 files changed, 19 insertions(+), 15 deletions(-)
gitweb/static/js/blame_incremental.js +11 -11
index 3317390..2067dcd 100644
@@ -8,7 +8,7 @@
  * @license GPLv2 or later
  */
 
-
+/* ============================================================ */
 /*
  * This code uses DOM methods instead of (nonstandard) innerHTML
  * to modify page.
@@ -27,7 +27,7 @@
  */
 
 
-/* ============================================================ */
+/* ............................................................ */
 /* utility/helper functions (and variables) */
 
 var xhr;        // XMLHttpRequest object
@@ -133,7 +133,7 @@ function writeTimeInterval() {
 }
 
 /**
- * show an error message alert to user within page (in prohress info area)
+ * show an error message alert to user within page (in progress info area)
  * @param {String} str: plain text error message (no HTML)
  *
  * @globals div_progress_info
@@ -183,7 +183,7 @@ function getColorNo(tr) {
 
 var colorsFreq = [0, 0, 0];
 /**
- * return one of given possible colors (curently least used one)
+ * return one of given possible colors (currently least used one)
  * example: chooseColorNoFrom(2, 3) returns 2 or 3
  *
  * @param {Number[]} arguments: one or more numbers
@@ -204,8 +204,8 @@ function chooseColorNoFrom() {
 }
 
 /**
- * given two neigbour <tr> elements, find color which would be different
- * from color of both of neighbours; used to 3-color blame table
+ * given two neighbor <tr> elements, find color which would be different
+ * from color of both of neighbors; used to 3-color blame table
  *
  * @param {HTMLElement} tr_prev
  * @param {HTMLElement} tr_next
@@ -217,14 +217,14 @@ function findColorNo(tr_prev, tr_next) {
 	var color_next = getColorNo(tr_next);
 
 
-	// neither of neighbours has color set
+	// neither of neighbors has color set
 	// THEN we can use any of 3 possible colors
 	if (!color_prev && !color_next) {
 		return chooseColorNoFrom(1,2,3);
 	}
 
-	// either both neighbours have the same color,
-	// or only one of neighbours have color set
+	// either both neighbors have the same color,
+	// or only one of neighbors have color set
 	// THEN we can use any color except given
 	var color;
 	if (color_prev === color_next) {
@@ -238,7 +238,7 @@ function findColorNo(tr_prev, tr_next) {
 		return chooseColorNoFrom((color % 3) + 1, ((color+1) % 3) + 1);
 	}
 
-	// neighbours have different colors
+	// neighbors have different colors
 	// THEN there is only one color left
 	return (3 - ((color_prev + color_next) % 3));
 }
@@ -259,7 +259,7 @@ function isStartOfGroup(tr) {
 
 /**
  * change colors to use zebra coloring (2 colors) instead of 3 colors
- * concatenate neighbour commit groups belonging to the same commit
+ * concatenate neighbor commit groups belonging to the same commit
  *
  * @globals colorRe
  */
gitweb/static/js/common-lib.js +8 -4
index 566c98a..33887d7 100644
@@ -13,14 +13,17 @@
 /* Padding */
 
 /**
- * pad number N with nonbreakable spaces on the left, to WIDTH characters
+ * pad INPUT on the left with STR that is assumed to have visible
+ * width of single character (for example nonbreakable spaces),
+ * to WIDTH characters
+ *
  * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
  *          ('\u00A0' is nonbreakable space)
  *
  * @param {Number|String} input: number to pad
  * @param {Number} width: visible width of output
  * @param {String} str: string to prefix to string, e.g. '\u00A0'
- * @returns {String} INPUT prefixed with (WIDTH - INPUT.length) x STR
+ * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
  */
 function padLeftStr(input, width, str) {
 	var prefix = '';
@@ -34,7 +37,7 @@ function padLeftStr(input, width, str) {
 }
 
 /**
- * Pad INPUT on the left to SIZE width, using given padding character CH,
+ * Pad INPUT on the left to WIDTH, using given padding character CH,
  * for example padLeft('a', 3, '_') is '__a'.
  *
  * @param {String} input: input value converted to string.
@@ -126,7 +129,8 @@ var maybeQuotedRe = /^\"(.*)\"$/;
 /**#@-*/
 
 /**
- * unquote maybe git-quoted filename
+ * unquote maybe C-quoted filename (as used by git, i.e. it is
+ * in double quotes '"' if there is any escape character used)
  * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a	a'
  *
  * @param {String} str: git-quoted string
Signature · 1 line
1.7.3
#3 lore raw

PATCH 3/3 gitweb: Fix parsing of negative fractional timezones in JavaScript

In reply to Jakub Narebski

Extract converting numerical timezone in the form of '(+|-)HHMM' to timezoneOffset function, and fix parsing of negative fractional timezones.

This was used to format timestamps in 'blame_incremental' view.

---
 gitweb/static/js/common-lib.js |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)
gitweb/static/js/common-lib.js +19 -5
index 33887d7..cc72d1e 100644
@@ -87,7 +87,24 @@ function createRequestObject() {
  * used to extract hours and minutes from timezone info, e.g '-0900'
  * @constant
  */
-var tzRe = /^([+-][0-9][0-9])([0-9][0-9])$/;
+var tzRe = /^([-+])([0-9][0-9])([0-9][0-9])$/;
+
+/**
+ * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
+ *
+ * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
+ * @returns {Number} offset from UTC in seconds for timezone
+ *
+ * @globals tzRe
+ */
+function timezoneOffset(timezoneInfo) {
+	var match = tzRe.exec(timezoneInfo);
+	var tz_sign = (match[1] === '-' ? -1 : +1);
+	var tz_hour = parseInt(match[2],10);
+	var tz_min  = parseInt(match[3],10);
+
+	return tz_sign*(((tz_hour*60) + tz_min)*60);
+}
 
 /**
  * return date in local time formatted in iso-8601 like format
@@ -96,14 +113,11 @@ var tzRe = /^([+-][0-9][0-9])([0-9][0-9])$/;
  * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
  * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
  * @returns {String} date in local time in iso-8601 like format
- *
- * @globals tzRe
  */
 function formatDateISOLocal(epoch, timezoneInfo) {
-	var match = tzRe.exec(timezoneInfo);
 	// date corrected by timezone
 	var localDate = new Date(1000 * (epoch +
-		(parseInt(match[1],10)*3600 + parseInt(match[2],10)*60)));
+		timezoneOffset(timezoneInfo)));
 	var localDateStr = // e.g. '2005-08-07'
 		localDate.getUTCFullYear()                 + '-' +
 		padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
Signature · 1 line
1.7.3
#4 lore raw
In reply to Jakub Narebski

The only thing I would comment on is that we may want to document (for the build process) a way to force a certain ordering of various files. Case in point we probably want to force the things like various global defs (what I'm proposing in the timezone stuff) at the top and let the function defs go below that.

Other than that this all looks good.

- John 'Warthog9' Hawley

Quoted 28 lines · Jakub Narebski

This patch series is inspired by patch by John 'Warthog9' Hawley

[PATCH 1/1] gitweb: javascript ability to adjust time based on timezone
Message-Id: <[email protected]>
http://thread.gmane.org/gmane.comp.version-control.git/169384/focus=169882

that added a few new JavaScript files for the new feature that this patch implemented.

This is marked as RFC mainly because other patches in series, especially the last one fixing bug in gitweb's JavaScript code, should be send independently on gitweb.js splitting.

Jakub Narebski (3):
gitweb: Split JavaScript for maintability; concatenate on build
gitweb: Update and improve comments in JavaScript files
gitweb: Fix parsing of negative fractional timezones in JavaScript

 .gitignore                                         |    1 +
 gitweb/Makefile                                    |   12 +-
 .../static/{gitweb.js => js/blame_incremental.js}  |  216 ++------------------
 gitweb/static/js/common-lib.js                     |  191 +++++++++++++++++
 gitweb/static/js/detect-javascript.js              |   44 ++++
 5 files changed, 261 insertions(+), 203 deletions(-)
 rename gitweb/static/{gitweb.js => js/blame_incremental.js} (75%)
 create mode 100644 gitweb/static/js/common-lib.js
 create mode 100644 gitweb/static/js/detect-javascript.js
#5 lore raw
Quoted 5 lines · J.H.

The only thing I would comment on is that we may want to document (for the build process) a way to force a certain ordering of various files. Case in point we probably want to force the things like various global defs (what I'm proposing in the timezone stuff) at the top and let the function defs go below that.

Actually I don't think that matters, at least not if JavaScript files contain only variables and functions, and do not include any code that is actually run when file is loaded. A very simple test (below) confirms that.

In JavaScript variable declarations ('var' keyword) and function definitions ('function' keyword) get "hoisted" at beginning.

Quoted 1 line

Other than that this all looks good.

Thanks.

-- 8< --
<html>
<head>
<title>JavaScript test</title>
<script>
// -------------------------------------------
// B

var varB = 'varB';

function fooB() {
fooA();
alert('varB='+varB + '; varA='+varA);
return 'fooB';
}

// -------------------------------------------
// A

var varA = 'varA';

function fooA() {
alert('varA='+varA);
return 'fooA';
}

// ===========================================
// main
fooB();
</script>
</head>
<body>
test
</body>
</html>
-- >8 --

Signature · 2 lines
Jakub Narebski
Poland