'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'); } }); }); }; var Keeper = (function () { var self = this; 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 () { getBookmark(this.id); }); }, displayPage = function (obj) { var $bodyContents = $('#bodyContents'); if (obj.reduced.length > 0) { var vdata = { data: { _id: obj._id, _rev: obj._rev, title: obj.title, reduced: obj.reduced, tags: obj.tags, url: obj.url } }; var redoData = { _id: obj._id, _rev: obj._rev, url: obj.url }; var html = new EJS({url: 'partials/view.ejs'}).render(vdata); $bodyContents.empty(); $bodyContents.scrollTop(0); $bodyContents.append(html); $('#redo').on('click', function () { self.trigger('redo', redoData); }); $('#tagSave').on('click', function () { self.trigger('tagsave', redoData); }); $('#tageditmode').on('click', function () { self.trigger('startTags'); }); } }, getBookmark = function (id) { //console.log('getBookmark'); 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); } }); }, updateMainTagList = function (obj) { var $taglist = $('#tagList'); var html = new EJS({url: '/partials/taglist.ejs'}).render(obj); $taglist.empty(); $taglist.html(html); }, getMainTagList = function () { var url = '/tags'; $.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); updateMainTagList(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); } }); }, saveTags = function (d) { function parse(input) { return input .trim() .split(/\s*,\s*/) .map(function (col) { return col.trim(); }) .filter(function (col) { return col && col.length; }); } var data = d; var url = '/tags'; var tags = {}, tagItems = $('#edittags').val(); tagItems = parse(tagItems); tags.list = tagItems; tags.solid = tagItems.join(', '); data.tags = tags; $.ajax({ type: 'POST', url: url, data: d, 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); $('#visualTabs').toggle(); $('#tagForm').toggle(); }, 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); } }); }, redo = function (d) { console.log('redooing'); var url = '/redo'; $.ajax({ type: 'POST', url: url, data: d, 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(); getMainTagList(); }, listFromTag = function (tag) { console.log('get for ' + tag); var url = '/tags/' + tag; $.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); } }); }; $('#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(); }); $(window).on('hashchange', ()=> { self.trigger('hashchange', location.hash); }); this.bind('redo', function (data) { redo(data); }); this.bind('tagsave', function (data) { saveTags(data); }); this.bind('startTags', function (data) { $('#visualTabs').toggle(); $('#tagForm').toggle(); }); this.bind('hashchange', function (data) { var tagroute = data.replace('#?', ''); if (tagroute !== '') { listFromTag(tagroute); } else { getList(); } }); start(); }) ; MicroEvent.mixin(Keeper); var keeper = new Keeper();