Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function add_filter(type, filter) {
- // Get the current filters
- var filters = (localStorage.filters !== undefined ? JSON.parse(localStorage.filters) : []);
- // Add filter
- filters.push({type : type, filter : filter});
- localStorage.filters = JSON.stringify(filters);
- }
- function remove_filter(type, filter) {
- // Get the current filters
- var filters = (localStorage.filters !== undefined ? JSON.parse(localStorage.filters) : []);
- // Loop through filters
- for (var i = 0; i < filters.length; i++) {
- if (filters[i].type == type && filters[i].filter == filter) {
- // Remove any matches
- filters.splice(i, 1);
- }
- }
- localStorage.filters = JSON.stringify(filters);
- }
- /** Simple JavaScript filter
- * Filters posts that match <rule>
- *
- * rule format
- * {
- * name : <poster name>
- * trip : <poster trip>
- * email : <poster email>
- * subject: <post subject>
- * }
- *
- * apply_filter(rule)
- * Hides all posts that match <rule>
- *
- * add_filter(rule)
- * Applys the filter <rule>, then stores the rule in localStorage
- *
- * apply_all_filters()
- * Applys all the filters in localStorage, should be used on page load
- **/
- function apply_filter (rule) {
- if (!rule) { return; }
- // Loop through all the posts
- $(".postContainer").filter(function (index) {
- // Fetch the post from the DOM
- // avoiding use of `this' because JSLint won't stop whining
- var post = $(".postContainer")[index];
- // If a post matches a rule, hide the post
- if ($(post).find(".name").text() == rule.name) {
- $(post).hide();
- }
- if ($(post).find(".trip").text() == rule.trip) {
- $(post).hide();
- }
- if ($(post).find(".email").attr("href") == "mailto:" + rule.email) {
- $(post).hide();
- }
- if ($(post).find(".subject").text() == rule.subject) {
- $(post).hide();
- }
- return;
- });
- }
- // Add a filter to the localStorage
- function add_filter (rule) {
- if (!rule) { return; }
- // Apply the filter first
- apply_filter(rule);
- // Get all of the stored filters
- var filters = [];
- if (localStorage.filters) { filters = JSON.parse(localStorage.filters); }
- // Add the new filter and store it in localStorage
- filters.push(rule);
- localStorage.filters = JSON.stringify(filters);
- return;
- }
- function remove_filter (rule) {
- if (!rule) { return; }
- if (!localStorage.filters) { return; }
- // Get all of the stored filters
- var filters = JSON.parse(localStorage.filters);
- var new_filters = [];
- // Loop through the rules
- for (var i = 0; i < filters.length; i++) {
- // Remove any rules that match
- if (JSON.stringify(filters[i]) == JSON.stringify(rule)) {
- continue;
- }
- new_filters.push(filters[i]);
- }
- // Put the left over filters in the localStorage
- localStorage.filters = JSON.stringify(new_filters);
- return;
- }
- // Apply all filters in the localStorage
- function apply_all_filters () {
- if (!localStorage.filters) { return; }
- // Get all of the stored filters
- var filters = JSON.parse(localStorage.filters);
- // Loop through the rules and apply them
- for (var i = 0; i < filters.length; i++) { apply_filter( filters[i] ); }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement