From 1ce9fb8174cd7f5fb39f87b83095caf6a47258f1 Mon Sep 17 00:00:00 2001 From: Fredrick Brennan Date: Sat, 20 Sep 2014 15:27:26 +0000 Subject: [PATCH] Allow users to choose their favorite boards for sites w/too many to put in bar --- js/favorites.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 js/favorites.js diff --git a/js/favorites.js b/js/favorites.js new file mode 100644 index 00000000..30bf12d0 --- /dev/null +++ b/js/favorites.js @@ -0,0 +1,75 @@ +/* + * favorites.js - Allow user to favorite boards and put them in the bar + * + * Copyright (c) 2014 Fredrick Brennan + * + * Usage: + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/favorites.js'; + */ + +if (!localStorage.favorites) { + localStorage.favorites = '[]'; +} + +function favorite(board) { + var favorites = JSON.parse(localStorage.favorites); + favorites.push(board); + localStorage.favorites = JSON.stringify(favorites); +}; + +function unfavorite(board) { + var favorites = JSON.parse(localStorage.favorites); + var index = $.inArray(board, favorites); + if (~index) { + favorites.splice(index, 1); + } + localStorage.favorites = JSON.stringify(favorites); +}; + +function handle_boards(data) { + var boards = new Array(); + data = JSON.parse(data); + + $.each(data, function(k, v) { + boards.push(''+v+''); + }) + + if (boards[0]) { + return $('').append(' [ '+boards.slice(0,10).join(" / ")+' ] '); + } +} + +function add_favorites() { + $('.favorite-boards').remove(); + + var boards = handle_boards(localStorage.favorites); + + $('.boardlist').append(boards); +}; + +if (active_page == 'thread' || active_page == 'index') { + $(document).ready(function(){ + var favorites = JSON.parse(localStorage.favorites); + var is_board_favorite = ~$.inArray(board_name, favorites); + console.log(is_board_favorite); + + $('header>h1').append('\u2605'); + add_favorites(); + + $('#favorite-star').on('click', function(e) { + e.preventDefault(); + if (!$(this).data('active')) { + favorite(board_name); + add_favorites(); + $(this).css('color', 'yellow'); + $(this).data('active', true); + } else { + unfavorite(board_name); + add_favorites(); + $(this).css('color', 'grey'); + $(this).data('active', false); + } + }); + }); +}