Skip to content

Commit

Permalink
Task #27 Removing goto method. White-space adjustment in template-fun…
Browse files Browse the repository at this point in the history
…ctions.php
  • Loading branch information
jesgs authored and ardathksheyna committed Jul 23, 2017
1 parent 38b71aa commit 9ace02d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 84 deletions.
220 changes: 136 additions & 84 deletions assets/js/bookmark.js
@@ -1,84 +1,136 @@
(function ($) {
if (typeof(localStorage) != 'object') {
console.log('Browser does not support LocalStorage');
return;
}

$(function() {
BookMark.init();
// KISS
$('#bookmark-comic').on('click', function (e) {
e.preventDefault();

// store date, page title, and URL
});

$('#show-comic-bookmark-history').on('click', function (e) {
// show a list of recently bookmarked comics, starting with most recent
});

});

var BookMark = {
storage : null,
BOOKMARK_HISTORY : 'mangapress-bookmark-history',
BOOKMARK : 'mangapress-bookmark',
$bookmark : null,
init: function() {
this.storage = localStorage;
this.checkItem();
},

checkItem : function() {
var bookmark = JSON.parse(this.storage.getItem(this.BOOKMARK)),
pageHref = window.location.href;

this.$bookmark = $('#bookmark-comic');

if (bookmark == null) {
return;
}

if (bookmark.url == pageHref) {
this.$bookmark.text( this.$bookmark.data('bookmarkedLabel') );
}
},

bookmark : function() {
var href = window.location.href,
pageTitle = window.document.title,
data = {};

var existingBookmarkData = this.storage.getItem(this.BOOKMARK);

if (existingBookmarkData) {
var bookmarkHistory = JSON.parse(this.storage.getItem(this.BOOKMARK_HISTORY));
if (!bookmarkHistory) {
bookmarkHistory = [];
}

bookmarkHistory.push(existingBookmarkData);
this.storage.setItem(this.BOOKMARK_HISTORY, JSON.stringify(bookmarkHistory));

// change label state
this.$bookmark.text( this.$bookmark.data('bookmarkedLabel') );
}

data = {
url : href,
title : pageTitle,
date : Date.now()
};

this.storage.setItem(this.BOOKMARK, JSON.stringify(data));
},

history : function() {

},

goto : function() {

}
};
}(jQuery));
(function ($) {
if (typeof(localStorage) != 'object') {
console.log('Browser does not support LocalStorage');
return;
}

$(function() {
Bookmark.init();
// KISS
$('#bookmark-comic').on('click', function (e) {
e.preventDefault();

// store date, page title, and URL
Bookmark.bookmark();
});

$('#bookmark-comic-history').on('click', function (e) {
// show a list of recently bookmarked comics, starting with most recent
e.preventDefault();
Bookmark.history();
});

});

var Bookmark = {
storage : null,
BOOKMARK_HISTORY : 'mangapress-bookmark-history',
BOOKMARK : 'mangapress-bookmark',
$bookmark : null,
$bookmarkNav : null,
$bookmarkHistory : null,
init: function() {
this.storage = localStorage;
this.checkItem();

var bookmarkHistory = JSON.parse(this.storage.getItem(this.BOOKMARK_HISTORY));
if (!bookmarkHistory) {
bookmarkHistory = [];
this.storage.setItem(this.BOOKMARK_HISTORY, JSON.stringify(bookmarkHistory));
}
},

checkItem : function() {
var bookmark = JSON.parse(this.storage.getItem(this.BOOKMARK)),
pageHref = window.location.href;

this.$bookmark = $('#bookmark-comic');
this.$bookmarkNav = $('#comic-bookmark-navigation');

if (bookmark !== null && bookmark.url == pageHref) {
this.$bookmark.text( this.$bookmark.data('bookmarkedLabel') );
}
},

bookmark : function() {
var href = (this.$bookmark.data('href') !== undefined) ? this.$bookmark.data('href') : window.location.href,
pageTitle = (this.$bookmark.data('title') !== undefined) ? this.$bookmark.data('title') : window.document.title,
data = {};

var existingBookmarkData = this.storage.getItem(this.BOOKMARK);

if (existingBookmarkData) {
var bookmarkHistory = JSON.parse(this.storage.getItem(this.BOOKMARK_HISTORY));
if (!bookmarkHistory) {
bookmarkHistory = [];
}

bookmarkHistory.push(existingBookmarkData);
this.storage.setItem(this.BOOKMARK_HISTORY, JSON.stringify(bookmarkHistory));

// change label state
this.$bookmark.text( this.$bookmark.data('bookmarkedLabel') );
}

data = {
url : href,
title : pageTitle,
date : Date.now()
};

this.storage.setItem(this.BOOKMARK, JSON.stringify(data));
},

history : function() {
var self = this,
revBookmarkHistory = JSON.parse(self.storage.getItem(self.BOOKMARK_HISTORY));

if (revBookmarkHistory.length == 0) {
// No bookmarks available
return;
}

var bookmarkHistory = revBookmarkHistory.reverse(),
$historyModal = $('<div id="bookmark-history-modal"><div id="bookmark-history-content"></div><p>[<a href="#" id="bookmark-history-close">close</a>]</p></div>')
.css({
'width': '300px',
'z-index' : 9999,
'border' : '1px solid black',
'background-color' : '#fff',
'position' : 'absolute',
'padding' : '5px',
'left' : '50%',
'margin-left' : '-150px'
});

$historyModal.find('#bookmark-history-content').html(function(){
var htmlString = "<table>";

htmlString = "<thead><tr><td>Title</td><td>Date</td></tr></thead>";

for (var i = 0; i < bookmarkHistory.length; i++) {
var columns = [],
bookmark = JSON.parse(bookmarkHistory[i]),
d = new Date(bookmark.date),
date = d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear(),
link = "<a href=\"" + bookmark.url + "\">" + bookmark.title + "</a>";

columns.push(link, date);

htmlString += "<tr><td>" + columns.join('</td><td>') + "</td></tr>"
}

return htmlString + "</table>";
});

// append
self.$bookmarkNav.append($historyModal)

// add event for closing modal
$('#bookmark-history-close').on('click', function(e){
e.preventDefault();
$historyModal.remove();
});
}
};
}(jQuery));
1 change: 1 addition & 0 deletions includes/template-functions.php
Expand Up @@ -13,6 +13,7 @@

/**
* Bookmark button template tag
*
* @todo Add l10/i18n functionality
* @todo Define $attrs parameters
* @param array $attrs Attributes
Expand Down

0 comments on commit 9ace02d

Please sign in to comment.