Blame view

app/js/plugins/faq/faq.js 4.35 KB
feacde5ff   Rishav   setup acuefuel in...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  /**
   * @name      FAQ Plugin
   * @author    Rod Howard
   * @url       http://goideate.com
   * @date      April 28, 2012
   * @license   GNU/GPL Version 3
   *
   *
   * Example:
   *
   *   $(function() {
   *     $("element, #id, .class").technify({
   *       // #{EDIT-HERE}# Your run-time options go here ...
   *     });
   *   });
   */
  /**
   * Create an anonymous function to avoid library conflicts
   */
  (function($) {
      /**
       * Add our plugin to the jQuery.fn object
       */
      $.fn.goFaq = function(options) {
          /**
           * Define some default settings
           */
          var defaults = {
          	enableSearch: true,
          	enableToc: true,
          	enableStyling: true,
              //numberHtml: '{{#}}.',
              numberHtml: '<div class="faq-number">{{#}}</div>'
          };
          /**
           * Merge the runtime options with the default settings
           */
          var options = $.extend({}, defaults, options);
          /**
           * Iterate through the collection of elements and
           * return the object to preserve method chaining
           */
          return this.each(function(i) {
              /**
               * Wrap the current element in an instance of jQuery
               */
              var $this = $(this);
              
              var $container = $this.wrap ('<div class="faq-container"></div>');
              
              $this.addClass ('faq-list');
              
              if (options.enableSearch) {            	
  	            var $form = generateSearchForm ();
  	            $form.insertBefore ($this);
              }
              
              if (options.enableToc) {
  	            var $toc = generateToc ($this);
  	            $toc.insertBefore ($this);
  	        }
              			
              
              var $empty = generateEmptySearch ();
              $empty.appendTo ($container);
              
              generateAnswerNumbers ($this);
              
          });
          
          function search (e) {
  			var el, container, filter, count, pattern, container, answers, toc, tocs, empty;
  			
  			el = $(this);
  			container = el.parents ('.faq-container');
  			filter = el.val ();
  			toc = container.find ('.faq-toc');
  			answers = container.find ('.faq-list').find ('li');
  			tocs = container.find ('.faq-toc').find('li');
  			empty = container.find ('.faq-empty');
  			pattern = new RegExp (filter, 'i');
  			
  			answers.hide ();
  			tocs.hide ();
  			
  			$.grep (answers.find ('.faq-text'), function (input) {
  				if (pattern.test ($(input).text ())) {
  					$(input).parents ('li').show ();
  					
  					var index = $(input).parents ('li').index ();				
  					tocs.eq (index).show ();				
  				}			
  			});	
  			
  			if (!answers.is (':visible')) {
  				empty.show ();
  				toc.hide ();
  			} else {
  				empty.hide ();
  				toc.show ();
  			}
  		}
          
  		
  		function generateEmptySearch () {
  			var $empty = $('<div>', { 'class': 'faq-empty' });
  			
  			return $empty.html ('Nothing Found');
  		}
          
          function generateSearchForm () {
          	
          	var $form = $('<form>', { 'class': 'faq-search' });
          	var $input = $('<input>', { 'type': 'text', 'class': 'form-control', 'name': 'search', 'placeholder': 'Search by Keyword' });
          	
          	$input.appendTo ($form);
          	
          	$input.bind ('keyup', search)
          	
          	return $form;
          }
          
          function generateAnswerNumbers ($list) {
          	$list.find ('li').each (function (i) {
          		var id = parseInt (i+1);
          		
          		
          		$(this).wrapInner ('<div class="faq-text"></div>');
          		
          		
              	if (options.enableStyling) {
  					var icon = '<div class="faq-icon">' + options.numberHtml + '</div>';
  	        		
  					icon = icon.replace ('{{#}}', id);
  					$(this).prepend (icon);
  				}
          	});
          }
          
          function generateToc ($list) {
          	var html = '<ol>';	
          	
  			$list.find ('li').each (function (i) {				
  				var id = parseInt (i+1);							
  				html += '<li>' + id + '. <a href="#faq-' + id + '">' + $(this).find ('h4').text () + '</a></li>';								
  				$(this).attr ('id', 'faq-' + id);				
  			});	
  					
  			html += '</ol>';
  			
          	var $toc = $('<div>', { 'class': 'faq-toc' });
          	
          	$toc.html (html);
          	
          	return $toc;
          	
          }
      };
  })(jQuery);