Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Endless tree structured UL > LI > UL
  2.  
  3. $.extend($.expr[":"], {
  4.     "containsIN" : function(elem, i, match, array) {
  5.         return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
  6.     }
  7. });
  8.  
  9.  $('#my-search').on('search', function () { alert("a"); });
  10.  
  11.  
  12. var treefilter = function(el, options) {
  13.  
  14.     var defaults = {
  15.         offsetLeft : 20, // left offset for each level
  16.         searcher : null, // search input field
  17.         expanded : false, // if true, your list will be show with expanded.
  18.         expanded : false, // if true, your list will be show with expanded.
  19.         multiselect : false // multiselect.
  20.     };
  21.  
  22.     // Public Variables
  23.     var plugin = this;
  24.     var status = []; // save folder status for "var memory"
  25.  
  26.     plugin.settings = {};
  27.  
  28.     // Main Function
  29.     var init = function() {
  30.         plugin.settings = $.extend({}, defaults, options);
  31.         plugin.el = el;
  32.  
  33.         // set class names to tags
  34.         el.addClass("tf-tree");
  35.         el.find("li").addClass("tf-child-true");
  36.         el.find("li").css("padding-left", plugin.settings.offsetLeft);
  37.         el.find("li div:only-child").parent().removeClass("tf-child-true");
  38.         el.find("li div:only-child").parent().addClass("tf-child-false");
  39.  
  40.         // if the list has a checkbox, block event bubbling.
  41.         el.find("input[type=checkbox]").click(function(e) {
  42.             e.stopPropagation();
  43.         });
  44.  
  45.         // set click event.
  46.         el.find("li.tf-child-true").children("div").click(function(e) {
  47.             if (e.metaKey || e.ctrlKey) {
  48.                 if ($(this).parent().hasClass("tf-open")) {
  49.                     $(this).parent().find("li.tf-child-true").removeClass("tf-open");
  50.                 } else {
  51.                     $(this).parent().find("li.tf-child-true").addClass("tf-open");
  52.                 }
  53.             }
  54.             $(this).parent().toggleClass("tf-open");
  55.         });
  56.  
  57.         // toggle effect when multiselect enabled.
  58.         el.find("li.tf-child-false").click(function() {
  59.             if (plugin.settings.multiSelect != true) {
  60.                 el.find("li.tf-selected").removeClass("tf-selected");
  61.             }
  62.             $(this).toggleClass("tf-selected");
  63.         });
  64.  
  65.         if (plugin.settings.searcher) {
  66.             searcher();
  67.         }
  68.     };
  69.  
  70.     // PUBLIC METHOD
  71.     plugin.openAll = function() {
  72.         plugin.el.find("li.tf-child-true").parent().addClass("tf-open");
  73.     };
  74.     plugin.closeAll = function() {
  75.         plugin.el.find("li.tf-child-true").parent().removeClass("tf-open");
  76.     };
  77.  
  78. var processSearcherValue = function() {
  79.     if ($(this).val().length == 0) {
  80.         plugin.el.find(".tf-search-result").removeClass("tf-search-result");
  81.         memory("out", status);
  82.     } else {
  83.         plugin.closeAll();
  84.         plugin.el.find("li.tf-open").removeClass("tf-open");
  85.         plugin.el.find("li.tf-search-result").removeClass("tf-search-result");
  86.         plugin.el.find("li:containsIN('" + $(this).val() + "')").addClass("tf-search-result");
  87.         plugin.el.find("li.tf-search-result").parent().addClass("tf-search-result");
  88.     }
  89. };
  90.  
  91. var searcher = function() {
  92.     $(plugin.settings.searcher).keyup(processSearcherValue);
  93.     $(plugin.settings.searcher).keydown(function() {
  94.  
  95.         if ($(this).val().length == 0) {
  96.             memory("in", status);
  97.         }
  98.     });
  99. };
  100.  
  101.  
  102.     // save current status of folder
  103.     // action : string "in" / "out"
  104.     // array : array that saves current status
  105.     // list : el
  106.     var memory = function(action) {
  107.         if (action == "in") {
  108.             status = [];
  109.             plugin.el.find("li").each(function() {
  110.                 status.push($(this).hasClass("tf-open"));
  111.             });
  112.         } else if (action == "out") {
  113.             plugin.el.find("li").each(function(i) {
  114.                 if (status[i]) {
  115.                     $(this).addClass("tf-open");
  116.                 } else {
  117.                     $(this).removeClass("tf-open");
  118.                 }
  119.             });
  120.         }
  121.     }
  122.  
  123.     init();
  124. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement