summaryrefslogtreecommitdiff
path: root/static/js/app.js
blob: 4abf21441a4b47d6dd17e968a3cc112b1a8272e1 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var timeInterval;
var isInterval = false;
scribble_history = [];
unshift = 0;

function autosave() {

	// If autosave is set, it saves every 3 seconds and everytime memo looses focus
	if (localStorage.getItem("autosave") == "true"){
		if ( ! isInterval ) {
			timeInterval = setInterval( function(){
				save("#memo", "");
			}, 6000);
			isInterval = true;
		}
	}
};

function registerAutosave(id){
	$(id).addClass("btn-danger");
	$(id).html('<span class="glyphicon glyphicon-remove-circle"></span> Disable Autosave');
	localStorage.setItem("autosave", "true");
	$("#post").blur( function(){ save("#memo", "") } );
	autosave();
}

// Function to save memo
function save(id, manually){
	$.ajax({
		url: "/api/new",
		data: $(id).serialize(),
		dataType: "json",
		type: "POST",
		
		success: function(response){
			if ( response != null && response.success){
				addHistory(response);
				if (manually === "manually"){
					if (window.location.pathname != "/" + response.data.Title) {
						window.location.href = response.data.Url + "/../../"+ response.data.Title;
					}
					swal("Saved!", "Your notice was successfully saved!", "success");
				} else if (manually === "view") {
					window.location.href = response.data.Url;
				}
			} else {
				swal({
					title: "Saving failed",
					text: "There was an error. The website says: \""+response.data.error + "\"",
					type: "warning",
					showCancelButton: true,
					confirmButtonClass: "btn-danger",
					confirmButtonText: "Retry",
					closeOnConfirm: false,
				},
				function(){save(id, manually);})
			}
		},
	})
};

/*
function search(){
	$.ajax({
		url: "/api/get",
		data: $('#search').serialize(),
		dataType: "json",
		type: "GET",

		success: function(r){
			if(r.success == 200){
				window.location.href = r.data.Url;
			} else {
				window.location.href = $('#search').val();
			}
		}

		error: function(r){
			console.log("Not found");
			window.location.href = $('#search').val();
		}
	})
};
*/

$(document).ready(function(){

	if(!localStorage["history"]){
		localStorage["history"] = JSON.stringify(scribble_history);
	}
	scribble_history = JSON.parse(localStorage["history"]);

	for(var i = 0; i<scribble_history.length; i++){
		$('#history').append(scribble_history[i].Value);
	}
	/*
	if(unshift === 1){
		// Create sidebar entry
		var url = $('#root-anchor').data("url");
		var title = $('#root-anchor').data("title");
		var newTab = '<li><a href="'+url+'"><span class="number">1</span> '+title+'</a></li>';
		scribble_history.unshift({
			Key: title,
			Value: newTab
		});
	} 
	*/

	// Register click handler on save button to avoid full page refresh
	$("#save").click( function(e){
		save("#memo", "manually");
		e.preventDefault();
	});
	$(".btn-disabled").click( function(e){
		e.preventDefault();
	});

	if( localStorage.getItem("autosave") == "true"){
		registerAutosave("#autosave");
	};

	$('#autosave').click(function() {
		$(this).toggleClass("btn-danger").promise().done(function(){
			if ( $(this).html().match(/Enable/) ){
				registerAutosave(this);
			} else {
				$(this).html('<span class="glyphicon glyphicon-floppy-open"></span> Enable Autosave');
				localStorage.setItem("autosave", "false")
				clearInterval(timeInterval);
				isInterval = false;
				$("#post").off("blur");
			}
		});
	});

	$('#view').click(function(e) {
		e.preventDefault();
		save("#memo", "view");
	});

	/*
	$('#search').click(function(e){
		e.preventDefault();
		search();
	});
	*/

});

function addHistory(response) {

	// Do not allow first save on page

	// Do not allow duplicates
	for(i=0;i<scribble_history.length;i++){
		if (scribble_history[i].Key == response.data.Title){
			return;
		}
	}

	// Create sidebar entry
	var url = response.data.Url.replace("/view", "");
	var l = scribble_history.length +1;
	var newTab = '<li><a href="'+url+'"><span class="number">'+l+'</span> '+response.data.Title+'</a></li>';

	// Push to array
	scribble_history.push({
		Key: response.data.Title,
		Value: newTab 
	});

	//Save in memory
	localStorage["history"] = JSON.stringify(scribble_history);

	// Append to sidebar
	if( $('#root-anchor').data('title') != response.data.Title) {
		$('#history').append(newTab);
	}
};