summaryrefslogtreecommitdiffstats
path: root/public_html/data/js/application.js
blob: 674f680088e8887826c5c0f2c58ed8b300609347 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(function () {
'use strict';
define(
	[
		'require',
		'util',
		'lexer-input',
		'tabwidth-input',
		'thumbnail-view',
		'uploader',
		'tablesorter',
		'jquery',
		'jquery.lazyload',
		'bootstrap',
		'jquery.checkboxes',
	],
	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.setupHistoryPopovers();
				this.setupToggleSelectAllEvent();
				this.setupLineWrapToggle();
				this.setupLazyLoadingImages();
				this.setupTableRangeSelect();
				this.setupAsciinema();
				this.focusLoginFormOnClick();
			},

			setupTableRangeSelect: function () {
				$('#upload_history').checkboxes('range', true);
			},

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

			setupHistoryPopovers: function () {
				$('#upload_history a').popover({
					trigger: 'hover',
					placement: 'bottom',
					html: true,
					viewport: '#upload_history',
					template: '<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><pre class="popover-content"></pre></div>'
				});
			},

			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).lazyload({treshold: 200});
				}
			},

			setupAsciinema: function () {
				_.each($('.asciinema_player'), function (item) {
					item = $(item);
					asciinema.player.js.CreatePlayer(item.attr("id"), item.data("url"));
				});
			},

			focusLoginFormOnClick: function () {
				$('.dropdown-toggle').on('click', function (e) {
					setTimeout(function() {
						$(e.target).parent().find(".dropdown-menu input.form-control").first().focus();
					}, 0);
				});
			},
		};

		return App;
	}
);
})();