Created
November 26, 2016 06:21
-
-
Save vgrish/6f866bfd09522fa18eff5a1531503539 to your computer and use it in GitHub Desktop.
quickview
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
/** v 1.1.0 */ | |
var quickview = { | |
config: {}, | |
defaults: { | |
selectors: { | |
view: 'quickview-view', | |
close: 'quickview-close btn-danger', | |
next: 'quickview-next btn-info', | |
prev: 'quickview-prev btn-info' | |
}, | |
text: { | |
title: 'quickview', | |
close: 'Закрыть', | |
next: 'Следующий', | |
prev: 'Предыдущий' | |
}, | |
direction: { | |
next: 'next', | |
prev: 'prev' | |
}, | |
size: { | |
normal: 'size-normal', | |
small: 'size-small', | |
wide: 'size-wide', | |
large: 'size-large' | |
} | |
}, | |
initialize: function(config) { | |
quickview.config = config; | |
$(document).on('submit', config.selector, function(e) { | |
e.preventDefault(); | |
return false; | |
}); | |
$(document).on('click', config.parentSelector + ' ' + config.selector + ' .' + quickview.defaults.selectors.view, function(e) { | |
var $this = $(this); | |
var action = 'view'; | |
var $form = $this.closest(config.selector); | |
var hash = $form.data('hash'); | |
var id = $form.data('id'); | |
var index = $(config.parentSelector + ' ' + config.selector + ' .' + quickview.defaults.selectors.view).index(this); | |
var data = quickview.getResponse(config.actionUrl, action, hash, id); | |
if (!data) { | |
return false; | |
} | |
if (typeof BootstrapDialog == 'undefined') { | |
alert('not BootstrapDialog !'); | |
return false; | |
} | |
var properties = data.properties; | |
if (!!!properties) { | |
properties = {}; | |
} | |
var size = quickview.modal.getSize(properties); | |
var title = quickview.modal.getTitle(properties); | |
var loop = quickview.modal.getLoop(properties); | |
var buttons = quickview.modal.getButtons(properties); | |
var closable = quickview.modal.getClosable(properties); | |
var draggable = quickview.modal.getDraggable(properties); | |
var parentSelector = quickview.modal.getParentSelector(properties); | |
var dialog = BootstrapDialog.show({ | |
size: size, | |
title: title, | |
buttons: buttons, | |
closable: closable, | |
draggable: draggable, | |
message: data.output, | |
data: { | |
ctx: config.ctx, | |
url: config.actionUrl, | |
action: action, | |
index: index, | |
loop: loop, | |
selector: config.selector, | |
parentSelector: parentSelector | |
}, | |
onshow: function(dialogRef){ | |
if (properties.onshowCustomJs) { | |
new Function('return ' + properties.onshowCustomJs)(); | |
} | |
}, | |
onshown: function(dialogRef){ | |
if (properties.onshownCustomJs) { | |
new Function('return ' + properties.onshownCustomJs)(); | |
} | |
}, | |
onhide: function(dialogRef){ | |
if (properties.onhideCustomJs) { | |
new Function('return ' + properties.onhideCustomJs)(); | |
} | |
}, | |
onhidden: function(dialogRef){ | |
if (properties.onhiddenCustomJs) { | |
new Function('return ' + properties.onhiddenCustomJs)(); | |
} | |
} | |
}); | |
dialog.getModalHeader().hide(); | |
dialog.getModalFooter().hide(); | |
return false; | |
}); | |
}, | |
getResponse: function(url, action, hash, id) { | |
var request = $.ajax({ | |
url: url, | |
type: 'POST', | |
dataType: 'json', | |
async: false, | |
cache: false, | |
data: { | |
ctx: quickview.config.ctx, | |
action: action, | |
hash: hash, | |
id: id | |
}, | |
success: function(response) {} | |
}); | |
request.done(function(response) {}); | |
if (!!request.responseJSON && !!request.responseJSON.data) { | |
return request.responseJSON.data; | |
} | |
return false; | |
}, | |
getNeighbor: function(modal, direction) { | |
var url = modal.getData('url'); | |
var action = modal.getData('action'); | |
var index = modal.getData('index'); | |
var loop = modal.getData('loop'); | |
var selector = modal.getData('selector'); | |
var parentSelector = modal.getData('parentSelector'); | |
var items = {}; | |
if (parentSelector != '') { | |
items = $(parentSelector).find(selector); | |
} | |
else { | |
items = $(document).find(selector); | |
} | |
var countItems = items.size(); | |
switch (true) { | |
case countItems <= 1: | |
return false; | |
case !loop && index == 0 && direction == quickview.defaults.direction.prev: | |
return false; | |
case !loop && index == (countItems - 1) && direction == quickview.defaults.direction.next: | |
return false; | |
} | |
var neighbors = { | |
item: {}, | |
index: {} | |
}; | |
var work = {}; | |
if (index !== (countItems - 1)) { | |
neighbors.item.next = items[index + 1]; | |
neighbors.index.next = index + 1; | |
} else { | |
neighbors.item.next = items[0]; | |
neighbors.index.next = 0; | |
} | |
if (index == 0) { | |
neighbors.item.prev = items[countItems - 1]; | |
neighbors.index.prev = countItems - 1; | |
} else { | |
neighbors.item.prev = items[index - 1]; | |
neighbors.index.prev = index - 1; | |
} | |
switch (direction) { | |
case quickview.defaults.direction.prev: | |
work.item = neighbors.item.prev; | |
work.index = neighbors.index.prev; | |
break; | |
case quickview.defaults.direction.next: | |
work.item = neighbors.item.next; | |
work.index = neighbors.index.next; | |
break; | |
} | |
if (!work.item) { | |
return false; | |
} | |
var id = $(work.item).data('id'); | |
var hash = $(work.item).data('hash'); | |
var data = quickview.getResponse(url, action, hash, id); | |
if (!data) { | |
return false; | |
} | |
modal.setData('index', work.index); | |
return data; | |
}, | |
modal: { | |
getSize: function(config) { | |
var size = quickview.defaults.size.normal; | |
if (config.bootstrapDialogSize) { | |
size = config.bootstrapDialogSize; | |
} | |
return size; | |
}, | |
getTitle: function(config) { | |
var title = quickview.defaults.text.title; | |
if (config.title) { | |
title = config.title; | |
} | |
return title; | |
}, | |
getButtons: function(config) { | |
var buttons = []; | |
if (config.withNeighbors != '0') { | |
buttons.push({ | |
label: quickview.defaults.text.prev, | |
cssClass: quickview.defaults.selectors.prev, | |
hotkey: 37, | |
action: function(modal) { | |
var neighbor = quickview.getNeighbor(modal, quickview.defaults.direction.prev); | |
if (!!neighbor) { | |
modal.setMessage(neighbor.output); | |
} | |
} | |
}, { | |
label: quickview.defaults.text.next, | |
cssClass: quickview.defaults.selectors.next, | |
hotkey: 39, | |
action: function(modal) { | |
var neighbor = quickview.getNeighbor(modal, quickview.defaults.direction.next); | |
if (!!neighbor) { | |
modal.setMessage(neighbor.output); | |
} | |
} | |
}) | |
} | |
buttons.push({ | |
label: quickview.defaults.text.close, | |
cssClass: quickview.defaults.selectors.close, | |
action: function(modal) { | |
modal.close(); | |
} | |
}); | |
return buttons; | |
}, | |
getClosable: function(config) { | |
var closable = true; | |
if (!!!config.closable) { | |
closable = config.closable; | |
} | |
return closable; | |
}, | |
getDraggable: function(config) { | |
var draggable = true; | |
if (!!!config.draggable) { | |
closable = config.draggable; | |
} | |
return draggable; | |
}, | |
getLoop: function(config) { | |
var loop = true; | |
if (!!!config.withLoop) { | |
loop = config.withLoop; | |
} | |
return loop; | |
}, | |
getParentSelector: function(config) { | |
var parentSelector = ''; | |
if (config.parentSelector) { | |
parentSelector = config.parentSelector; | |
} | |
return parentSelector; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment