1
0
mirror of https://github.com/owncloud/bookmarks.git synced 2024-12-11 12:24:10 +01:00
OwncloudBookmarksOfficial/js/bookmarks.js

472 lines
12 KiB
JavaScript
Raw Normal View History

var bookmarksPage = 0;
var bookmarksLoading = false;
2012-08-13 21:58:46 +02:00
var dialog;
var bookmarksSorting = 'bookmarks_sorting_recent';
var fullTags = [];
var ajaxCallCount = 0;
var allTagsCollection;
var availableTagsCollection;
var selectedTagsCollection;
var availableTagsList;
var selectedTagsList;
$(document).ready(function () {
allTagsCollection = new OCA.Bookmarks.TagCollection();
availableTagsCollection = new OCA.Bookmarks.TagCollection();
selectedTagsCollection = new OCA.Bookmarks.TagCollection();
availableTagsList = new OCA.Bookmarks.TagListView({
model: availableTagsCollection,
id: 'availableTagsList'
});
selectedTagsList = new OCA.Bookmarks.TagListView({
model: selectedTagsCollection,
id: 'selectedTagsList'
});
getTags();
watchUrlField();
$('#bm_import').change(attachSettingEvent);
$('#add_url').on('keydown keyup change click', watchUrlField);
$('#app-settings').on('click keydown', toggleSettings);
$('#bm_export').click(exportBm);
$('#emptycontent-setting').click(function () {
if (!$('#app-settings').hasClass('open')) {
$('#app-settings').click();
}
});
$('.bookmarks_list').scroll(updateOnBottom).empty();
2012-06-28 11:40:42 +02:00
$('#tag_filter input').tagit({
allowSpaces: true,
availableTags: fullTags,
onTagFinishRemoved: filterTagsChanged,
2014-06-12 18:20:16 +02:00
placeholderText: t('bookmarks', 'Filter by tag')
2012-06-28 11:40:42 +02:00
}).tagit('option', 'onTagAdded', filterTagsChanged);
2015-12-17 23:48:12 +01:00
$('.navigationAllBookmarks').click(resetTagFilter);
2011-08-15 22:05:07 +02:00
getBookmarks();
});
function getTags() {
jQuery.ajax({
url: 'tag',
success: function (result) {
fullTags = result;
console.warn(fullTags);
},
async: false
});
}
2012-07-31 22:48:11 +02:00
var formatString = (function () {
var replacer = function (context) {
return function (s, name) {
2012-07-31 22:48:11 +02:00
return context[name];
};
};
return function (input, context) {
2012-07-31 22:48:11 +02:00
return input.replace(/\{(\w+)\}/g, replacer(context));
};
})();
function increaseAjaxCallCount() {
ajaxCallCount++;
if (ajaxCallCount - 1 === 0) {
updateLoadingAnimation();
}
}
function decreaseAjaxCallCount() {
if (ajaxCallCount > 0) {
ajaxCallCount--;
updateLoadingAnimation();
}
}
function updateLoadingAnimation() {
if (ajaxCallCount === 0) {
$('#bookmark_add_submit').removeClass('icon-loading-small');
$('#bookmark_add_submit').addClass('icon-add');
} else {
$('#bookmark_add_submit').removeClass('icon-add');
$('#bookmark_add_submit').addClass('icon-loading-small');
}
}
function watchClickInSetting(e) {
if ($('#app-settings').find($(e.target)).length === 0) {
toggleSettings();
}
}
function checkURL(url) {
if (url.substring(0, 3) === "htt") {
return url;
}
return "http://" + url;
}
function toggleSettings() {
if ($('#app-settings').hasClass('open')) { //Close
$('#app-settings').switchClass("open", "");
$('body').unbind('click', watchClickInSetting);
}
else {
$('#app-settings').switchClass("", "open");
$('body').bind('click', watchClickInSetting);
}
}
2012-06-29 14:12:29 +02:00
function addFilterTag(event) {
event.preventDefault();
$('#tag_filter input').tagit('createTag', $(this).text());
2012-06-28 11:40:42 +02:00
}
2015-12-17 23:48:12 +01:00
function resetTagFilter() {
$('#tag_filter input').tagit('removeAll');
}
2012-06-28 00:43:11 +02:00
function updateTagsList(tag) {
2015-12-17 23:48:12 +01:00
var selectedTags = $('#tag_filter input').tagit('assignedTags');
var html = tmpl("tag_tmpl", tag);
$('.tag_list').append(html);
2015-12-17 23:48:12 +01:00
$('.tag_list li').each(function(){
var tagName = $(this).find('a span').text();
var inArrayResult = $.inArray(tagName, selectedTags);
// console.warn(inArrayResult);
// console.warn(tagName);
// console.log(this);
2015-12-17 23:48:12 +01:00
if(inArrayResult > -1) {
$(this).addClass('active');
}
});
return;
var $entry = $('.navigationTagTemplate')
.clone();
$entry
.removeClass('hidden')
.removeClass('navigationTagTemplate')
.addClass('navigationTagEntry');
$entry.find('a').text(tag.tag);
$entry.find('.count').text(tag.nbr);
$entry.appendTo($('#navigation-list'));
2012-06-28 00:43:11 +02:00
}
2012-06-28 11:40:42 +02:00
function filterTagsChanged()
{
$('#bookmarkFilterTag').val($('#tag_filter input').val());
2012-06-28 11:40:42 +02:00
$('.bookmarks_list').empty();
bookmarksPage = 0;
2012-06-28 11:40:42 +02:00
getBookmarks();
}
2011-08-15 22:05:07 +02:00
function getBookmarks() {
if (bookmarksLoading) {
2011-08-15 22:05:07 +02:00
//have patience :)
return;
}
increaseAjaxCallCount();
bookmarksLoading = true;
//Update Rel Tags if first page
if (bookmarksPage === 0) {
$.ajax({
type: 'GET',
url: 'bookmark',
data: {type: 'rel_tags', tag: $('#bookmarkFilterTag').val(), page: bookmarksPage, sort: bookmarksSorting},
success: function (tags) {
$('.tag_list').empty();
for (var i in tags.data) {
updateTagsList(tags.data[i]);
}
$('.tag_list .tag_edit').click(renameTag);
$('.tag_list .tag_delete').click(deleteTag);
2015-12-17 23:48:12 +01:00
$('.navigationTagEntry a').click(addFilterTag);
2015-12-17 23:48:12 +01:00
$('.with-menu a').bind('click', addFilterTag);
}
});
}
2011-08-15 22:05:07 +02:00
$.ajax({
type: 'GET',
url: 'bookmark',
data: {type: 'bookmark', tag: $('#bookmarkFilterTag').val(), page: bookmarksPage, sort: bookmarksSorting},
complete: function () {
decreaseAjaxCallCount();
},
success: function (bookmarks) {
if (bookmarks.data.length) {
bookmarksPage += 1;
}
2011-08-15 22:05:07 +02:00
$('.bookmark_link').unbind('click', recordClick);
$('.bookmark_delete').unbind('click', delBookmark);
2012-06-29 14:12:29 +02:00
$('.bookmark_edit').unbind('click', editBookmark);
for (var i in bookmarks.data) {
2011-08-15 22:05:07 +02:00
updateBookmarksList(bookmarks.data[i]);
}
checkEmpty();
2011-08-15 22:05:07 +02:00
$('.bookmark_link').click(recordClick);
$('.bookmark_delete').click(delBookmark);
2012-06-29 14:12:29 +02:00
$('.bookmark_edit').click(editBookmark);
bookmarksLoading = false;
if (bookmarks.data.length) {
updateOnBottom();
}
2011-08-15 22:05:07 +02:00
}
});
2011-08-15 22:05:07 +02:00
}
function watchUrlField() {
var form = $('#add_form');
var el = $('#add_url');
var button = $('#bookmark_add_submit');
form.unbind('submit');
if (!acceptUrl(el.val())) {
form.bind('submit', function (e) {
e.preventDefault();
});
button.addClass('disabled');
}
else {
button.removeClass('disabled');
form.bind('submit', addBookmark);
}
}
function acceptUrl(url) {
return url.replace(/^\s+/g, '').replace(/\s+$/g, '') !== '';
}
2012-06-29 14:12:29 +02:00
function addBookmark(event) {
event.preventDefault();
var url = $('#add_url').val();
//If trim is empty
if (!acceptUrl(url)) {
return;
}
2012-08-13 21:58:46 +02:00
$('#add_url').val('');
var bookmark = {url: url, description: '', title: '', from_own: 0, added_date: new Date()};
increaseAjaxCallCount();
2012-08-13 21:58:46 +02:00
$.ajax({
type: 'POST',
url: 'bookmark',
data: bookmark,
complete: function () {
decreaseAjaxCallCount();
},
success: function (data) {
if (data.status === 'success') {
// First remove old BM if exists
$('.bookmark_single').filterAttr('data-id', data.item.id).remove();
var bookmark = $.extend({}, bookmark, data.item);
updateBookmarksList(bookmark, 'prepend');
checkEmpty();
watchUrlField();
2012-08-13 21:58:46 +02:00
}
}
});
2011-08-15 22:05:07 +02:00
}
function delBookmark() {
var record = $(this).parent().parent();
OC.dialogs.confirm(t('bookmarks', 'Are you sure you want to remove this bookmark?'),
t('bookmarks', 'Warning'), function (answer) {
if (answer) {
$.ajax({
type: 'DELETE',
url: 'bookmark/' + record.data('id'),
success: function (data) {
if (data.status === 'success') {
record.remove();
checkEmpty();
}
}
});
}
2011-08-15 22:05:07 +02:00
});
}
function checkEmpty() {
if ($('.bookmarks_list').children().length === 0) {
$("#emptycontent").show();
$("#bm_export").addClass('disabled');
2013-02-07 00:33:47 +01:00
$('.bookmarks_list').hide();
} else {
$("#emptycontent").hide();
$("#bm_export").removeClass('disabled');
2013-02-07 00:33:47 +01:00
$('.bookmarks_list').show();
}
}
function editBookmark() {
if ($('.bookmark_single_form').length) {
2012-09-02 17:53:33 +02:00
$('.bookmark_single_form .reset').click();
}
2011-09-13 13:08:11 +02:00
var record = $(this).parent().parent();
var bookmark = record.data('record');
var html = tmpl("item_form_tmpl", bookmark);
record.after(html);
record.hide();
var rec_form = record.next().find('form');
rec_form.find('.bookmark_form_tags ul').tagit({
allowSpaces: true,
availableTags: fullTags,
placeholderText: t('bookmarks', 'Tags')
});
rec_form.find('.reset').bind('click', cancelBookmark);
rec_form.bind('submit', function (event) {
event.preventDefault();
var form_values = $(this).serialize();
$.ajax({
type: 'PUT',
url: $(this).attr('action') + "/" + this.elements['record_id'].value,
data: form_values,
success: function (data) {
if (data.status === 'success') {
//@TODO : do better reaction than reloading the page
filterTagsChanged();
} else { // On failure
//@TODO : show error message?
}
}
});
});
2011-09-13 13:08:11 +02:00
}
function cancelBookmark(event) {
event.preventDefault();
var rec_form = $(this).closest('form').parent();
rec_form.prev().show();
rec_form.remove();
}
2012-10-11 21:45:05 +02:00
function updateBookmarksList(bookmark, position) {
position = typeof position !== 'undefined' ? position : 'append';
bookmark = $.extend({title: '', description: '', added_date: new Date('now'), tags: []}, bookmark);
var tags = bookmark.tags;
var taglist = '';
for (var i = 0, len = tags.length; i < len; ++i) {
if (tags[i] !== '')
taglist = taglist + '<a class="bookmark_tag" href="#">' + escapeHTML(tags[i]) + '</a> ';
2011-08-15 22:05:07 +02:00
}
if (!hasProtocol(bookmark.url)) {
bookmark.url = 'http://' + bookmark.url;
}
if (bookmark.added) {
bookmark.added_date.setTime(parseInt(bookmark.added) * 1000);
}
if (!bookmark.title)
bookmark.title = '';
var html = tmpl("item_tmpl", bookmark);
if (position === "prepend") {
$('.bookmarks_list').prepend(html);
} else {
$('.bookmarks_list').append(html);
}
var line = $('div[data-id="' + bookmark.id + '"]');
line.data('record', bookmark);
if (taglist !== '') {
line.append('<p class="bookmark_tags">' + taglist + '</p>');
2012-02-01 22:02:06 +01:00
}
line.find('a.bookmark_tag').bind('click', addFilterTag);
line.find('.bookmark_link').click(recordClick);
line.find('.bookmark_delete').click(delBookmark);
line.find('.bookmark_edit').click(editBookmark);
2011-08-15 22:05:07 +02:00
}
function updateOnBottom() {
//check wether user is on bottom of the page
var top = $('.bookmarks_list>:last-child').position().top;
var height = $('.bookmarks_list').height();
// use a bit of margin to begin loading before we are really at the
// bottom
if (top < height * 1.2) {
2012-07-01 23:08:41 +02:00
getBookmarks();
2011-08-15 22:05:07 +02:00
}
}
function recordClick() {
2011-08-15 22:05:07 +02:00
$.ajax({
2012-06-10 23:38:26 +02:00
type: 'POST',
url: 'bookmark/click',
data: 'url=' + encodeURIComponent($(this).attr('href'))
});
2011-08-15 22:05:07 +02:00
}
function hasProtocol(url) {
var regexp = /(ftp|http|https|sftp)/;
return regexp.test(url);
}
function renameTag() {
if ($('input[name="tag_new_name"]').length)
return; // Do nothing if a tag is currenlty edited
var tagElement = $(this).closest('li');
tagElement.append('<form><input name="tag_new_name" type="text"></form>');
var form = tagElement.find('form');
//tag_el.find('.tags_actions').hide();
var tagName = tagElement.find('.tag').hide().text();
tagElement.find('input').val(tagName).focus().bind('blur', function () {
form.trigger('submit');
});
form.bind('submit', submitTagName);
}
function submitTagName(event) {
event.preventDefault();
var tagElement = $(this).closest('li');
var newTagName = tagElement.find('input').val();
var oldTagName = tagElement.find('.tag').show().text();
//tag_el.find('.tag_edit').show();
//tag_el.find('.tags_actions').show();
tagElement.find('input').unbind('blur');
tagElement.find('form').unbind('submit').remove();
if (newTagName !== oldTagName && newTagName !== '') {
//submit
$.ajax({
type: 'POST',
url: 'tag',
data: {old_name: oldTagName, new_name: newTagName},
success: function (bookmarks) {
if (bookmarks.status === 'success') {
filterTagsChanged();
}
}
});
}
}
function deleteTag() {
var tag_el = $(this).closest('li');
var old_tag_name = tag_el.find('.tag').show().text();
2012-08-13 19:24:34 +02:00
OC.dialogs.confirm(t('bookmarks', 'Are you sure you want to remove this tag from every entry?'),
t('bookmarks', 'Warning'), function (answer) {
if (answer) {
$.ajax({
type: 'DELETE',
url: 'tag',
data: {old_name: old_tag_name},
success: function (bookmarks) {
if (bookmarks.status === 'success') {
filterTagsChanged();
}
}
});
}
});
2012-07-31 22:48:11 +02:00
}