151 lines
4.3 KiB
JavaScript
151 lines
4.3 KiB
JavaScript
|
'use strict';
|
||
|
/**
|
||
|
* Created by Martin on 24/02/2016.
|
||
|
*/
|
||
|
|
||
|
$.fn.pressEnter = function (fn) {
|
||
|
|
||
|
return this.each(function () {
|
||
|
$(this).bind('enterPress', fn);
|
||
|
$(this).keyup(function (e) {
|
||
|
if (e.keyCode === 13) {
|
||
|
$(this).trigger('enterPress');
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
(function () {
|
||
|
console.log('GO!');
|
||
|
var $list = $('#listContainer');
|
||
|
var displayList = function (obj) {
|
||
|
var html = new EJS({url: '/partials/list.ejs'}).render(obj);
|
||
|
console.log(html);
|
||
|
$list.empty();
|
||
|
$list.append(html);
|
||
|
$('#listContainer').find('.entry').not('.emptyMessage').click(function () {
|
||
|
console.log('Clicked list. ' + this.id);
|
||
|
getRecipe(this.id);
|
||
|
});
|
||
|
}, displayPage = function (obj) {
|
||
|
var $bodyContents = $('#bodyContents');
|
||
|
|
||
|
if (obj.reduced.length > 0) {
|
||
|
$bodyContents.empty();
|
||
|
$bodyContents.append(obj.reduced);
|
||
|
}
|
||
|
}, getRecipe = function (id) {
|
||
|
console.log('get recipe');
|
||
|
var url = '/entry/' + id;
|
||
|
var data = '';
|
||
|
$.ajax({
|
||
|
type: 'GET',
|
||
|
url: url,
|
||
|
data: data,
|
||
|
dataType: 'json',
|
||
|
|
||
|
timeout: 10000,
|
||
|
|
||
|
//contentType: ('application/json'),
|
||
|
headers: {
|
||
|
'Access-Control-Allow-Origin': '*',
|
||
|
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
|
||
|
'Access-Control-Allow-Headers': 'Content-Type'
|
||
|
|
||
|
},
|
||
|
success: function (data) {
|
||
|
// console.log(data);
|
||
|
displayPage(data);
|
||
|
},
|
||
|
error: function (xhr, type) {
|
||
|
console.log('ajax error');
|
||
|
console.log(xhr);
|
||
|
console.log(type);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
getList = function () {
|
||
|
|
||
|
var url = '/list';
|
||
|
|
||
|
$.ajax({
|
||
|
type: 'GET',
|
||
|
url: url,
|
||
|
data: '',
|
||
|
dataType: 'json',
|
||
|
|
||
|
timeout: 10000,
|
||
|
|
||
|
//contentType: ('application/json'),
|
||
|
headers: {
|
||
|
'Access-Control-Allow-Origin': '*',
|
||
|
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
|
||
|
'Access-Control-Allow-Headers': 'Content-Type'
|
||
|
|
||
|
},
|
||
|
success: function (data) {
|
||
|
// console.log(data);
|
||
|
displayList(data);
|
||
|
},
|
||
|
error: function (xhr, type) {
|
||
|
console.log('ajax error');
|
||
|
console.log(xhr);
|
||
|
console.log(type);
|
||
|
}
|
||
|
});
|
||
|
}, addNew = function (newUrl) {
|
||
|
var url = '/add';
|
||
|
|
||
|
var data = {url: JSON.stringify(newUrl)};
|
||
|
$.ajax({
|
||
|
type: 'POST',
|
||
|
url: url,
|
||
|
data: data,
|
||
|
dataType: 'json',
|
||
|
|
||
|
timeout: 10000,
|
||
|
|
||
|
//contentType: ('application/json'),
|
||
|
headers: {
|
||
|
'Access-Control-Allow-Origin': '*',
|
||
|
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
|
||
|
'Access-Control-Allow-Headers': 'Content-Type'
|
||
|
|
||
|
},
|
||
|
success: function () {
|
||
|
// console.log(data);
|
||
|
// displayList(data);
|
||
|
},
|
||
|
error: function (xhr, type) {
|
||
|
console.log('ajax error');
|
||
|
console.log(xhr);
|
||
|
console.log(type);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
start = function () {
|
||
|
|
||
|
getList();
|
||
|
};
|
||
|
|
||
|
$('#newurl').pressEnter(function () {
|
||
|
var url = $(this).val();
|
||
|
if (url !== null) {
|
||
|
console.log('Adding: ' + url);
|
||
|
addNew(url);
|
||
|
$('#addstatus').fadeIn(400).delay(1500).fadeOut(400);
|
||
|
$(this).val('');
|
||
|
/* setTimeout(function () {
|
||
|
getList();
|
||
|
}, 5000);*/
|
||
|
}
|
||
|
});
|
||
|
|
||
|
$('#fnRefresh').on('click', function () {
|
||
|
getList();
|
||
|
});
|
||
|
|
||
|
start();
|
||
|
})();
|