Created
February 24, 2015 16:37
-
-
Save oleglukashev/8b2223d96548d622b6ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
getExtension = (filename) -> | |
parts = filename.split('.') | |
parts[parts.length - 1] | |
isImage = (filename) -> | |
ext = getExtension(filename) | |
$.inArray(ext.toLowerCase(), ['jpg', 'jpeg', 'gif', 'bmp', 'png']) > -1 | |
validation_errors = -> | |
$form = $('#new_reception') | |
$form.find('.error').remove() | |
errors_count = 0 | |
text_inputs = $form | |
.find('#reception_name,#reception_phone,#reception_email,textarea,input[type="text"][name$="[price]"]') | |
text_inputs.each -> | |
if $(this).val() is "" | |
$(this).addClass('reception-input-error') | |
errors_count += 1 | |
file_inputs = $form.find('input[type="file"]') | |
file_inputs.each -> | |
unless isImage($(this).val()) || ($(this).attr('id').match(/_0_image/) == null && $(this).val() is "") | |
$(this).closest('.receptions-form__photo').addClass('reception-input-error') | |
errors_count += 1 | |
errors_count | |
recalculateImgPos = (img) -> | |
verticalMargin = img.outerHeight()/2 | |
horizontalMargin = img.outerWidth()/2 | |
img.css | |
'margin-top': -verticalMargin | |
'margin-left': -horizontalMargin | |
previewUploadedimage = (input, appendWrap) -> | |
if input.files and input.files[0] | |
reader = new FileReader() | |
reader.onload = (e) -> | |
img = $('<img />', src: e.target.result) | |
$removeBtn = $('.js-remove-reception-photo') | |
appendWrap.find($removeBtn).siblings().hide() | |
$removeBtn.removeClass('is-hidden') | |
appendWrap.prepend(img) | |
if img.closest('.receptions-form__photo').hasClass('reception-input-error') | |
img.closest('.receptions-form__photo').removeClass('reception-input-error') | |
recalculateImgPos(img) | |
reader.readAsDataURL input.files[0] | |
appendFields = (appendTarget, appendTemplate, appendContext) -> | |
appendTarget.append(HandlebarsTemplates['reception_items/' + appendTemplate](appendContext)) | |
$ -> | |
if $('body.receptions-show').length | |
$(window).on 'load', () -> | |
$('.reception-success__request-item__image img').each -> | |
recalculateImgPos($(this)) | |
$(this).css | |
visibility: 'visible' | |
if $('body#receptions').length | |
unless $('#new_reception').hasClass('remote-form') | |
$('#new_reception').submit (e) -> | |
if validation_errors() > 0 | |
e.preventDefault() | |
else | |
$("#new_reception .receptions-form__controls .btn").on('click', (e, xhr) -> | |
if validation_errors() > 0 | |
e.preventDefault() | |
) | |
$(document).on 'click', '.js-add-reception-item', (e) -> | |
e.preventDefault() | |
$container = $(".receptions-form__items") | |
context = | |
i: $(".receptions-form__item").length | |
appendFields($container, 'new', context) | |
$(document).on 'click', '.js-remove-reception-item', (e) -> | |
e.preventDefault() | |
$(this).closest('.receptions-form__item').remove() | |
$(document).on 'change', '.receptions-form__photo-upload', () -> | |
$wrapper = $(this).closest('.receptions-form__photo') | |
previewUploadedimage(this, $wrapper) | |
$(document).on 'click', '.js-remove-reception-photo', (e) -> | |
e.preventDefault() | |
$(this).addClass('is-hidden') | |
$(this).siblings('input').val('') | |
$(this).siblings('img').remove() | |
$(this).siblings('.js-upload-reception-photo').show() | |
$(document).on 'click', '.js-upload-reception-photo', (e) -> | |
e.preventDefault() | |
$(this).siblings('input').trigger('click') | |
$('.receptions-form').on 'keydown', '#reception_name,#reception_phone,#reception_email,textarea,input[type="text"][name$="[price]"]', () -> | |
if $(this).hasClass('reception-input-error') | |
$(this).removeClass('reception-input-error') | |
$('.reception-login-popup').magnificPopup | |
items: | |
src: '#additional-auth-reg-block' | |
type: 'inline' | |
$('.reception-login-popup').magnificPopup | |
items: | |
src: '#additional-auth-reg-block' | |
type: 'inline' | |
$('.reception-trigger-popups a.btn').on 'click', (e) -> | |
e.preventDefault() | |
$('.reception-trigger-popups a.btn').removeClass('active') | |
$('.additional-registration-block,.additional-authorization-block').removeClass('active') | |
$(this).addClass('active') | |
if $(this).hasClass('reception-login-popup') | |
$('.additional-authorization-block').addClass('active') | |
else | |
$('.additional-registration-block').addClass('active') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment