source: extensions/SmartAlbums/admin/template/addFilters.js @ 26725

Last change on this file since 26725 was 26725, checked in by mistic100, 10 years ago

use external JS with template feature

File size: 6.8 KB
Line 
1var addFilter = (function($){
2  var count=0,
3      limit_count=0,
4      level_count=0;
5
6  // MAIN EVENT HANDLERS
7  $('#addFilter').change(function() {
8    if ($(this).val() != -1) {
9      add_filter($(this).val());
10      $(this).val(-1);
11    }
12  });
13
14  $('#removeFilters').click(function() {
15    $('#filtersList li').each(function() {
16      $(this).remove();
17    });
18
19    limit_level=0;
20    level_count=0;
21    return false;
22  });
23
24  $('input[name="is_smart"]').change(function() {
25    $('#SmartAlbum_options').toggle();
26    $('input[name="countImages"]').toggle();
27    $('.count_images_wrapper').toggle();
28  });
29
30  $('input[name="countImages"]').click(function() {
31    countImages($("#smart"));
32    return false;
33  });
34
35
36  // ADD FILTER FUNCTIONS
37  function add_filter(type, cond, value) {
38    count++;
39
40    var content = $("#filtersRepo #filter_"+type).html().replace(/iiii/g, count);
41    $block = $($.parseHTML(content)).appendTo("#filtersList");
42
43    if (cond) {
44      select_cond($block, type, cond);
45    }
46
47    if (value) {
48      if (type == "tags") {
49        $block.find(".filter-value .tagSelect").html(value);
50      }
51      else if (type == "album") {
52        select_options($block, value);
53      }
54      else if (type == "level") {
55        select_options($block, value);
56      }
57      else if (type != "dimensions") {
58        $block.find(".filter-value input").val(value);
59      }
60    }
61
62    init_jquery_handlers($block);
63
64    if (type == "dimensions") {
65      select_dimensions($block, cond, value);
66    }
67
68    if (type == 'limit') {
69      limit_count=1;
70      $("#addFilter option[value='limit']").attr('disabled','disabled');
71    }
72    else if (type == 'level') {
73      level_count=1;
74      $("#addFilter option[value='level']").attr('disabled','disabled');
75    }
76  }
77
78  function select_cond($block, type, cond) {
79    $block.find(".filter-cond option").removeAttr('selected');
80    $block.find(".filter-cond option[value='"+cond+"']").attr('selected', 'selected');
81  }
82
83  function select_dimensions($block, cond, value) {
84    console.log($block, cond, value);
85    cond = cond || 'width';
86
87    $block.find(">.filter-value>span").hide();
88    $block.find(".dimension_"+cond).show();
89
90    if (value) {
91      values = value.split(',');
92    }
93    else {
94      values = $block.find(".filter_dimension_"+cond+"_slider").slider("values");
95    }
96    $block.find(".filter_dimension_"+cond+"_slider").slider("values", values);
97  }
98
99  function select_options($block, value) {
100    values = value.split(',');
101    for (j in values) {
102      $block.find(".filter-value option[value='"+ values[j] +"']").attr('selected', 'selected');
103    }
104  }
105
106
107  // DECLARE JQUERY PLUGINS AND VERSATILE HANDLERS
108  function init_jquery_handlers($block) {
109    // remove filter
110    $block.find(".removeFilter").click(function() {
111      type = $(this).next("input").val();
112      if (type == 'limit') {
113        limit_count=1;
114        $("#addFilter option[value='limit']").removeAttr('disabled');
115      }
116      else if (type == 'level') {
117        level_count=1;
118        $("#addFilter option[value='level']").removeAttr('disabled');
119      }
120
121      $(this).parents('li').remove();
122      return false;
123    });
124
125    // date filter
126    if ($block.hasClass('filter_date')) {
127      $block.find("input[type='text']").each(function() {
128        $(this).datepicker({
129          dateFormat:'yy-mm-dd',
130          firstDay:1
131        });
132      });
133    }
134
135    // tags filter
136    if ($block.hasClass('filter_tags')) {
137      $block.find(".tagSelect").tokenInput(
138        [{foreach from=$all_tags item=tag name=tags}{ name:"{$tag.name|escape:javascript}", id:"{$tag.id}" }{if !$smarty.foreach.tags.last},{/if}{/foreach}],
139        {
140          hintText: '{'Type in a search term'|translate}',
141          noResultsText: '{'No results'|translate}',
142          searchingText: '{'Searching...'|translate}',
143          animateDropdown: false,
144          preventDuplicates: true,
145          allowFreeTagging: false
146        }
147      );
148    }
149
150    // album filter
151    if ($block.hasClass('filter_album')) {
152      $block.find(".albumSelect").chosen();
153    }
154
155    // dimension filter
156    if ($block.hasClass('filter_dimensions')) {
157      $block.find(".filter-cond select").change(function() {
158        select_dimensions($block, $(this).val());
159      });
160
161      $block.find(".filter_dimension_width_slider").slider({
162        range: true,
163        min: {$dimensions.bounds.min_width},
164        max: {$dimensions.bounds.max_width},
165        values: [{$dimensions.bounds.min_width}, {$dimensions.bounds.max_width}],
166        slide: function(event, ui) {
167          change_dimension_info($block, ui.values, "{'between %d and %d pixels'|translate}");
168        },
169        change: function(event, ui) {
170          change_dimension_info($block, ui.values, "{'between %d and %d pixels'|translate}");
171        }
172      });
173
174      $block.find(".filter_dimension_height_slider").slider({
175        range: true,
176        min: {$dimensions.bounds.min_height},
177        max: {$dimensions.bounds.max_height},
178        values: [{$dimensions.bounds.min_height}, {$dimensions.bounds.max_height}],
179        slide: function(event, ui) {
180          change_dimension_info($block, ui.values, "{'between %d and %d pixels'|translate}");
181        },
182        change: function(event, ui) {
183          change_dimension_info($block, ui.values, "{'between %d and %d pixels'|translate}");
184        }
185      });
186
187      $block.find(".filter_dimension_ratio_slider").slider({
188        range: true,
189        step: 0.01,
190        min: {$dimensions.bounds.min_ratio},
191        max: {$dimensions.bounds.max_ratio},
192        values: [{$dimensions.bounds.min_ratio}, {$dimensions.bounds.max_ratio}],
193        slide: function(event, ui) {
194          change_dimension_info($block, ui.values, "{'between %.2f and %.2f'|translate}");
195        },
196        change: function(event, ui) {
197          change_dimension_info($block, ui.values, "{'between %.2f and %.2f'|translate}");
198        }
199      });
200
201      $block.find("a.dimensions-choice").click(function() {
202        $block.find(".filter_dimension_"+ $(this).data("type") +"_slider").slider("values",
203          [$(this).data("min"), $(this).data("max")]
204        );
205      });
206    }
207  }
208
209
210  // GENERAL FUNCTIONS
211  function change_dimension_info($block, values, text) {
212    $block.find("input[name$='[value][min]']").val(values[0]);
213    $block.find("input[name$='[value][max]']").val(values[1]);
214    $block.find(".filter_dimension_info").html(sprintf(text, values[0], values[1]));
215  }
216
217  function countImages(form) {
218    jQuery.post("{$COUNT_SCRIPT_URL}", 'cat_id={$CAT_ID}&'+form.serialize(),
219      function success(data) {
220        jQuery('.count_images_wrapper').html(data);
221      }
222    );
223  }
224
225  return add_filter; // expose add_filter method
226}(jQuery));
Note: See TracBrowser for help on using the repository browser.