Commits

Anonymous committed e2b691b

Simple file layout change, no code.

Comments (0)

Files changed (1)

chrome/content/gst.list.js

-GST.sortedList = function() {
-	let sortList, posList;
-	clearLists();
-	
-	function clearLists() {
-		sortList = []; posList = [];
-		sortList.groupProp = "gstSortGroup";
-		posList.groupProp = "gstPosGroup";
+(function() {
+let sortList, posList;
+clearLists();
+
+function clearLists() {
+	sortList = []; posList = [];
+	sortList.groupProp = "gstSortGroup";
+	posList.groupProp = "gstPosGroup";
+}
+
+function getList() {
+	if (GST.isSortBetween) return posList;
+	if (GST.isSortGroups) return sortList;
+	throw "Sort is something other than 'between' and 'groups'";
+}
+
+function getSort() { return GST.groupBy.getSort() || GST.sortBy.getSort(); }
+function getSortWithin() { return GST.sortWithinBy.getSort(); }
+
+function getListLen(list) {
+	let len = 0;
+	for (let i = 0; i < list.length; i++)
+		len += list[i].length;
+	return len;
+}
+
+function print(list, sortableProp) {
+	function getTabIdx(tab) {
+		for (let i = 0; i < gBrowser.tabs.length; i++)
+			if (gBrowser.tabs[i] == tab) return i;
+		return null;
 	}
 	
-	function getList() {
-		if (GST.isSortBetween) return posList;
-		if (GST.isSortGroups) return sortList;
-		throw "Sort is something other than 'between' and 'groups'";
+	let output = sortableProp + "\n";
+	for (let i = 0; i < list.length; i++) {
+		for (let j = 0; j < list[i].length; j++) {
+			let tab = list[i][j];
+			output += tab[sortableProp] + " " + tab._tPos + ", ";
+			GST.assert(tab._tPos === getTabIdx(tab), "_tPos isn't accurate.");
+		}
+		output += "\n";
+	}
+	GST.log(output + "Len -- " + getListLen(list));
+}
+
+function positionTab(tab, list) {
+	let group = tab[list.groupProp];
+	let idx = 0;
+	for (let i = 0; i < list.length; i++) {
+		if (list[i] === group) { idx += group.indexOf(tab); break; }
+		else idx += list[i].length;
+	}
+	gBrowser.moveTabTo(tab, idx);
+	GST.assert(idx === tab._tPos, "Move unsuccessful. " + idx + " " + tab._tPos);
+}
+
+function doSortList(list, sort) {
+	let sortProp = GST.props[sort];
+	list.sort(compareFunction);
+	
+	function compareFunction(a, b) {
+		if (a === b) throw "list has duplicates";
+		if (a instanceof Array) { a = a[0]; b = b[0]; }
+		if (a[sortProp] < b[sortProp]) return -1;
+		if (a[sortProp] > b[sortProp]) return 1;
+		if (a._tPos < b._tPos) return -1;
+		if (a._tPos > b._tPos) return 1;
+		throw "two tabs have equivalent _tPos";
+	}
+}
+
+function addTab(tab) {
+	removeTab(tab, sortList);
+
+	let group = null;
+	let groupByProp = GST.groupBy.getProp(tab);
+	for (let i = 0; i < sortList.length; i++) {
+		let sameGroup = (sortList[i][0][groupByProp] === tab[groupByProp]);
+		if (sameGroup) { group = sortList[i]; break; }
 	}
 	
-	function getSort() { return GST.groupBy.getSort() || GST.sortBy.getSort(); }
-	function getSortWithin() { return GST.sortWithinBy.getSort(); }
-	
-	function getListLen(list) {
-		let len = 0;
-		for (let i = 0; i < list.length; i++)
-			len += list[i].length;
-		return len;
+	if (group) {
+		group.push(tab);
+		doSortList(group, getSortWithin());
+	} else {
+		group = [tab];
+		sortList.push(group);
+		doSortList(sortList, getSort());
+	}
+	tab.gstSortGroup = group;
+}
+
+function addPosTab(tab) {
+	function getMatchingTab(tab) {
+		GST.assert(tab.gstSortGroup, "missing gstSortGroup; addTab() not called before addPosTab()?");
+		GST.assert(tab.gstSortGroup.length > 0, "zero-length sort group");
+		if (tab.gstSortGroup.length === 1) return null;
+		for (let i = 0; i < tab.gstSortGroup.length; i++) {
+			let similarTab = tab.gstSortGroup[i];
+			if (similarTab !== tab) {
+				if (similarTab.gstPosGroup)
+					return similarTab;
+			}
+		}
+		return null;
 	}
 	
-	function print(list, sortableProp) {
-		function getTabIdx(tab) {
-			for (let i = 0; i < gBrowser.tabs.length; i++)
-				if (gBrowser.tabs[i] == tab) return i;
-			return null;
-		}
+	function splice(matchGroup) {
+		let leftGroup, rightGroup;
+		let matchTabIdx = matchGroup.indexOf(matchTab);
+		let midGroup = matchGroup.splice(matchTabIdx, 1);
+		if (matchTabIdx < matchGroup.length)	// remember our length is now 1 less
+			rightGroup = matchGroup.splice(matchTabIdx);
+		if (matchTabIdx > 0)
+			leftGroup = matchGroup;
+
+		matchTab.gstPosGroup = midGroup;
+		midGroup.gstIsUniform = true;
 		
-		let output = sortableProp + "\n";
-		for (let i = 0; i < list.length; i++) {
-			for (let j = 0; j < list[i].length; j++) {
-				let tab = list[i][j];
-				output += tab[sortableProp] + " " + tab._tPos + ", ";
-				GST.assert(tab._tPos === getTabIdx(tab), "_tPos isn't accurate.");
-			}
-			output += "\n";
-		}
-		GST.log(output + "Len -- " + getListLen(list));
+		let matchGroupIdx = posList.indexOf(matchGroup);
+		posList.splice(matchGroupIdx, 1, midGroup);
+		if (rightGroup) posList.splice(matchGroupIdx + 1, 0, rightGroup);
+		if (leftGroup) posList.splice(matchGroupIdx, 0, leftGroup);
+		return midGroup;
+	}
+
+	let group;
+	let matchTab = getMatchingTab(tab);
+	if (matchTab) {
+		let matchGroup = matchTab.gstPosGroup;
+		if (!matchGroup.gstIsUniform)
+			group = splice(matchGroup);
+		else if (tab.gstPosGroup !== matchGroup)
+			group = matchGroup;
+	} else if (!tab.gstPosGroup) {
+		let lastGroup;
+		if (posList.length > 0)
+			lastGroup = posList[posList.length - 1];
+		if (!lastGroup || lastGroup.gstIsUniform) {
+			group = [];
+			posList.push(group);
+		} else
+			group = lastGroup;
 	}
 	
-	function positionTab(tab, list) {
-		let group = tab[list.groupProp];
-		let idx = 0;
-		for (let i = 0; i < list.length; i++) {
-			if (list[i] === group) { idx += group.indexOf(tab); break; }
-			else idx += list[i].length;
-		}
-		gBrowser.moveTabTo(tab, idx);
-		GST.assert(idx === tab._tPos, "Move unsuccessful. " + idx + " " + tab._tPos);
+	let lenBefore = getListLen(posList);
+	if (group) {
+		let toBeSingle = (tab.gstPosGroup && tab.gstPosGroup.length === 2);
+		if (toBeSingle)	tab.gstPosGroup.gstIsUniform = false;
+		removeTab(tab, posList);
+		
+		tab.gstPosGroup = group;
+		tab.gstPosGroup.push(tab);
 	}
-	
-	function doSortList(list, sort) {
-		let sortProp = GST.props[sort];
-		list.sort(compareFunction);
-		
-		function compareFunction(a, b) {
-			if (a === b) throw "list has duplicates";
-			if (a instanceof Array) { a = a[0]; b = b[0]; }
-			if (a[sortProp] < b[sortProp]) return -1;
-			if (a[sortProp] > b[sortProp]) return 1;
-			if (a._tPos < b._tPos) return -1;
-			if (a._tPos > b._tPos) return 1;
-			throw "two tabs have equivalent _tPos";
+	doSortList(tab.gstPosGroup, GST.sortBy.getSort());
+	let lenAfter = getListLen(posList);
+	GST.assert(lenAfter >= lenBefore, "Net fewer tabs after addPosTab()");
+}
+
+function removeTab(tab, list) {
+	let group = tab[list.groupProp];
+	if (!group) return;
+	let idx = group.indexOf(tab);
+	group.splice(idx, 1);
+	if (group.length === 0)
+		list.splice(list.indexOf(group), 1);
+}
+
+function positionCheck(list) {
+	let idx = 0;
+	for (let i = 0; i < list.length; i++) {
+		for (let j = 0; j < list[i].length; j++) {
+			let tab = list[i][j];
+			if (tab._tPos !== idx) {
+				//GST.halt = true;
+				GST.assert(tab._tPos === idx, "Position check failure: " + idx + " " + tab._tPos);
+			}
+			idx++;
 		}
 	}
+}
+
+GST.sortedList = {
+	update : function(tab, sortable, value) {
+		GST.assert(GST.ssTabsOpened, "Tab update occurring before restoring tabs have finished opening.");
+		//if (GST.halt) return;
+		let sortableProp = GST.props[sortable]
+		tab[sortableProp] = value;
+		// save in session store
+		GST.ss.setTabValue(tab, sortableProp, value);
+	
+		addTab(tab);
+		addPosTab(tab);
+		let relevantToSort = (sortable === getSort() || sortable === getSortWithin());
+		if (!relevantToSort) return;
+	
+		let list = getList();
+		positionTab(tab, list);
+		print(list, sortableProp);
+		GST.assert(getListLen(getList()) === gBrowser.tabs.length, "List isn't correct length."); 
+	},
+	
+	removeTab : function(tab) {
+		removeTab(tab, sortList);
+		removeTab(tab, posList);
+	},
+	
+	report : function() {
+		GST.log("-------- REPORT --------");
+		print(getList(), GST.props[getSort()]);
+		print(getList(), GST.props[GST.isSortBetween ? GST.sortBy.getSort() : GST.sortWithinBy.getSort()]);
+		GST.log("-------- END REPORT --------");
+		GST.assert(getListLen(getList()) === gBrowser.tabs.length, "List isn't correct length."); 
+	},
+	
+	fullSort : function() {
+		clearLists();
 		
-	function addTab(tab) {
-		removeTab(tab, sortList);
-
-		let group = null;
-		let groupByProp = GST.groupBy.getProp(tab);
-		for (let i = 0; i < sortList.length; i++) {
-			let sameGroup = (sortList[i][0][groupByProp] === tab[groupByProp]);
-			if (sameGroup) { group = sortList[i]; break; }
+		let queue = [];
+		for (let i = 0; i < gBrowser.tabs.length; i++) {
+			let tab = gBrowser.tabs[i];
+			tab.gstSortGroup = null;
+			tab.gstPosGroup = null;
+			addTab(tab)
+			queue.push(tab);
 		}
-		
-		if (group) {
-			group.push(tab);
-			doSortList(group, getSortWithin());
-		} else {
-			group = [tab];
-			sortList.push(group);
-			doSortList(sortList, getSort());
+	
+		let list = getList();
+		for (let i = 0; i < queue.length; i++) {
+			let tab = queue[i];
+			addPosTab(tab);
+			positionTab(tab, list);
 		}
-		tab.gstSortGroup = group;
+		this.report();
+		positionCheck(list);
 	}
-	
-	function addPosTab(tab) {
-		function getMatchingTab(tab) {
-			GST.assert(tab.gstSortGroup, "missing gstSortGroup; addTab() not called before addPosTab()?");
-			GST.assert(tab.gstSortGroup.length > 0, "zero-length sort group");
-			if (tab.gstSortGroup.length === 1) return null;
-			for (let i = 0; i < tab.gstSortGroup.length; i++) {
-				let similarTab = tab.gstSortGroup[i];
-				if (similarTab !== tab) {
-					if (similarTab.gstPosGroup)
-						return similarTab;
-				}
-			}
-			return null;
-		}
-		
-		function splice(matchGroup) {
-			let leftGroup, rightGroup;
-			let matchTabIdx = matchGroup.indexOf(matchTab);
-			let midGroup = matchGroup.splice(matchTabIdx, 1);
-			if (matchTabIdx < matchGroup.length)	// remember our length is now 1 less
-				rightGroup = matchGroup.splice(matchTabIdx);
-			if (matchTabIdx > 0)
-				leftGroup = matchGroup;
-
-			matchTab.gstPosGroup = midGroup;
-			midGroup.gstIsUniform = true;
-			
-			let matchGroupIdx = posList.indexOf(matchGroup);
-			posList.splice(matchGroupIdx, 1, midGroup);
-			if (rightGroup) posList.splice(matchGroupIdx + 1, 0, rightGroup);
-			if (leftGroup) posList.splice(matchGroupIdx, 0, leftGroup);
-			return midGroup;
-		}
-
-		let group;
-		let matchTab = getMatchingTab(tab);
-		if (matchTab) {
-			let matchGroup = matchTab.gstPosGroup;
-			if (!matchGroup.gstIsUniform)
-				group = splice(matchGroup);
-			else if (tab.gstPosGroup !== matchGroup)
-				group = matchGroup;
-		} else if (!tab.gstPosGroup) {
-			let lastGroup;
-			if (posList.length > 0)
-				lastGroup = posList[posList.length - 1];
-			if (!lastGroup || lastGroup.gstIsUniform) {
-				group = [];
-				posList.push(group);
-			} else
-				group = lastGroup;
-		}
-		
-		let lenBefore = getListLen(posList);
-		if (group) {
-			let toBeSingle = (tab.gstPosGroup && tab.gstPosGroup.length === 2);
-			if (toBeSingle)	tab.gstPosGroup.gstIsUniform = false;
-			removeTab(tab, posList);
-			
-			tab.gstPosGroup = group;
-			tab.gstPosGroup.push(tab);
-		}
-		doSortList(tab.gstPosGroup, GST.sortBy.getSort());
-		let lenAfter = getListLen(posList);
-		GST.assert(lenAfter >= lenBefore, "Net fewer tabs after addPosTab()");
-	}
-	
-	function removeTab(tab, list) {
-		let group = tab[list.groupProp];
-		if (!group) return;
-		let idx = group.indexOf(tab);
-		group.splice(idx, 1);
-		if (group.length === 0)
-			list.splice(list.indexOf(group), 1);
-	}
-	
-	function positionCheck(list) {
-		let idx = 0;
-		for (let i = 0; i < list.length; i++) {
-			for (let j = 0; j < list[i].length; j++) {
-				let tab = list[i][j];
-				if (tab._tPos !== idx) {
-					//GST.halt = true;
-					GST.assert(tab._tPos === idx, "Position check failure: " + idx + " " + tab._tPos);
-				}
-				idx++;
-			}
-		}
-	}
-	
-	let that = {
-		update : function(tab, sortable, value) {
-			GST.assert(GST.ssTabsOpened, "Tab update occurring before restoring tabs have finished opening.");
-			//if (GST.halt) return;
-			let sortableProp = GST.props[sortable]
-			tab[sortableProp] = value;
-			// save in session store
-			GST.ss.setTabValue(tab, sortableProp, value);
-
-			addTab(tab);
-			addPosTab(tab);
-			let relevantToSort = (sortable === getSort() || sortable === getSortWithin());
-			if (!relevantToSort) return;
-
-			let list = getList();
-			positionTab(tab, list);
-			print(list, sortableProp);
-			GST.assert(getListLen(getList()) === gBrowser.tabs.length, "List isn't correct length."); 
-		},
-		
-		removeTab : function(tab) {
-			removeTab(tab, sortList);
-			removeTab(tab, posList);
-		},
-		
-		report : function() {
-			GST.log("-------- REPORT --------");
-			print(getList(), GST.props[getSort()]);
-			print(getList(), GST.props[GST.isSortBetween ? GST.sortBy.getSort() : GST.sortWithinBy.getSort()]);
-			GST.log("-------- END REPORT --------");
-			GST.assert(getListLen(getList()) === gBrowser.tabs.length, "List isn't correct length."); 
-		},
-		
-		fullSort : function() {
-			clearLists();
-			
-			let queue = [];
-			for (let i = 0; i < gBrowser.tabs.length; i++) {
-				let tab = gBrowser.tabs[i];
-				tab.gstSortGroup = null;
-				tab.gstPosGroup = null;
-				addTab(tab)
-				queue.push(tab);
-			}
-
-			let list = getList();
-			for (let i = 0; i < queue.length; i++) {
-				let tab = queue[i];
-				addPosTab(tab);
-				positionTab(tab, list);
-			}
-			this.report();
-			positionCheck(list);
-		}
-	};
-	return that;
-}();
+};
+}());