Last active
December 20, 2015 11:19
-
-
Save KlonD90/6122499 to your computer and use it in GitHub Desktop.
example js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bindable = (function() { | |
function Bindable() {} | |
Bindable.prototype.bind = function(cb) { | |
if (typeof this.subscribers === 'undefined') { | |
this.subscribers = []; | |
} | |
return this.subscribers.push(cb); | |
}; | |
Bindable.prototype.call = function() { | |
var cb, _i, _len, _ref, _results; | |
if (typeof this.subscribers === 'undefined') { | |
this.subscribers = []; | |
} | |
_ref = this.subscribers; | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
cb = _ref[_i]; | |
_results.push(cb()); | |
} | |
return _results; | |
}; | |
return Bindable; | |
})(); | |
BindCollection = (function(_super) { | |
__extends(BindCollection, _super); | |
function BindCollection(list) { | |
this.list = list; | |
} | |
BindCollection.prototype.push = function(val) { | |
this.list.push(val); | |
return this.call(); | |
}; | |
BindCollection.prototype["delete"] = function(val) { | |
var find; | |
find = this.list.indexOf(val); | |
if (find >= 0) { | |
this.list.splice(find, 1); | |
return this.call(); | |
} | |
}; | |
BindCollection.prototype.get = function() { | |
return this.list; | |
}; | |
return BindCollection; | |
})(Bindable); | |
BindVal = (function(_super) { | |
__extends(BindVal, _super); | |
function BindVal(val) { | |
this.val = val; | |
} | |
BindVal.prototype.set = function(val) { | |
this.val = val; | |
return this.call(); | |
}; | |
BindVal.prototype.get = function() { | |
return this.val; | |
}; | |
return BindVal; | |
})(Bindable); | |
MySet = (function() { | |
function MySet(obj) { | |
this.obj = obj; | |
} | |
MySet.prototype.set = function(key, val) { | |
if (typeof this.obj[key] === 'undefined') { | |
this.obj[key] = []; | |
} | |
return this.obj[key].push(val); | |
}; | |
MySet.prototype.get = function(key) { | |
return this.obj[key]; | |
}; | |
MySet.prototype.keys = function() { | |
var key, keys, v, _ref; | |
keys = []; | |
_ref = this.obj; | |
for (key in _ref) { | |
v = _ref[key]; | |
keys.push(key); | |
} | |
return keys; | |
}; | |
return MySet; | |
})(); | |
OptimizedIndex = (function() { | |
function OptimizedIndex(obj, len) { | |
this.obj = obj; | |
this.len = len; | |
} | |
OptimizedIndex.prototype.set = function(key, val) { | |
var cluster; | |
if (key === '') { | |
return false; | |
} | |
cluster = key.substr(0, this.len); | |
if (typeof this.obj[cluster] === 'undefined') { | |
this.obj[cluster] = {}; | |
} | |
if (typeof this.obj[cluster][key] === 'undefined') { | |
this.obj[cluster][key] = []; | |
} | |
return this.obj[cluster][key].push(val); | |
}; | |
OptimizedIndex.prototype.search = function(query) { | |
var clusterName, items, name, q, queries, result, _i, _len, _ref; | |
query = query.toLowerCase().replace(',', '').replace('.', '').replace(/^\s+|\s+$/g, ""); | |
result; | |
queries = query.split(' '); | |
if (queries.length > 1) { | |
result = false; | |
for (_i = 0, _len = queries.length; _i < _len; _i++) { | |
q = queries[_i]; | |
if (result === false) { | |
result = this.search(q); | |
} else { | |
result = _.intersection(result, this.search(q)); | |
} | |
} | |
} else { | |
result = []; | |
clusterName = query.substr(0, this.len); | |
_ref = this.obj[clusterName]; | |
for (name in _ref) { | |
items = _ref[name]; | |
if (name.indexOf(query) >= 0) { | |
result = _.union(result, items); | |
} | |
} | |
} | |
return result; | |
}; | |
return OptimizedIndex; | |
})(); | |
Filter = { | |
isInit: false, | |
processData: function(data) { | |
var country, firstLetter, genre, index, item, key, word, year, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _len6, _m, _n, _o, _p, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8; | |
this.data = _.map(data, function(item) { | |
item.genre = _.map(item.genre, function(genre) { | |
return genre.genre; | |
}); | |
if (item.year_end === '-1') { | |
item.year_end = '2013'; | |
} | |
return item; | |
}); | |
this.genresList = new MySet({}); | |
this.countriesList = new MySet({}); | |
this.yearsList = new MySet({}); | |
this.letterList = new MySet({}); | |
_ref = this.data; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
item = _ref[_i]; | |
_ref1 = item.genre; | |
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | |
genre = _ref1[_j]; | |
this.genresList.set(genre, item); | |
} | |
_ref2 = item.country; | |
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | |
country = _ref2[_k]; | |
this.countriesList.set(country, item); | |
} | |
for (year = _l = _ref3 = item.year_start, _ref4 = item.year_end; _ref3 <= _ref4 ? _l <= _ref4 : _l >= _ref4; year = _ref3 <= _ref4 ? ++_l : --_l) { | |
this.yearsList.set(year, item); | |
} | |
firstLetter = item.name.substr(0, 1); | |
var regexp_en = /[A-Z]/gi; | |
var regexp_num = /\d+/gi; | |
if(firstLetter.match(regexp_en)){ | |
this.letterList.set('A-Z', item); | |
} | |
else if(firstLetter.match(regexp_num)){ | |
this.letterList.set('0-9', item); | |
} | |
else { | |
this.letterList.set(firstLetter.toUpperCase(), item); | |
} | |
} | |
$('.genreList').html(''); | |
$('.genreList').append("<li class=\"null\"><a href=\"#\">Любой</a></li>"); | |
_ref5 = this.genresList.keys(); | |
for (_m = 0, _len3 = _ref5.length; _m < _len3; _m++) { | |
key = _ref5[_m]; | |
$('.genreList').append('<li><a href="#">' + key + '</a></li>'); | |
} | |
$('.countryList').html(''); | |
$('.countryList').append("<li class=\"null\"><a href=\"#\">Любая</a></li>"); | |
_ref6 = this.countriesList.keys(); | |
for (_n = 0, _len4 = _ref6.length; _n < _len4; _n++) { | |
key = _ref6[_n]; | |
$('.countryList').append('<li><a href="#">' + key + '</a></li>'); | |
} | |
this.searchIndex = _.map(this.data, function(item) { | |
return { | |
link: item, | |
pattern: ("" + (item.country.join(' ')) + " " + item.actors + " " + (item.genre.join(' ')) + " " + item.name + " " + item.name_en + " " + item.year_start + " " + item.year_end).toLowerCase().replace(',', '').replace(/^\s+|\s+$/g, "") | |
}; | |
}); | |
this.reverseSearchIndex = new OptimizedIndex({}, 3); | |
_ref7 = this.searchIndex; | |
for (_o = 0, _len5 = _ref7.length; _o < _len5; _o++) { | |
index = _ref7[_o]; | |
_ref8 = index.pattern.split(' '); | |
for (_p = 0, _len6 = _ref8.length; _p < _len6; _p++) { | |
word = _ref8[_p]; | |
this.reverseSearchIndex.set(word, index.link); | |
} | |
} | |
return console.log(this.reverseSearchIndex); | |
}, | |
behaviour: { | |
changeSearch: function() { | |
return $('.field-search').on('keyup', function() { | |
var query, word, words, _i, _len; | |
if($(this).val() != ''){ | |
$(this).css('color','#444'); | |
} | |
else { | |
$(this).css('color','#A6A6A6'); | |
} | |
query = $(this).val().toLowerCase().replace(',', '').replace('.', '').replace(/^\s+|\s+$/g, ""); | |
words = query.split(' '); | |
for (_i = 0, _len = words.length; _i < _len; _i++) { | |
word = words[_i]; | |
if (word.length < 3) { | |
Filter.search.set(''); | |
return false; | |
} | |
} | |
return Filter.search.set(query); | |
}); | |
}, | |
setTitleYears: function() { | |
if (Filter.years.get() === false) { | |
return $('.years').text('Год...').removeClass('active'); | |
} else { | |
return $('.years').text(Filter.years.get()).addClass('active'); | |
} | |
}, | |
setTitleCountries: function() { | |
if (Filter.countries.get() === false) { | |
return $('.country').removeClass('active').text('Страна'); | |
} else { | |
return $('.country').text(Filter.countries.get()).addClass('active'); | |
} | |
}, | |
setTitleGenres: function() { | |
if (Filter.genres.get().length == 0) { | |
return $('.genre').removeClass('active').text('Жанр'); | |
} else { | |
return $('.genre').text(Filter.genres.get()[0]).addClass('active'); | |
} | |
}, | |
toggleChoice: function() { | |
/*$('.genreList').on('click', 'li', function(e) { | |
e.preventDefault(); | |
$(this).toggleClass('active'); | |
if ($(this).hasClass('active')) { | |
switch ($(this).parents('.slideMenu').data('source')) { | |
case 'genre': | |
return Filter.genres.push($(this).text()); | |
} | |
} else { | |
switch ($(this).parents('.slideMenu').data('source')) { | |
case 'genre': | |
return Filter.genres["delete"]($(this).text()); | |
} | |
} | |
});*/ | |
$('.genreList').on('click', 'li', function(e) { | |
var _this = this; | |
e.preventDefault(); | |
Filter.genres["delete"]($(_this).parents('ul').find('li.active').text()); | |
return $(this).siblings().removeClass('active').end().addClass('active').parents('ul').slideUp(100, function() { | |
val = $(_this).hasClass('null') ? false : $(_this).text(); | |
return val ? Filter.genres.push(val) : false; | |
}); | |
$(this).toggleClass('active'); | |
if ($(this).hasClass('active')) { | |
switch ($(this).parents('.slideMenu').data('source')) { | |
case 'genre': | |
return Filter.genres.push($(this).text()); | |
} | |
} else { | |
switch ($(this).parents('.slideMenu').data('source')) { | |
case 'genre': | |
return Filter.genres["delete"]($(this).text()); | |
} | |
} | |
}); | |
$('.countryList,.yearsList').on('click', 'li', function(e) { | |
var _this = this; | |
e.preventDefault(); | |
return $(this).siblings().removeClass('active').end().addClass('active').parents('ul').slideUp(100, function() { | |
var val; | |
val = $(_this).hasClass('null') ? false : $(_this).text(); | |
switch ($(_this).parents('.slideMenu').data('source')) { | |
case 'country': | |
return Filter.countries.set(val); | |
case 'year': | |
return Filter.years.set(val); | |
/* case 'genre': | |
Filter.genres["delete"](cur); | |
return Filter.genres.push($(this).text());*/ | |
} | |
}); | |
}); | |
return $('.genre, .country, .years').each(function() { | |
var index, label, slider, timer; | |
timer = false; | |
index = $(this).index(); | |
slider = $('.slideMenu').eq(index); | |
label = $(this); | |
return $(this).add(slider).hover(function() { | |
switch (slider.data('source')) { | |
case 'country': | |
$('.country').text('Страна'); | |
break; | |
case 'year': | |
$('.years').text('Год...'); | |
break; | |
case 'genre': | |
$('.genre').text('Жанр...'); | |
} | |
if (timer) { | |
clearTimeout(timer); | |
} | |
return slider.slideDown(100, function() {slider.jScrollPane({verticalGutter: 0});}); | |
}, function() { | |
var func, | |
_this = this; | |
func = function() { | |
return slider.slideUp(100, function() { | |
switch (slider.data('source')) { | |
case 'country': | |
if (Filter.countries.get()) { | |
return $('.country').text(Filter.countries.get()); | |
} | |
break; | |
case 'year': | |
if (Filter.years.get()) { | |
return $('.years').text(Filter.years.get()); | |
} | |
break; | |
case 'genre': | |
if (Filter.genres.get().length > 0) { | |
return $('.genre').text(Filter.genres.get()[0]); | |
} | |
} | |
}); | |
}; | |
return timer = setTimeout(func, 1000); | |
}); | |
}); | |
//$('.scroll-pane').delay(1000).jScrollPane(); | |
}, | |
letterChoice: function() { | |
return $('.letters span').click(function() { | |
var letter; | |
letter = $(this).siblings().removeClass('active').end().addClass('active').text(); | |
if (letter !== 'Все') { | |
return Filter.letter.set(letter); | |
} else { | |
return Filter.letter.set(false); | |
} | |
}); | |
}, | |
render: function() { | |
var data, genre, genreData, letterData, _i, _len, _ref; | |
//console.log(Filter.letter.get()); | |
if (Filter.letter.get()) { | |
if(!(letterData = Filter.letterList.get(Filter.letter.get()))){ | |
letterData = new Array(); | |
} | |
} else { | |
letterData = false; | |
} | |
if (Filter.genres.get().length > 0) { | |
genreData = []; | |
_ref = Filter.genres.get(); | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
genre = _ref[_i]; | |
genreData = _.union(genreData, Filter.genresList.get(genre)); | |
} | |
//console.log(genreData); | |
} else { | |
genreData = false; | |
} | |
if (letterData && genreData) { | |
data = _.intersection(letterData, genreData); | |
} else { | |
if (letterData) { | |
data = letterData; | |
} else { | |
if (genreData) { | |
data = genreData; | |
} else { | |
data = Filter.data; | |
} | |
} | |
} | |
if (Filter.countries.get()) { | |
data = _.intersection(data, Filter.countriesList.get(Filter.countries.get())); | |
} | |
if (Filter.years.get()) { | |
data = _.intersection(data, Filter.yearsList.get(Filter.years.get())); | |
} | |
if (Filter.search.get().length > 0) { | |
data = _.intersection(data, Filter.reverseSearchIndex.search(Filter.search.get())); | |
} | |
return RenderCinemaList.setData(data); | |
} | |
}, | |
bind: function() { | |
Filter.behaviour.toggleChoice(); | |
Filter.behaviour.letterChoice(); | |
return Filter.behaviour.changeSearch(); | |
}, | |
initialize: function() { | |
var hash, query; | |
Filter.isInit = true; | |
Filter.letter = new BindVal(false); | |
Filter.genres = new BindCollection([]); | |
Filter.years = new BindVal(false); | |
Filter.countries = new BindVal(false); | |
Filter.search = new BindVal(''); | |
hash = location.hash; | |
if (hash.indexOf('search=')) { | |
query = decodeURIComponent(hash.substr(hash.indexOf('search=') + 'search='.length)); | |
Filter.search.set(query); | |
$('.field-search').val(query); | |
if(query){ | |
$('.field-search').css('color', '#444'); | |
} | |
} | |
Filter.letter.bind(Filter.behaviour.render); | |
Filter.genres.bind(Filter.behaviour.setTitleGenres); | |
Filter.genres.bind(Filter.behaviour.render); | |
Filter.years.bind(Filter.behaviour.setTitleYears); | |
Filter.years.bind(Filter.behaviour.render); | |
Filter.countries.bind(Filter.behaviour.setTitleCountries); | |
Filter.countries.bind(Filter.behaviour.render); | |
return Filter.search.bind(Filter.behaviour.render); | |
} | |
}; | |
window.Filter = Filter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment