var App = { cartHighlightEnabled: false, currentPage: null, slogans: ['order food in 5 seconds'], service: '/api/', cached: {}, cart: {}, community: null, page: {}, config: null, order: { cardChanged: false, pay_type: 'card', delivery_type: 'delivery', tip: '10' }, _init: false, _pageInit: false }; $(function() { $('.admin-restaurant-link').live('click',function() { App.loadRestaurant($(this).attr('data-id_restaurant')); }); App.loadRestaurant = function(id_restaurant) { App.cache('Restaurant', id_restaurant , function() { var restaurant = this; var checkswap = { 'delivery_fee_check' : 'delivery_fee', 'delivery_min_check': 'delivery_min', 'fee_restaurant_check': 'fee_restaurant', 'fee_customer_check': 'fee_customer', 'id_community_check': 'id_community' }; $('.admin-restaurant-form input, .admin-restaurant-form select, .admin-restaurant-form textarea').each(function() { if ($(this).attr('type') == 'checkbox') { if (restaurant[$(this).attr('name')] == 1 && $(this).attr('value') == '1') { $(this).click(); } if (restaurant[$(this).attr('name')] == 0 && $(this).attr('value') == '0') { $(this).click(); } } else { $(this).val(restaurant[$(this).attr('name')]); } for (var x in checkswap) { if ($(this).attr('name') == x) { if (restaurant[checkswap[x]] && restaurant[checkswap[x]] != '0') { //$('input[name="' + x + '"][value="0"]').prop('checked', false); //$('input[name="' + x + '"][value="1"]').prop('checked', true); $('input[name="' + x + '"][value="1"]').click(); } else { //$('input[name="' + x + '"][value="0"]').prop('checked', true); //$('input[name="' + x + '"][value="1"]').prop('checked', false); $('input[name="' + x + '"][value="0"]').click(); } } } }); App.restaurant = restaurant.id_restaurant; $('.admin-restaurant-content').html(''); var categories = restaurant.categories(); var isDishes = false; var $categoriesContainer = $('
'); $('.admin-restaurant-dishes .admin-restaurant-content').append($categoriesContainer); for (var i in categories) { var dishes = categories[i].dishes(); var $categoryTab = $('

'+ categories[i].name+'

'); $categoriesContainer.append($categoryTab); for (var x in dishes) { App.showDish(dishes[x]); isDishes = true; } } $('.accordion').accordion({ collapsible: true, active: false, heightStyle: "content", beforeActivate: function( event, ui ) { console.log('antes', event, ui); }, activate: function( event, ui ){ console.log('entro', event, ui); var speed = 100; var accordionOptions = $('.accordion').accordion('option'); setTimeout(function() { $('.accordion').accordion('destroy'); $('.accordion').accordion(accordionOptions); }, 1.1 * speed); } }); // $('.accordion .ui-accordion-content').sortable().disableSelection(); if (!isDishes) { $('input[name="dish_check"][value="0"]').prop('checked', true); $('input[name="dish_check"][value="1"]').prop('checked', false); $('.admin-restaurant-dishes').hide(); } else { $('input[name="dish_check"][value="0"]').prop('checked', false); $('input[name="dish_check"][value="1"]').prop('checked', true); $('.admin-restaurant-dishes').show(); } var days = { 'sun': 'Sunday', 'mon': 'Monday', 'tue': 'Tuesday', 'wed': 'Wednesday', 'thu': 'Thursday', 'fri': 'Friday', 'sat': 'Saturday' }; for (var d in days) { var day = $('
' + days[d] + '
'); var dayWrap = $('
').appendTo(day); dayWrap.after('
'); if (!restaurant._hours) { $('input[name="hours_check"][value="0"]').prop('checked', true); $('input[name="hours_check"][value="1"]').prop('checked', false); $('.admin-restaurant-hours').hide(); } else { $('input[name="hours_check"][value="0"]').prop('checked', false); $('input[name="hours_check"][value="1"]').prop('checked', true); $('.admin-restaurant-hours').show(); var dayitem = restaurant._hours[d]; for (var x in dayitem) { var row = $('
'); row.append('' + ' TO ' + ' '); dayWrap.append(row); } } var row = $('
'); row.append(' TO '); dayWrap.append(row); $('.admin-restaurant-hours').append(day); } }); }; /** * Adds a new hours range if they are all filled up * * @return void */ $('.hours-date-hour input').live('keyup', function() { var allfull = true; $(this).closest('.hours-date-hours').find('input').each(function() { if ($(this).val() == '') { allfull = false; } }); if (allfull) { var day = $(this).attr('name').replace(/-open|-close/,''); var row = $('
'); row.append(' TO '); $(this).closest('.hours-date-hour').append(row); } }); var getValues = function(selector, restaurant) { $(selector).each(function() { var name, value, group = false; if ($(this).attr('name').match(/^.*\[\]$/)) { group = true; name = $(this).attr('name').replace(/^(.*)\[\]$/,'$1'); if (!restaurant[name]) { restaurant[name] = []; } } else { name = $(this).attr('name'); } if ($(this).attr('type') == 'checkbox' || $(this).attr('type') == 'radio') { value = $(this).prop('checked') ? true : false; } else { value = $(this).val(); } if (group) { restaurant[name][restaurant[name].length] = value; } else { restaurant[name] = value; } }); return restaurant; } $('.admin-restaurant-save').live('click', function() { saveRestaurant(true); }); $('.admin-restaurant-save-details').live('click', function() { saveRestaurant(false); }); $('.admin-restaurant-save-hours').live('click', function() { saveHours(); }); $('.admin-restaurant-save-dishes').live('click', function() { saveDishes(); }); var saveDishes = function(complete) { var selector = 'input.dataset-dish, select.dataset-dish, textarea.dataset-dish'; var dishes = []; $('.admin-food-item-wrap').each(function() { var id = $(this).attr('data-id_dish'); var values = getValues($(this).find(selector), {}); var dish = { name: values['dish-name'], description: values['dish-description'], price: values['dish-price'] }; if (id) { dish.id_dish = id; } dish.optionGroups = []; $(this).find('.admin-dish-options .admin-dish-options-wrapper').each(function() { var id = $(this).attr('data-parent'); var name = $(this).find('.admin-dish-options-title').html(); name = name.substr(0,name.length-1); var optionGroup = { name: name, 'default': values['dish-options-default'], type: $(this).attr('data-type'), price: $(this).attr('data-modifies_price') == 'true' ? true : false, options: [] }; if (id) { optionGroup.id_option = id; } $(this).find('.dish-options').each(function() { var id = $(this).attr('data-id_option'); var values = getValues($(this).find('input'), {}); if (values['dish-options-name']) { var option = { name: values['dish-options-name'], price: values['dish-options-price'] || 0.00, 'default': $(this).find('input[type="checkbox"], input[type="radio"]').prop('checked'), }; if (id) { option.id_option = id; } optionGroup.options[optionGroup.options.length] = option; } }); dish.optionGroups[dish.optionGroups.length] = optionGroup; }); dishes[dishes.length] = dish; }); $.post('/api/restaurant/' + App.restaurant + '/dishes', {dishes: dishes}, function() { if (complete) { complete(); } }); } var saveRestaurant = function(all) { var selector = 'input.dataset-restaurant, select.dataset-restaurant, textarea.dataset-restaurant'; var id = App.restaurant; if (id) { App.cache('Restaurant', id, function() { var restaurant = getValues(selector, this); restaurant.save(function() { if (all) { saveHours(function() { saveDishes(function() { }); }); } }); }); } else { var restaurant = getValues(selector, {}); restaurant = new Restaurant(restaurant); restaurant.save(function(r) { App.cache('Restaurant', r.id_restaurant, function() { App.restaurant = this.id_restaurant; if (all) { saveHours(function() { saveDishes(function() { location.href = '/admin/restaurants/' + App.restaurant; }); }); } }); }); } }; var saveHours = function(complete) { var selector = '.hours-date-hour input'; var id = App.restaurant; if (id) { App.cache('Restaurant', id, function() { var h = getValues(selector, {}); var hours = {'sun': [],'mon': [],'tue': [],'wed': [],'thu': [],'fri': [],'sat': []}; var vals = getValues('input.dataset-restaurant', {}); if (vals.hours_check) { for (var d in hours) { for (var x in h[d + '-open']) { if (!h[d + '-open'][x]) continue; hours[d][hours[d].length] = [App.unFormatTime(h[d + '-open'][x]), App.unFormatTime(h[d + '-close'][x])]; } } } $.post('/api/restaurant/' + id + '/hours', {hours: hours}, function() { if (complete) { complete(); } }); }); } }; $('.admin-restaurant-hours-save-all').live('click',function() { $('.admin-restaurant-hours-save-link').click(); }); $('.check label').live('click',function() { $(this).closest('.check').find('input').click(); }); $('.order-range-all label').live('click', function() { $(this).parent().find('input').click(); }); $('[name="phone"]').live('keyup', function(e) { $(this).val( App.phone.format($(this).val()) ); }); var changeACheck = function() { var name = $(this).attr('name'); var parent = $(this).closest('.content-sub').length ? $(this).closest('.content-sub') : $(this).closest('.content-primary'); parent.find('input[name="' + name + '_check"][value="1"]').prop('checked', true); parent.find('input[name="' + name + '_check"][value="0"]').prop('checked', false); }; $('.change-a-check').live('change', changeACheck).live('keyup', changeACheck); $('.bind-a-check').click(function(e) { var name = $(this).attr('name'); var value = $(this).attr('value'); var parent = $(this).closest('.content-sub').length ? $(this).closest('.content-sub') : $(this).closest('.content-primary'); $(this).prop('checked', true); parent.find('input[name="' + name + '"][value="' + (value == '1' ? '0' : '1') + '"]').prop('checked', false); if (value == '1') { parent.find('.check-content').fadeIn(); } else { parent.find('.check-content').fadeOut(100); } e.stopPropagation(); return false; }); if ($('.date-picker').length) { var d = $('.date-picker').val(); d = d.split(','); $('.date-picker').DatePicker({ format: 'm/d/Y', date: d, current: d[0], starts: 1, mode: 'range', calendars: 2, position: 'r', onBeforeShow: function(){ //$('.date-picker').DatePickerSetDate($('.date-picker').val(), true); }, onChange: function(formated, dates){ $('.date-picker').val(formated); if ($('#closeOnSelect input').attr('checked')) { $('.date-picker').DatePickerHide(); } } }); } $('input[name="order-range-all"]').live('change', function() { if ($(this).prop('checked')) { $('.date-picker').attr('disabled', 'disabled'); $('.date-picker').val(''); } else { $('.date-picker').removeAttr('disabled'); } }); $('.hours-date-hour input').live('change', function() { $(this).val(App.formatTime($(this).val())); }); /** * What to do when clicking a dish * * @todo refactorize how the accordion should be redrawn in a private method */ $('.admin-food-item').live('click', function() { var speed = 100; $(this).closest('.admin-food-item-wrap').find('.admin-food-item-content').slideToggle(speed); $(this).toggleClass('admin-food-item-collapsed'); // re-draws the dishes accordion after expanding/collapsing dish var accordionOptions = $('.accordion').accordion('option'); setTimeout(function() { $('.accordion').accordion('destroy'); $('.accordion').accordion(accordionOptions); }, 1.1 * speed); }); var ignoreKeys = [37,38,39,40,16,9]; //,17,18,91,13,16 var cleanInput = function(e) { if (ignoreKeys.indexOf(e.which) !== -1) { return; } var cleaned = App.cleanInput($(this).val(), $(this).attr('data-clean_type') || 'text'); var caret = $(this).getCursorPosition(); $(this).val(cleaned); if (e.type == 'keyup') { $(this).setCursorPosition(caret); } }; $('.clean-input').live('keyup', cleanInput).live('change', cleanInput); var changeDish = function(e) { $(this).closest('.admin-food-item-wrap').find('.food-name').html($(this).val()); }; $('.dish-name').live('keyup', changeDish).live('change', changeDish); var changePrice = function(e) { $(this).closest('.admin-food-item-wrap').find('.food-price-num').html($(this).val()); }; $('.dish-price input').live('keyup', changePrice).live('change', changePrice); $('.control-link-add').live('click', function() { App.showDish({}); }); $('.admin-food-item-delete').live('click', function() { var parent = $(this).closest('.admin-food-item-wrap'); var id_dish = parent.attr('data-id_dish'); var name = parent.find('.dish-name').val(); var remove = function() { parent.fadeOut(100,function() { $(this).remove(); }); }; if (!id_dish) { remove(); } else { if (confirm('Are you sure you want to delete "' + name + '"')) { remove(); } } }); $('.dish-options-delete').live('click', function() { var parent = $(this).closest('.dish-options'); var id_option = parent.attr('data-id_option'); var name = parent.find('input[name="dish-options-name"]').val(); var remove = function() { parent.fadeOut(100,function() { $(this).remove(); }); }; if (!id_option) { remove(); } else { if (confirm('Are you sure you want to delete "' + name + '"')) { remove(); } } }); $('.admin-dish-options-wrapper input[type="text"]').live('keyup', function() { var allfull = true; $(this).closest('.admin-dish-options-wrapper').find('.dish-options').each(function() { var selfComplete = true; $(this).find('input[type="text"]').each(function() { if ($(this).val() == '' || !$(this).val()) { allfull = selfComplete = false; } }); if (selfComplete) { $(this).addClass('blue'); } }); if (allfull) { $(this).closest('.input-faker').append(App.returnOption({price: '', name: '', id_option: ''},$(this).closest('.admin-dish-options-wrapper').attr('data-type'),$(this).closest('.admin-dish-options-wrapper').attr('data-parent'))); } }); $('.control-link-add-option').live('click', function() { var self = $(this); $('#dialog-option-group').dialog({ resizable: false, height: 250, width: 400, modal: true, buttons: { 'Create': function() { if ($(this).find('[name="admin-option-name"]').val()) { App.createOptionGroup(this, self); $(this).dialog('close'); $(this).find('[name="admin-option-name"]').val(''); $(this).find('[name="admin-option-price"]').removeAttr('checked'); $(this).find('[name="admin-option-type"]').val('check'); } }, Cancel: function() { $(this).dialog('close'); $(this).find('[name="admin-option-name"]').val(''); $(this).find('[name="admin-option-price"]').removeAttr('checked'); $(this).find('[name="admin-option-type"]').val('check'); } } }); }); $('.control-link-add-menu').live('click', function() { $('#dialog-add-menu').dialog({ resizable: false, height: 160, width: 315, modal: true, buttons: { 'Create': function() { if ($(this).find('[name="admin-option-name"]').val()) { App.createOptionGroup(this); $(this).dialog('close'); $(this).find('[name="admin-option-name"]').val(''); $(this).find('[name="admin-option-price"]').removeAttr('checked'); $(this).find('[name="admin-option-type"]').val('check'); } }, Cancel: function() { $(this).dialog('close'); $(this).find('[name="admin-option-name"]').val(''); $(this).find('[name="admin-option-price"]').removeAttr('checked'); $(this).find('[name="admin-option-type"]').val('check'); } } }); }); }); /** * Generates HTML to show dish and it's items * * @todo Not sure if hide purges HTML or what. */ App.showDish = function(dishItem) { if (!dishItem.id_dish) { dishItem = { 'name': '', 'description': '', 'id_dish': '', 'price': '' }; } var dish = $(''); dish.append('
' + dishItem.name + '($' + dishItem.price + ')
') var content = $('
'); var padding = $('
'); dish.append(content); content.append(padding); var options = $('
'); var basicOptions = $('
'); var basicWrapper = $('
Basic options:
') .append(basicOptions); var optGroups = []; if (dishItem.options) { var opts = dishItem.options(); options.append(basicWrapper); for (var x in opts) { var option = opts[x]; if (option.id_option_parent) { continue; } if (option.type == 'check') { basicOptions.append(App.returnOption(option,option.type)); } else if (option.type == 'select') { var optionAdder = $('
'); var optionWrapper = $('
' + option.name + ':
') .append(optionAdder); var select = $('') .append('
') .append('') .append('
') .append(options); content .append('
') .append('
Delete
') .append('
'); $('[data-id_category="'+ dishItem.id_category +'"] + div').append(dish); if (!dishItem.id_dish) { dish.find('.dish-name').focus(); dish.fadeIn(200); } }; App.createOptionGroup = function(el, source) { el = $(el); var parent = source.closest('.admin-food-item-wrap'); var option = { name: el.find('[name="admin-option-name"]').val(), price: el.find('[name="admin-option-price"]').attr('checked') ? true : false, type: el.find('[name="admin-option-type"]').val(), id_option: '', id: '' }; var optionAdder = $('
'); var optionWrapper = $('
' + option.name + ':
') .append(optionAdder); optionAdder.append(App.returnOption({price: '',name:'',id_option:''}, option.type, option.id_option)); parent.find('.admin-dish-options .admin-restaurant-options-controls').before(optionWrapper); }; /* App.addOptionGroup = function(option) { var optionAdder = $('
'); var optionWrapper = $('
' + option.name + ':
') .append(optionAdder); var select = $(''; break; default: case 'check': defaulted = ''; break; } return $('
' + '
' + '
' + defaulted + '' + '
$
' + '' + '' + '
' + '
' + '
'); }; App.orders = { params: function() { return { search: $('input[name="order-search"]').val(), env: $('select[name="env"]').val(), processor: $('select[name="processor"]').val(), limit: $('input[name="limit"]').val(), dates: $('input[name="date-range"]').val(), restaurant: $('select[name="restaurant"]').val(), community: $('select[name="community"]').val() }; }, load: function() { //admin-orders-filter $('.orders-loader').show(); $('.orders-content').html(''); $.ajax({ url: '/admin/orders/content', data: App.orders.params(), complete: function(content) { $('.orders-content').html(content.responseText); $('.orders-loader').hide(); } }); }, export: function() { var params = App.orders.params(); params.export = 'csv'; location.href = '/admin/orders/content?' + jQuery.param(params); } }; App.suggestions = { params: function() { return { search: $('input[name="suggestion-search"]').val(), type: $('select[name="type"]').val(), status: $('select[name="status"]').val(), limit: $('input[name="limit"]').val(), dates: $('input[name="date-range"]').val(), restaurant: $('select[name="restaurant"]').val(), community: $('select[name="community"]').val() }; }, load: function() { $('.suggestions-loader').show(); $('.suggestions-content').html(''); $.ajax({ url: '/admin/suggestions/content', data: App.suggestions.params(), complete: function(content) { $('.suggestions-content').html(content.responseText); $('.suggestions-loader').hide(); } }); }, prepareForm: function( id_suggetion ){ $( '.admin-suggestion-save' ).live( 'click', function(){ $( '#suggestion-status' ).html( '' ); var status = $( '#status' ).val(); var data = { 'status' : status }; var url = App.service + 'suggestion/' + id_suggetion; $.ajax({ type: "POST", dataType: 'json', data: data, url: url, success: function(content) { $( '#suggestion-status' ).html( 'Status saved!' ); }, error: function( ){ $( '#suggestion-status' ).html( 'Error, please try it again.' ); } }); } ); } }; (function($) { $.fn.getCursorPosition = function() { var input = this.get(0); if (!input) return; // No (input) element found if ('selectionStart' in input) { // Standard-compliant browsers return input.selectionStart; } else if (document.selection) { // IE input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length); return sel.text.length - selLen; } } $.fn.setCursorPosition = function(position){ if(this.length == 0) return this; return $(this).setSelection(position, position); } $.fn.setSelection = function(selectionStart, selectionEnd) { if(this.length == 0) return this; input = this[0]; if (input.createTextRange) { var range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); } else if (input.setSelectionRange) { input.focus(); input.setSelectionRange(selectionStart, selectionEnd); } return this; } })(jQuery);