Advertisement
Garbonzo

Bookmarking Functionality

Jan 16th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.     HELEN Bookmarking
  3.  
  4.     Handles the adding and removing of bookmarks from site content for healthequitynetwork.org
  5.  
  6. **/
  7. function Helen_Bookmarking(){
  8.     //for referencing myself
  9.     helen_bookmarking = this;
  10.     this.bookmarks = [];
  11.    
  12.     /**
  13.      *  Get the user's current bookmarks, update star icon for that content on this page
  14.      **/
  15.     $.getJSON( "/?ACT=85&function=get_bookmarks", function( data ) {
  16.         if(typeof data != 'undefined' && data != null && data.length > 0){
  17.             $.each( data, function( key, val ) {
  18.                 helen_bookmarking.bookmarks.push(parseInt(val));
  19.                 //update any bookmark icons
  20.                 $.each($('.bookmark-button[data-file-id='+val+']'), function(){
  21.                     $(this).attr('data-original-title','Remove Bookmark');
  22.                     var star = $(this).children('span');
  23.                     star.removeClass('glyphicon-star-empty').addClass('glyphicon-star bookmarked');        
  24.                 });        
  25.             });
  26.         }
  27.     });
  28.  
  29.     $('.bookmark-button').on('click',function(event){
  30.         event.preventDefault();
  31.         var star = $(this).children('span');
  32.         var file_id = $(this).data('file-id');
  33.  
  34.         var y = event.screenY - 100;
  35.         if(star.hasClass('glyphicon-star-empty')){
  36.             $(this).attr('data-original-title','Remove Bookmark');
  37.             star.removeClass('glyphicon-star-empty').addClass('glyphicon-star bookmarked');
  38.             helen_bookmarking.add_bookmark(file_id);
  39.             flash_notification('Bookmark Added!', '#090', y);  
  40.         }
  41.         else{
  42.             $(this).attr('data-original-title','Bookmark');
  43.             star.removeClass('glyphicon-star').removeClass('bookmarked').addClass('glyphicon-star-empty');
  44.             helen_bookmarking.remove_bookmark(file_id);
  45.             flash_notification('Bookmark Removed!', '#900', y);    
  46.            
  47.             if ($(this).parents('#my-bookmarks-table').length) {
  48.                 $(this).closest('tr').fadeOut().remove();
  49.                 $('#total-bookmarks').html((parseInt($('#total-bookmarks').html())-1) +'');
  50.             }
  51.         }
  52.     });
  53. };
  54.  
  55. Helen_Bookmarking.prototype.add_bookmark = function(file_id) {
  56.     $.post( "/?ACT=85&function=add_bookmark&library_content_id="+file_id);
  57.     helen_bookmarking.bookmarks.push(parseInt(file_id));
  58. }
  59.  
  60. Helen_Bookmarking.prototype.remove_bookmark = function(file_id) {
  61.     $.post( "/?ACT=85&function=remove_bookmark&library_content_id="+file_id);
  62.     helen_bookmarking.bookmarks.splice( $.inArray(parseInt(file_id), helen_bookmarking.bookmarks), 1 );
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement