bookmarks.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var bookmarks_page = 0;
  2. var bookmarks_loading = false;
  3. var bookmarks_sorting = 'bookmarks_sorting_recent';
  4. $(document).ready(function() {
  5. $('#bookmark_add_submit').click(addOrEditBookmark);
  6. $(window).resize(function () {
  7. fillWindow($('.bookmarks_list'));
  8. });
  9. $(window).resize();
  10. $('.bookmarks_list').scroll(updateOnBottom).empty().width($('#content').width());
  11. getBookmarks();
  12. });
  13. function getBookmarks() {
  14. if(bookmarks_loading) {
  15. //have patience :)
  16. return;
  17. }
  18. $.ajax({
  19. url: OC.filePath('bookmarks', 'ajax', 'updateList.php'),
  20. data: 'tag=' + encodeURIComponent($('#bookmarkFilterTag').val()) + '&page=' + bookmarks_page + '&sort=' + bookmarks_sorting,
  21. success: function(bookmarks){
  22. if (bookmarks.data.length) {
  23. bookmarks_page += 1;
  24. }
  25. $('.bookmark_link').unbind('click', recordClick);
  26. $('.bookmark_delete').unbind('click', delBookmark);
  27. $('.bookmark_edit').unbind('click', showBookmark);
  28. for(var i in bookmarks.data) {
  29. updateBookmarksList(bookmarks.data[i]);
  30. $("#firstrun").hide();
  31. }
  32. if($('.bookmarks_list').is(':empty')) {
  33. $("#firstrun").show();
  34. }
  35. $('.bookmark_link').click(recordClick);
  36. $('.bookmark_delete').click(delBookmark);
  37. $('.bookmark_edit').click(showBookmark);
  38. bookmarks_loading = false;
  39. if (bookmarks.data.length) {
  40. updateOnBottom()
  41. }
  42. }
  43. });
  44. }
  45. // function addBookmark() {
  46. // Instead of creating editBookmark() function, Converted the one above to
  47. // addOrEditBookmark() to make .js file more compact.
  48. function addOrEditBookmark(event) {
  49. var id = $('#bookmark_add_id').val();
  50. var url = encodeEntities($('#bookmark_add_url').val());
  51. var title = encodeEntities($('#bookmark_add_title').val());
  52. var tags = encodeEntities($('#bookmark_add_tags').val());
  53. $("#firstrun").hide();
  54. if (id == 0) {
  55. $.ajax({
  56. url: OC.filePath('bookmarks', 'ajax', 'addBookmark.php'),
  57. data: 'url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&tags=' + encodeURIComponent(tags),
  58. success: function(response){
  59. $('.bookmarks_input').val('');
  60. $('.bookmarks_list').empty();
  61. bookmarks_page = 0;
  62. getBookmarks();
  63. }
  64. });
  65. }
  66. else {
  67. $.ajax({
  68. url: OC.filePath('bookmarks', 'ajax', 'editBookmark.php'),
  69. data: 'id=' + id + '&url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&tags=' + encodeURIComponent(tags),
  70. success: function(){
  71. $('.bookmarks_input').val('');
  72. $('#bookmark_add_id').val('0');
  73. $('.bookmarks_list').empty();
  74. bookmarks_page = 0;
  75. getBookmarks();
  76. }
  77. });
  78. }
  79. }
  80. function delBookmark(event) {
  81. var record = $(this).parent().parent();
  82. $.ajax({
  83. url: OC.filePath('bookmarks', 'ajax', 'delBookmark.php'),
  84. data: 'id=' + record.data('id'),
  85. success: function(data){
  86. if (data.status == 'success') {
  87. record.remove();
  88. if($('.bookmarks_list').is(':empty')) {
  89. $("#firstrun").show();
  90. }
  91. }
  92. }
  93. });
  94. }
  95. function showBookmark(event) {
  96. var record = $(this).parent().parent();
  97. $('#bookmark_add_id').val(record.attr('data-id'));
  98. $('#bookmark_add_url').val(record.children('.bookmark_url:first').text());
  99. $('#bookmark_add_title').val(record.children('.bookmark_title:first').text());
  100. $('#bookmark_add_tags').val(record.children('.bookmark_tags:first').text());
  101. if ($('.bookmarks_add').css('display') == 'none') {
  102. $('.bookmarks_add').slideToggle();
  103. }
  104. $('html, body').animate({
  105. scrollTop: ($('.bookmarks_menu'))?$('.bookmarks_menu').offset().top:0
  106. }, 500);
  107. }
  108. function updateBookmarksList(bookmark) {
  109. var tags = encodeEntities(bookmark.tags).split(' ');
  110. var taglist = '';
  111. for ( var i=0, len=tags.length; i<len; ++i ){
  112. if(tags[i] != '')
  113. taglist = taglist + '<a class="bookmark_tag" href="?tag=' + encodeURIComponent(tags[i]) + '">' + tags[i] + '</a> ';
  114. }
  115. if(!hasProtocol(bookmark.url)) {
  116. bookmark.url = 'http://' + bookmark.url;
  117. }
  118. if(bookmark.title == '') bookmark.title = bookmark.url;
  119. $('.bookmarks_list').append(
  120. '<div class="bookmark_single" data-id="' + bookmark.id +'" >' +
  121. '<p class="bookmark_actions">' +
  122. '<span class="bookmark_edit">' +
  123. '<img class="svg" src="'+OC.imagePath('core', 'actions/rename')+'" title="Edit">' +
  124. '</span>' +
  125. '<span class="bookmark_delete">' +
  126. '<img class="svg" src="'+OC.imagePath('core', 'actions/delete')+'" title="Delete">' +
  127. '</span>&nbsp;' +
  128. '</p>' +
  129. '<p class="bookmark_title">'+
  130. '<a href="' + encodeEntities(bookmark.url) + '" target="_blank" class="bookmark_link">' + encodeEntities(bookmark.title) + '</a>' +
  131. '</p>' +
  132. '<p class="bookmark_url"><a href="' + encodeEntities(bookmark.url) + '" target="_blank" class="bookmark_link">' + encodeEntities(bookmark.url) + '</a></p>' +
  133. '</div>'
  134. );
  135. if(taglist != '') {
  136. $('div[data-id="'+ bookmark.id +'"]').append('<p class="bookmark_tags">' + taglist + '</p>');
  137. }
  138. }
  139. function updateOnBottom() {
  140. //check wether user is on bottom of the page
  141. var top = $('.bookmarks_list>:last-child').position().top;
  142. var height = $('.bookmarks_list').height();
  143. // use a bit of margin to begin loading before we are really at the
  144. // bottom
  145. if (top < height * 1.2) {
  146. getBookmarks();
  147. }
  148. }
  149. function recordClick(event) {
  150. $.ajax({
  151. url: OC.filePath('bookmarks', 'ajax', 'recordClick.php'),
  152. data: 'url=' + encodeURIComponent($(this).attr('href')),
  153. });
  154. }
  155. function encodeEntities(s){
  156. try {
  157. return $('<div/>').text(s).html();
  158. } catch (ex) {
  159. return "";
  160. }
  161. }
  162. function hasProtocol(url) {
  163. var regexp = /(ftp|http|https|sftp)/;
  164. return regexp.test(url);
  165. }