Debian Codegraph @

js/widgets/widget-mark.js

raw symbols SCIP
1 /*! Widget: mark.js - updated 9/23/2016 (v2.27.7) *//*
2 * Requires tablesorter v2.8+ and jQuery 1.7+
3 * by Rob Garrison
4 */
5 ;( function( $ ) {
6 'use strict';
7 var ts = $.tablesorter;
8
9 ts.mark = {
10 init : function( c ) {
11 if ( typeof $.fn.mark === 'function' ) {
12 var tmp,
13 update = c.widgetOptions.mark_tsUpdate;
14 c.$table.on( 'filterEnd.tsmark pagerComplete.tsmark' +
15 ( update ? ' ' + update : '' ), function( e, filters ) {
16 // filterEnd passes "config" as the param
17 ts.mark.update( c, e.type === update ? filters : '' );
18 });
19 // Regex to split up a query
20 tmp = '(?:<|=|>|\\||\"|' + "\\'|" +
21 '\\s+(?:&&|-|' +
22 ( ts.language.and || 'and' ) + '|' +
23 ( ts.language.or || 'or' ) + '|' +
24 ( ts.language.to || 'to' ) + ')\\s+)';
25 ts.mark.regex.filter = new RegExp(tmp, 'gim');
26 } else {
27 console.warn('Widget-mark not initialized: missing "jquery.mark.js"');
28 }
29 },
30 regex : {
31 mark : /^mark_(.+)$/,
32 // test for regex (e.g. "/(lorem|ipsum)/gi")
33 pure : /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})?$/
34 },
35 checkRegex : function( regex ) {
36 if ( regex instanceof RegExp ) {
37 // prevent lock up of mark.js
38 // (see https://github.com/julmot/mark.js/issues/55)
39 var result = '\u0001\u0002\u0003\u0004\u0005'.match( regex );
40 return result === null || result.length < 5;
41 }
42 return false;
43 },
44 cleanMatches : function( matches ) {
45 var results = [],
46 indx = matches && matches.length || 0;
47 while ( indx-- ) {
48 if ( matches[indx] !== '' ) {
49 results[ results.length ] = matches[ indx ];
50 }
51 }
52 return results;
53 },
54 // used when "any" match is performed
55 ignoreColumns : function( c ) {
56 var wo = c.widgetOptions,
57 len = c.columns,
58 cols = [];
59 while (len--) {
60 if (wo.mark_tsIgnore[len] ||
61 $( c.$headerIndexed[len] ).hasClass( 'mark-ignore' ) ) {
62 cols[cols.length] = ':nth-child(' + (len + 1) + ')';
63 }
64 }
65 if (cols.length) {
66 return ':not(' + cols.join(',') + ')';
67 }
68 return '';
69 },
70 update : function( c, filters ) {
71 var options = {},
72 wo = c.widgetOptions,
73 regex = ts.mark.regex,
74 $rows = c.$table
75 .find( 'tbody tr' )
76 .unmark()
77 .not( '.' + ( c.widgetOptions.filter_filteredRow || 'filtered' ) );
78 filters = filters || $.tablesorter.getFilters( c.$table );
79 // extract & save mark options from widgetOptions (prefixed with "mark_")
80 // update dynamically
81 $.each( c.widgetOptions, function( key, val ) {
82 var matches = key.match( regex.mark );
83 if ( matches && typeof matches[1] !== 'undefined' ) {
84 options[ matches[1] ] = val;
85 }
86 });
87 $.each( filters, function( indx, filter ) {
88 if ( filter &&
89 !( $(c.$headerIndexed[indx]).hasClass('mark-ignore') ||
90 wo.mark_tsIgnore[indx]
91 ) ) {
92 var testRegex = null,
93 matches = filter,
94 useRegex = false,
95 col = indx === c.columns ?
96 ts.mark.ignoreColumns( c ) :
97 ':nth-child(' + ( indx + 1 ) + ')';
98 // regular expression entered
99 if ( regex.pure.test( filter ) ) {
100 matches = regex.pure.exec( filter );
101 // ignore "all" matches (i.e. /.*/)
102 if (matches[1] === '.*') {
103 matches[1] = '';
104 }
105 try {
106 // make sure to include global flag when testing regex
107 testRegex = new RegExp( matches[ 1 ], 'gim' );
108 matches = new RegExp( matches[ 1 ], matches[ 2 ] );
109 } catch (err) {
110 matches = null;
111 }
112 if ( ts.mark.checkRegex( testRegex ) ) {
113 $rows.children( col ).markRegExp( matches, options );
114 }
115 // matches is either null, invalid, or done my markRegExp
116 return;
117 }
118 // all special querys (or, and, wild cards & fuzzy)
119 // regex seems to not be consistent here; so use string indexOf
120 // fuzzy or wild card matches
121 if ( filter.indexOf( '~' ) === 0 ) {
122 useRegex = true;
123 // fuzzy search separate each letter
124 matches = filter.replace( /~/g, '' ).split( '' );
125 } else {
126 // wild card matching
127 if ( filter.indexOf( '?' ) > -1 ) {
128 useRegex = true;
129 filter = filter.replace( /\?/g, '\\S{1}' );
130 }
131 if ( filter.indexOf( '*' ) > -1 ) {
132 useRegex = true;
133 filter = filter.replace( /\*/g, '\\S*' );
134 }
135 matches = filter.split( regex.filter );
136 }
137 if ( useRegex && matches && matches.length ) {
138 matches = new RegExp(
139 ts.mark.cleanMatches( matches ).join( '.*' ),
140 'gm'
141 );
142 if ( ts.mark.checkRegex( matches ) ) {
143 $rows.children( col ).markRegExp( matches, options );
144 }
145 } else {
146 // pass an array of matches
147 $rows.children( col ).mark(
148 ts.mark.cleanMatches( matches ),
149 options
150 );
151 }
152 }
153 });
154 }
155 };
156
157 ts.addWidget({
158 id: 'mark',
159 options: {
160 mark_tsUpdate : 'markUpdate',
161 mark_tsIgnore : {}
162 },
163 init : function( table, thisWidget, c, wo ) {
164 ts.mark.init( c, wo );
165 },
166 remove : function( table, c ) {
167 var update = c.widgetOptions.mark_tsUpdate;
168 c.$table.off( 'filterEnd.tsmark pagerComplete.tsmark' +
169 ( update ? ' ' + update : '' ) );
170 }
171 });
172
173 })( jQuery );