Completed
Push — master ( 165560...d3be23 )
by Denis
31s
created

jets.js (1 issue)

Severity
1
/*! Jets.js - v0.11.0 - 2016-07-30
2
* http://NeXTs.github.com/Jets.js/
3
* Copyright (c) 2015 Denis Lukov; Licensed MIT */
4
5
;(function(root, definition) {
6
  if (typeof module != 'undefined') module.exports = definition();
7
  else if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
8
  else root['Jets'] = definition();
9
}(this, function() {
10
  "use strict"
11
12
  function Jets(opts) {
13
    if( ! (this instanceof Jets)) {
14
      return new Jets(opts);
15
    }
16
    var self = this;
17
    ['searchTag', 'contentTag'].forEach(function(param) {
18
      var name = param.replace('Tag', ''),
19
        queryMethod = 'querySelector' + (param == 'contentTag' ? 'All' : '');
20
      self[name + '_tag'] = document[queryMethod](opts[param]);
21
      self[name + '_param'] = opts[param];
22
      if( ! self[name + '_tag']) {
23
        throw new Error('Error! Could not find ' + param + ' element');
24
      }
25
    });
26
27
    var defaults = {
28
      searchSelector: '*AND',
29
      hideBy: 'display:none',
30
      diacriticsMap: {}
31
    }
32
33
    self.options = {};
34
    ['columns', 'addImportant', 'searchSelector', 'hideBy', 'manualContentHandling', 'callSearchManually', 'diacriticsMap', 'didSearch', 'invert'].forEach(function(name) {
35
      self.options[name] = opts[name] || defaults[name];
36
    });
37
    if(this.options.searchSelector.length > 1) {
38
      var searchSelector = self.options['searchSelector'].trim();
39
      self.options.searchSelector = searchSelector.substr(0, 1);
40
      self.options.searchSelectorMode = searchSelector.substr(1).toUpperCase();
41
    }
42
43
    var last_search_query;
44
    self.search = function() {
45
      if(last_search_query == (last_search_query = self.search_tag.value)) return;
46
      (0,self._applyCSS());
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
47
      self.options.didSearch && self.options.didSearch(self.search_tag.value);
48
    };
49
    self._onSearch = function(event) {
50
      if(event.type == 'keydown')
51
        return setTimeout(self.search, 0);
52
      self.search();
53
    };
54
    self.destroy = function() {
55
      if( ! self.options.callSearchManually) self._processEventListeners('remove');
56
      self._destroy();
57
    };
58
59
    if( ! self.options.callSearchManually) self._processEventListeners('add');
60
    self._addStyleTag();
61
    self._setJets();
62
    self._applyCSS();
63
  }
64
65
  Jets.prototype = {
66
    constructor: Jets,
67
    _processEventListeners: function(action) {
68
      ['input', 'keydown', 'change'].forEach(function(event_type) {
69
        this.search_tag[action + 'EventListener'](event_type, this._onSearch);
70
      }.bind(this));
71
    },
72
    _applyCSS: function() {
73
      var options = this.options,
74
        search_phrase = this.replaceDiacritics(this.search_tag.value.trim().toLowerCase().replace(/\s\s+/g, ' ')).replace(/\\/g, '\\\\'),
75
        words = options.searchSelectorMode
76
          ? search_phrase.split(' ').filter(function(item, pos, arr) { return arr.indexOf(item) == pos; })
77
          : [search_phrase],
78
        is_strict_selector = options.searchSelectorMode == 'AND',
79
        selectors = new Array(words.length);
80
      for(var i = 0, ii = words.length; i < ii; i++) {
81
        selectors[i] = (is_strict_selector ? this.content_param + '>' : '') + (options.invert ? '' : ':not(') + '[data-jets' +
82
          options.searchSelector + '="' + words[i] + '"]' + (options.invert ? '' : ')');
83
      }
84
      var hide_rules = options.hideBy.split(';').filter(Boolean).map(function(rule) { return rule + (options.addImportant ? '!important' : '') });
85
      var css_rule = (is_strict_selector ? '' : this.content_param + '>') + selectors.join(is_strict_selector ? ',' : '') + '{' + hide_rules.join(';') + '}';
86
      this.styleTag.innerHTML = search_phrase.length ? css_rule : '';
87
    },
88
    _addStyleTag: function() {
89
      this.styleTag = document.createElement('style');
90
      document.head.appendChild(this.styleTag);
91
    },
92
    _getText: function(tag) {
93
      return tag && (tag.textContent || tag.innerText) || '';
94
    },
95
    _getContentTags: function(query) {
96
      return Array.prototype.slice.call(this.content_tag).reduce(function(all, elem) {
97
        return all.concat(Array.prototype.slice.call(elem.querySelectorAll(query || ':scope > *')));
98
      }, []);
99
    },
100
    _setJets: function(query, force) {
101
      var self = this,
102
        tags = self._getContentTags(force ? '' : query), text;
103
      for(var i = 0, tag; tag = tags[i]; i++) {
104
        if(tag.hasAttribute('data-jets') && ! force) continue;
105
        text = this.options.manualContentHandling
106
          ? this.options.manualContentHandling(tag)
107
          : self.options.columns && self.options.columns.length
108
            ? self.options.columns.map(function(column) {
109
                return self._getText(tag.children[column]);
110
              }).join(' ')
111
            : self._getText(tag);
112
        tag.setAttribute('data-jets', self.replaceDiacritics(text.trim().replace(/\s+/g, ' ').toLowerCase()));
113
      };
114
    },
115
    replaceDiacritics: function(text) {
116
      var diacritics = this.options.diacriticsMap;
117
      for(var letter in diacritics) if(diacritics.hasOwnProperty(letter)) {
118
        for(var i = 0, ii = diacritics[letter].length; i < ii; i++) {
119
          text = text.replace(new RegExp(diacritics[letter][i], 'g'), letter);
120
        }
121
      }
122
      return text;
123
    },
124
    update: function(force) {
125
      this._setJets(':scope > :not([data-jets])', force);
126
    },
127
    _destroy: function() {
128
      this.styleTag.parentNode && document.head.removeChild(this.styleTag);
129
      var tags = this._getContentTags();
130
      for(var i = 0, tag; tag = tags[i]; i++) {
131
        tag.removeAttribute('data-jets');
132
      }
133
    }
134
  }
135
136
  // :scope polyfill
137
  // http://stackoverflow.com/a/17989803/1221082
138
  ;(function(doc, proto) {
139
    try {
140
      doc.querySelector(':scope body');
141
    } catch (err) {
142
      ['querySelector', 'querySelectorAll'].forEach(function(method) {
143
        var nativ = proto[method];
144
        proto[method] = function(selectors) {
145
          if (/(^|,)\s*:scope/.test(selectors)) {
146
            var id = this.id;
147
            this.id = 'ID_' + Date.now();
148
            selectors = selectors.replace(/((^|,)\s*):scope/g, '$1#' + this.id);
149
            var result = doc[method](selectors);
150
            this.id = id;
151
            return result;
152
          } else {
153
            return nativ.call(this, selectors);
154
          }
155
        }
156
      });
157
    }
158
  })(window.document, Element.prototype);
159
160
  return Jets;
161
}));
162