summaryrefslogtreecommitdiffstats
path: root/static/functions/preview_paranoia.js
blob: 9f5318f3324371d35f751427303b148d85a25516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(function() {
	// Used to get user ID from URL.
	function getURLParameter(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
	}
	// Attach click event on document ready.
	$(function(){
		$('a#preview_paranoia').click(function(event) {
			event.preventDefault();
			var paranoia = {};
			// Build an object of unchecked (hidden, not allowed for others to see) paranoias.
			// We "abuse" object keys to implement sets in JavaScript. This is simpler and
			// more memory efficient than building a string and working through that each time.
			$('input[name^="p_"]').each(function() {
				if (!$(this).prop('checked')) {
					var attribute = $(this).attr('name').replace(/^p_/,'');
					if (/_c$/.test(attribute)) {
						paranoia[attribute.replace(/_.$/,'') + '+'] = 1;
					} else if (/_l$/.test(attribute)) {
						if (typeof paranoia[attribute.replace(/_.$/,'') + '+'] == "undefined") {
							paranoia[attribute.replace(/_.$/,'')] = 1;
						}
					} else {
						paranoia[attribute] = 1;
					}
				}
			});
			// Build into a comma-delimited string.
			var paranoiaString = "";
			for (var key in paranoia) {
				if (key === 'length' || !paranoia.hasOwnProperty(key)) {
					continue;
				}
				paranoiaString += key+',';
			}
			// Get rid of trailing comma.
			paranoiaString = paranoiaString.substring(0, paranoiaString.length - 1);
			// Get user ID from URL parameter.
			var userId = getURLParameter("userid");
			// Open a new tab with specified paranoia settings.
			window.open('user.php?id=' + encodeURIComponent(userId) + '&preview=1&paranoia=' + encodeURIComponent(paranoiaString), '_blank');
		});
	});
})();