///
///
var FancyCheckboxes;
(function (FancyCheckboxes) {
var FancyCheckbox = (function () {
function FancyCheckbox($selector) {
this.events = {
'error': [],
'success': []
};
this.selected = {};
this.$selector = $selector;
this.checkboxData = [];
this.populateCheckboxData(JSON.parse($selector.data('items') ? $selector.data('items').replace(/'/g, '"') : "[]"));
this.$selector.click(this.onSelectorClick.bind(this));
this.$selector.prev().append('
');
this.loadSelected();
}
FancyCheckbox.prototype.loadSelected = function () {
var context = this;
var first = true;
var $selected = context.$selector.prev().find('.selected-items');
this.selected = {};
$selected.html('');
console.dir($(this.$selector.data('field')).find(':selected'));
$(this.$selector.data('field')).find(':selected').each(function () {
context.selected[$(this).attr('value')] = $(this).html();
if (!first) {
$selected.append(', ');
}
if (first) {
first = false;
}
$selected.append('' + $(this).html() + '');
});
console.dir(this.selected);
};
FancyCheckbox.prototype.onSelectorClick = function (event) {
var context = this;
var content = $('');
content.append('');
content.append('' + this.render().html() + '
');
content.append('');
content.find('.modal-footer').append('');
var options = {
content: '' + content.html() + '
',
type: 'html',
modal: true,
height: window.innerHeight,
width: $('.mainBlock').outerWidth(),
autoSize: false,
afterClose: function () {
context.loadSelected();
},
afterShow: function () {
context.bindEvents();
}
};
$.fancybox(options);
};
FancyCheckbox.prototype.bindEvents = function () {
var context = this;
var $fancy = $('.fancycheckboxes');
$fancy.find('.more').each(function () {
var $link = $(this);
$link.on('click', function () {
$link.parent().animate({ height: $link.parent()[0].scrollHeight }, 200, function () {
$link.parent().height('auto');
$link.remove();
});
});
});
$fancy.find('.btn-primary').click(function () {
$(context.$selector.data('field')).find('option').removeAttr('selected');
$fancy.find('input:checked').each(function () {
console.dir($(context.$selector.data('field'))
.find('[value=' + $(this).attr('value') + ']'));
$(context.$selector.data('field'))
.find('[value=' + $(this).attr('value') + ']')
.prop('selected', 'selected');
});
$.fancybox.close();
});
};
FancyCheckbox.prototype.checkChecked = function (id) {
return this.selected.hasOwnProperty(id);
};
FancyCheckbox.prototype.on = function (eventName, callback) {
this.events[eventName].push(callback);
return this;
};
FancyCheckbox.prototype.trigger = function (eventName) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var context = this;
for (var i in this.events[eventName]) {
if (this.events[eventName].hasOwnProperty(i)) {
setTimeout(this.events[eventName][i].apply(context, args), 1);
}
}
};
FancyCheckbox.prototype.populateCheckboxData = function (checkboxData) {
checkboxData.sort(function (item1, item2) {
return item1.value.localeCompare(item2.value);
});
for (var i in checkboxData) {
if (checkboxData.hasOwnProperty(i)) {
if (checkboxData[i].items.length) {
var group = new CheckboxGroup(checkboxData[i], this);
this.checkboxData.push(group);
}
else {
var item = new CheckboxItem(checkboxData[i], this);
this.checkboxData.push(item);
}
}
}
};
FancyCheckbox.prototype.render = function () {
var items = $('');
this.checkboxData.forEach(function (item) {
if (item instanceof CheckboxItem) {
var group = $('');
group.append(item.render());
items.append(group);
}
else {
items.append(item.render());
}
});
items.find('.fancycheckboxes-group').each(function () {
if ($(this).find('.fancycheckboxes-item').length > 5) {
$(this).append($('Все »'));
$(this).height(150);
}
});
return items;
};
return FancyCheckbox;
}());
FancyCheckboxes.FancyCheckbox = FancyCheckbox;
var CheckboxGroup = (function () {
function CheckboxGroup(item, widget) {
this.widget = widget;
this.name = item.value;
this.items = [];
this.populateCheckboxData(item.items);
}
CheckboxGroup.prototype.populateCheckboxData = function (checkboxData) {
checkboxData.sort(function (item1, item2) {
return item1.value.localeCompare(item2.value);
});
for (var i in checkboxData) {
if (checkboxData.hasOwnProperty(i)) {
if (checkboxData[i].items.length) {
var group = new CheckboxGroup(checkboxData[i], this.widget);
this.items.push(group);
}
else {
var item = new CheckboxItem(checkboxData[i], this.widget);
this.items.push(item);
}
}
}
};
CheckboxGroup.prototype.render = function () {
var group = $('');
group.append($('' + this.name + '
'));
this.items.forEach(function (item) {
group.append(item.render());
});
return group;
};
return CheckboxGroup;
}());
var CheckboxItem = (function () {
function CheckboxItem(item, widget) {
this.id = item.id;
this.name = item.value;
this.widget = widget;
}
CheckboxItem.prototype.render = function () {
var item = $('');
item.append($(' '));
if (this.widget.checkChecked(this.id)) {
item.find('input').attr('checked', 'checked');
}
return item;
};
return CheckboxItem;
}());
})(FancyCheckboxes || (FancyCheckboxes = {}));