'use strict'; /* |------------------------------------------------------------------------------ | Framework7 Plugins |------------------------------------------------------------------------------ */ Framework7.use(Framework7Keypad); /* |------------------------------------------------------------------------------ | Initialize App |------------------------------------------------------------------------------ */ var app = new Framework7({ root: '#app', id: window.config.app.id, name: window.config.app.title, version: window.config.app.version, theme: (function() { if (document.location.search.indexOf('theme=') >= 0) { return document.location.search.split('theme=')[1].split('&')[0]; } else { return 'auto'; } })(), rtl: false, routes: window.routes, lazyModulesPath: '/app/assets/vendor/framework7/lazy-components', init: false, data: function() { return { a2hs: null, config: window.config } }, actions: { closeByOutsideClick: true, convertToPopover: false }, autocomplete: { autoFocus: true, closeOnSelect: true, limit: 10, notFoundText: 'No Search Results', preloader: true, requestSourceOnOpen: true, searchbarPlaceholder: 'Search' }, calendar: { headerPlaceholder: 'Select', monthNames:['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'], monthNamesShort:['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'], dayNames:['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'], dayNamesShort:['Dom','Seg','Ter','Qua','Qui','Sex','Sáb'], dateFormat:'dd-mm-yyyy' }, dialog: { title: window.config.app.title }, lazy: { placeholder: 'data:image/svg+xml;charset=utf-8,', sequential: false }, listIndex: { label: true }, notification: { closeButton: true, icon: '', title: window.config.app.title }, panel: { swipe: false }, searchbar: { removeDiacritics: true }, sheet: { backdrop: true, closeByOutsideClick: true }, smartSelect: { closeOnSelect: true }, toast: { closeTimeout: 3000 }, touch: { fastClicksExclude: '.ap-dropdown-menu, .pac-container .pac-item' }, view: { pushState: true, pushStateSeparator: '#!' } }); /* |------------------------------------------------------------------------------ | App Events |------------------------------------------------------------------------------ */ app.on('init', function() { initializeServiceWorker(); initializeViews(); initializeTheme(); // initializeI18n(); initializeA2HS(); initializeBackButton(); getInternetConnectionStatus(); setAJAXDefaults(); setFormValidatorDefaults(); // initializeFacebookJsSdk(); }); app.on('pageInit', function() { localizeApp(); }); app.on('panelOpen', function() { app.$('.navbar .hamburger').addClass('is-active'); }); app.on('panelClose', function() { app.$('.navbar .hamburger').removeClass('is-active'); }); app.on('routerAjaxError', function() { app.toast.show({ text: 'Sem conexão com a Internet!', position: 'bottom', cssClass: 'bg-color-red' }); }); /* |------------------------------------------------------------------------------ | Initialize Service Worker |------------------------------------------------------------------------------ */ function initializeServiceWorker() { if ('serviceWorker' in navigator) { return navigator.serviceWorker.register('./service-worker.js') .then(function(registration) { console.log('Service worker successfully registered.'); return registration; }) .catch(function(err) { console.error('Unable to register service worker.', err); }); }else { console.log('Service workers não é suportado pelo navegador!.'); } } /* |------------------------------------------------------------------------------ | Initialize Views |------------------------------------------------------------------------------ */ function initializeViews() { var mainView = app.views.create('.view-main', { url: '/' }); if (window.config.theme.navigation == 'tabbar') { var componentsView = app.views.create('.view-components', { url: '/components' }); var screensView = app.views.create('.view-screens', { url: '/screens' }); var moreView = app.views.create('.view-more', { url: '/more' }); } } /* |------------------------------------------------------------------------------ | Initialize Theme |------------------------------------------------------------------------------ */ function initializeTheme() { var color = app.utils.theme.getColor(); var layout = app.utils.theme.getLayout(); app.utils.theme.setColor(color); app.utils.theme.setLayout(layout); } /* |------------------------------------------------------------------------------ | Initialize i18n |------------------------------------------------------------------------------ */ function initializeI18n() { var language = app.utils.i18n.getLanguage(); i18next .use(i18nextXHRBackend) .init({ lng: language.lang, fallbackLng: 'br', whitelist: ['br'], nonExplicitWhitelist: true, preload: ['br'], backend: { loadPath: '/app/assets/custom/i18n/{{lng}}.json' } }, function() { app.utils.i18n.setLanguage(language); }); } /* |------------------------------------------------------------------------------ | Localize App |------------------------------------------------------------------------------ */ function localizeApp() { if (window.localize) { window.localize('body'); } } /* |------------------------------------------------------------------------------ | Initialize Add to Home Screen |------------------------------------------------------------------------------ */ function initializeA2HS() { window.addEventListener('beforeinstallprompt', function(event) { event.preventDefault(); app.a2hs = event; var dialog = app.dialog.create({ title: '', content: '

Instale em seu dispositivo!
Permite acesso rápido e fácil quando você estiver em trânsito e funciona sem acesso à Internet.

', verticalButtons: true, buttons: [ { text: 'Instalar', bold: true, color: 'green', onClick: function() { app.a2hs.prompt(); app.a2hs.userChoice .then(function(choice) { if (choice.outcome == 'accepted') { app.toast.show({ text: 'Ok! Adicionado em seu dispositivo, arraste para sua tela inicial', position:'bottom', cssClass: 'toast-round bg-color-green' }); } else { app.toast.show({ text: 'Oops! Não foiível instalar o app!', position:'bottom', cssClass: 'toast-round bg-color-red' }); } app.a2hs = null; }); app.dialog.close(); } }, { text: 'Não Instalar', color: 'gray' } ] }); setTimeout(function() { dialog.open(); }, 6000 ); }); } /* |------------------------------------------------------------------------------ | Initialize Back Button |------------------------------------------------------------------------------ */ function initializeBackButton() { document.addEventListener('backbutton', function(event) { event.preventDefault(); var dismissibleModals = app.$('.actions-modal.modal-in').length + app.$('.login-screen.modal-in').length + app.$('.notification.modal-in').length + app.$('.panel-active').length + app.$('.popover.modal-in').length + app.$('.popup.modal-in').length + app.$('.sheet-modal.modal-in').length + app.$('.swipeout-opened').length + app.$('.td-wrap').length + app.$('.toast.modal-in').length + app.$('.tooltip.tooltip-in').length; var nonDismissibleModals = app.$('.dialog.modal-in').length; if (nonDismissibleModals) { return false; } else if (dismissibleModals) { app.actions.close(); app.loginScreen.close(); app.notification.close(); app.panel.close(); app.popover.close(); app.popup.close(); app.sheet.close(); app.swipeout.close(app.swipeout.el); app.$('.td-wrap').removeClass('td-show'); app.toast.close(); app.$('.tooltip').remove(); } else { var currentRoute = app.views.current.router.currentRoute.url; if (currentRoute == '/home' || currentRoute == '/screens/home') { app.dialog.confirm( '
Do you want to exit the app?
', '', function() { navigator.app.exitApp(); } ); } else if (app.data.config.theme.navigation == 'tabbar' && (currentRoute == '/components' || currentRoute == '/produtos'||currentRoute == '/screens' || currentRoute == '/more')) { app.tab.show('#tab-home'); } else { if (app.$('.page.page-current .navbar .back').length) { app.tab.show('#tab-home'); } else { return false; } } } }); } /* |------------------------------------------------------------------------------ | Get Internet Connection Status |------------------------------------------------------------------------------ */ function getInternetConnectionStatus() { window.addEventListener('online', function() { app.toast.show({ text: 'Conectado à Internet!', position: 'bottom', cssClass: 'bg-color-green' }); }); window.addEventListener('offline', function() { app.toast.show({ text: 'Sem conexão com a Internet!', position: 'bottom', cssClass: 'bg-color-red' }); }); } /* |------------------------------------------------------------------------------ | Set AJAX Defaults |------------------------------------------------------------------------------ */ function setAJAXDefaults() { app.request.setup({ beforeSend: function() { app.preloader.show(); }, complete: function() { app.preloader.hide(); } }); } /* |------------------------------------------------------------------------------ | Set Form Validator Defaults |------------------------------------------------------------------------------ */ function setFormValidatorDefaults() { jQuery.validator.setDefaults({ errorElement : 'div', errorPlacement: function(error, element) { error.appendTo(element.siblings('.input-error-message')); } }); } /* |------------------------------------------------------------------------------ | Initialize Facebook JavaScript SDK |------------------------------------------------------------------------------ */ function initializeFacebookJsSdk() { LazyLoad.js(['https://connect.facebook.net/en_US/sdk.js'], function() { window.fbAsyncInit = function() { FB.init({ appId: window.config.facebook.appId, autoLogAppEvents: true, xfbml: true, version: 'v3.3' }); }; app.on('pageInit', function() { if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {} else { FB.XFBML.parse(); } }); app.on('pageReInit', function() { if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {} else { FB.XFBML.parse(); } }); }); }