recipes/app/js/app.js

150 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-02-24 11:48:09 +00:00
/**
* Created by Martin on 24/02/2016.
*/
2016-02-24 13:31:29 +00:00
$.fn.pressEnter = function (fn) {
return this.each(function () {
$(this).bind('enterPress', fn);
$(this).keyup(function (e) {
if (e.keyCode == 13) {
$(this).trigger("enterPress");
}
})
});
};
2016-02-24 11:48:09 +00:00
(function () {
"use strict";
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 .entry").not('.emptyMessage').click(function () {
console.log('Clicked list. ' + this.id);
getRecipe(this.id);
});
}, displayPage = function (obj) {
var $bodyContents = $('#bodyContents');
if (obj.list[0].body.length > 0) {
$bodyContents.empty();
$bodyContents.append(obj.list[0].body);
}
}, getRecipe = function (id) {
console.log('get recipe');
var url = '/recipes/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) {
2016-02-24 13:31:29 +00:00
// console.log(data);
2016-02-24 11:48:09 +00:00
displayPage(data);
},
error: function (xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
},
getList = function () {
var url = '/recipes/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) {
2016-02-24 13:31:29 +00:00
// console.log(data);
2016-02-24 11:48:09 +00:00
displayList(data);
},
error: function (xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
}, addNew = function (newUrl) {
var url = '/recipes/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 (data) {
2016-02-24 13:31:29 +00:00
// console.log(data);
2016-02-24 11:48:09 +00:00
// displayList(data);
},
error: function (xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
},
start = function () {
getList();
};
2016-02-24 13:31:29 +00:00
$('#newurl').pressEnter(function () {
var url = $(this).val();
2016-02-24 11:48:09 +00:00
if (url != null) {
console.log('Adding: ' + url);
addNew(url);
2016-02-24 13:31:29 +00:00
$('#addstatus').fadeIn(400).delay(1500).fadeOut(400);
$(this).val('');
setTimeout((function () {
getList();
}), 5000);
2016-02-24 11:48:09 +00:00
}
});
2016-02-24 13:31:29 +00:00
$('#fnRefresh').on('click', function () {
getList();
});
2016-02-24 11:48:09 +00:00
start();
})();