blob: f3db039b9cc50c62bead697d945c3de00bd426e5 [file] [log] [blame]
<!--
Copyright (C) 2016 The Android Open Source Project
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.
-->
<script src="../../scripts/util.js"></script>
<script>
(function(window) {
'use strict';
window.Gerrit = window.Gerrit || {};
/** @polymerBehavior Gerrit.PathListBehavior */
Gerrit.PathListBehavior = {
COMMIT_MESSAGE_PATH: '/COMMIT_MSG',
MERGE_LIST_PATH: '/MERGE_LIST',
/**
* @param {string} a
* @param {string} b
* @return {number}
*/
specialFilePathCompare(a, b) {
// The commit message always goes first.
if (a === Gerrit.PathListBehavior.COMMIT_MESSAGE_PATH) {
return -1;
}
if (b === Gerrit.PathListBehavior.COMMIT_MESSAGE_PATH) {
return 1;
}
// The merge list always comes next.
if (a === Gerrit.PathListBehavior.MERGE_LIST_PATH) {
return -1;
}
if (b === Gerrit.PathListBehavior.MERGE_LIST_PATH) {
return 1;
}
const aLastDotIndex = a.lastIndexOf('.');
const aExt = a.substr(aLastDotIndex + 1);
const aFile = a.substr(0, aLastDotIndex) || a;
const bLastDotIndex = b.lastIndexOf('.');
const bExt = b.substr(bLastDotIndex + 1);
const bFile = b.substr(0, bLastDotIndex) || b;
// Sort header files above others with the same base name.
const headerExts = ['h', 'hxx', 'hpp'];
if (aFile.length > 0 && aFile === bFile) {
if (headerExts.includes(aExt) && headerExts.includes(bExt)) {
return a.localeCompare(b);
}
if (headerExts.includes(aExt)) {
return -1;
}
if (headerExts.includes(bExt)) {
return 1;
}
}
return aFile.localeCompare(bFile) || a.localeCompare(b);
},
computeDisplayPath(path) {
if (path === Gerrit.PathListBehavior.COMMIT_MESSAGE_PATH) {
return 'Commit message';
} else if (path === Gerrit.PathListBehavior.MERGE_LIST_PATH) {
return 'Merge list';
}
return path;
},
computeTruncatedPath(path) {
return Gerrit.PathListBehavior.truncatePath(
Gerrit.PathListBehavior.computeDisplayPath(path));
},
/**
* Truncates URLs to display filename only
* Example
* // returns '.../text.html'
* util.truncatePath.('dir/text.html');
* Example
* // returns 'text.html'
* util.truncatePath.('text.html');
* @return {string} Returns the truncated value of a URL.
*/
truncatePath(path) {
const pathPieces = path.split('/');
if (pathPieces.length < 2) {
return path;
}
// Character is an ellipsis.
return '\u2026/' + pathPieces[pathPieces.length - 1];
},
};
})(window);
</script>