summaryrefslogtreecommitdiffstats
path: root/data/js/application.js
blob: 310362dd9b0d250565090f87c045cf5282a8c348 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function () {
'use strict';
define(
	[
		'require',
		'util',
		'lexer-input',
		'tabwidth-input',
		'thumbnail-view',
		'uploader',
		'tablesorter',
		'jquery',
		'jquery.lazyload',
		'bootstrap'
	],
	function (
		require,
		Util,
		LexerInput,
		TabwidthInput,
		ThumbnailView,
		Uploader,
		TableSorter,
		$
	) {
		var ui = {
			lazyLoadingImages: 'img.lazyload'
		};

		var App = {
			 // Gets called for every request (before page load)
			initialize: function () {
				this.setupLineHighlight();
			},

			/*
			 * Gets called for every request after page load
			 * config contains app config attributes passed from php
			 */
			onPageLoaded: function () {
				Util.highlightLineFromHash();
				Util.setTabwidthFromLocalStorage();
				TabwidthInput.initialize();
				LexerInput.initialize();
				ThumbnailView.initialize();
				Uploader.initialize();
				TableSorter.initialize();
				this.configureTooltips();
				this.setupToggleSelectAllEvent();
				this.setupLineWrapToggle();
				this.setupLazyLoadingImages();
			},

			setupLineHighlight: function () {
				$(window).on('hashchange', Util.highlightLineFromHash);
			},
			
			configureTooltips: function () {
				$('[rel="tooltip"]').tooltip({
					placement: 'bottom',
					container: 'body',
				});
			},

			setupToggleSelectAllEvent: function () {
				$('#history-all').on('click', function(event) {
					// Suppress click event on table heading
					event.stopImmediatePropagation();
				});
				$('#history-all').on('change', function(event) {
					var checked = $(event.target).prop('checked');
					$('.delete-history').prop('checked', checked);
				});
			},

			setupLineWrapToggle: function () {
				var linesWrapped = localStorage.getItem('lines_wrapped') || 'true';
				Util.setLineWrap(linesWrapped === 'true');
				
				$('.linewrap-toggle').on('click', _.bind(Util.toggleLineWrap, Util));
			},

			setupLazyLoadingImages: function () {
				if ($(ui.lazyLoadingImages).length > 0) {
					$(ui.lazyLoadingImages).show().lazyload({treshold: 200});
				}
			}
		};

		return App;
	}
);
})();