(function(window, document, $, undefined) { console.log('wertyz-web.js started'); //Detect iframe load and redirect parent window, will work if same origin if(window != window.parent) { window.parent.location.href = window.location.href; return; } let wertyzMessageTimer = null; //Helper methods const Wertyz = { defaults: { defaultHandler: function(handlerName) { console.warn('No handler "' + handlerName + '" found'); } }, checks: { props: function(r, ...names) { let result = true; names.forEach(name => { result &= r.hasOwnProperty(name); }); return result; }, is$: function(elem) { return (elem instanceof $); } }, utils: { trycall: function(callable, ...args) { if(typeof callable === 'function') { return callable.apply(null, args); } console.warn('Not callable!', callable); }, to$: function(elem) { return $(elem); }, ajax: function(url, method, data) { let successHandler = function(data, string, xhr) { if(typeof data === 'object') { if(Wertyz.checks.props(data, 'status')) { switch(data.status) { case true: case 200: { break; } case false: case 400: case 500: { return; } } } if(Wertyz.checks.props(data, 'message')) { Wertyz.showWertyzMessage(data.message, 5000); } if(Wertyz.checks.props(data, 'action')) { if(data.action === 'reload') { $(window).off('beforeunload'); location.reload(); return; } else if(data.action === 'redirect' && Wertyz.checks.props(data, 'url')) { $(window).off('beforeunload'); location.replace(data.url); return; } } } }; let ajaxRequest = { type: method, url : url, data: data, timeout: 30000, success: successHandler, } return $.ajax(ajaxRequest); }, refreshContent(url, $target, showLoader = true) { if(typeof url === 'undefined' || typeof $target === 'undefined') { console.error('RefreshContent does not have required arguments', this); return; } if(showLoader) { Wertyz.showLoader(); } return Wertyz.utils.ajax(encodeURI(url), 'POST').done(function(data) { $target.html(data); Wertyz.ui.bind($target); }).fail(function() { Wertyz.showWertyzMessage('Upss...', 5000); }).always(function(){ if(showLoader) { Wertyz.hideLoader(); } }); }, formSubmit: function($form, url, defaultSuccessMessage = true) { if(typeof $form === 'undefined'){ console.error('Supplied form selector is undefined'); return; } if(!$form.is('form')) { console.error('Context is not a form'); return; } let action = url || $form.attr('action'); if(typeof action === 'undefined') { console.error('Url is not set and form action is undefined'); return; } Wertyz.showLoader(); return Wertyz.utils.ajax(encodeURI(action), 'POST', $form.serialize()).done(function(data){ if(defaultSuccessMessage) { Wertyz.showWertyzMessage('Hotovo', 5000); } $form.trigger('wertyz.postSubmit'); }).fail(function(){ Wertyz.showWertyzMessage('Upss...', 5000); }).always(function(){ Wertyz.hideLoader(); }); }, modalFormSubmit: function($form, url) { if(typeof $form === 'undefined'){ console.error('Supplied form selector is undefined'); return; } if(!$form.is('form')) { console.error('Context is not a form'); return; } let action = url || $form.attr('action'); if(typeof action === 'undefined') { console.error('Url is not set and form action is undefined'); return; } $('#wertyz-modal .wertyz-loader').addClass('active'); return Wertyz.utils.ajax(encodeURI(action), 'POST', $form.serialize()).done(function(data){ if(typeof data !== 'object' && data.length) { Wertyz.showModal(data); } else { Wertyz.showWertyzMessage('Hotovo', 5000); } $form.trigger('wertyz.postSubmit'); }).fail(function(){ Wertyz.showWertyzMessage('Upss...', 5000); }).always(function(){ Wertyz.hideModal(); $('#wertyz-modal .wertyz-loader').removeClass('active'); }); }, inputCheck: function($input, url) { if(typeof $input === 'undefined' || typeof url === 'undefined') { console.error('Supplied input selector or check url is undefined'); return; } if(!$input.is('input') && !$input.is('select') && !$input.is('textarea')) { console.error('Supplied input selector is not supported'); } url += '/term/' + $input.val(); return Wertyz.utils.ajax(encodeURI(url), 'POST').done(function(data){ if(typeof data !== 'object') { return; } if(Wertyz.checks.props(data, 'success') && data.success === 1) { $input.removeClass('error'); } else if(Wertyz.checks.props(data, 'error')) { $input.addClass('error'); } }); }, formCheck: function() { $form = $(this); if(typeof $form === 'undefined' || !$form.is('form')) { return false; } let $submit = $form.find('[type=submit]'); if(typeof $submit === 'undefined') { return false; } let invalid = false; $form.find('[required]').each(function(){ invalid |= $(this).hasClass('error'); invalid |= ($(this).val().length == 0); }); $submit.each(function(){ this.disabled = invalid; }); } }, showLoader: function(elem) { if(typeof elem !== 'undefined') { if(!Wertyz.checks.is$(elem)) { elem = Wertyz.utils.to$(elem); } let loader = (elem.hasClass('.wertyz-loader')) ? elem : elem.find('.wertyz-loader'); if(typeof loader !== 'undefined') { loader.delay(500).queue(function(){ $(this).addClass('active').dequeue(); }); return; } } $('#wertyz-main-loader').delay(500).queue(function(){ $(this).addClass('active').dequeue(); }); }, hideLoader: function(elem) { if(typeof elem !== 'undefined') { if(!Wertyz.checks.is$(elem)) { elem = Wertyz.utils.to$(elem); } var loader = (elem.hasClass('.wertyz-loader')) ? elem : elem.find('.wertyz-loader'); if(typeof loader !== 'undefined') { loader.clearQueue().stop().removeClass('active'); return; } } $('#wertyz-main-loader').clearQueue().stop().removeClass('active'); }, showWertyzMessage: function(message, timeout){ if(message === 'undefined') { return; } if(wertyzMessageTimer !== null) { clearTimeout(wertyzMessageTimer); } $('#wertyz-message .wertyz-message-content').html(message); $('#wertyz-message').addClass('active'); if(typeof timeout !== 'undefined') { wertyzMessageTimer = setTimeout(function() { $('#wertyz-message').removeClass('active'); wertyzMessageTimer = null; }, timeout); } }, hideWertyzMessage: function() { if(wertyzMessageTimer !== null) { clearTimeout(wertyzMessageTimer); } $('#wertyz-message .wertyz-message-content').html(); $('#wertyz-message').removeClass('active'); }, showModal: function(content) { if(typeof content !== 'undefined') { var modalContent = $('#wertyz-modal .modal-body-content') modalContent.html(content); Wertyz.ui.bind(modalContent); } $('#wertyz-modal').modal('show').on('hidden.bs.modal', function() { $('.modal-body-content').empty(); $(this).find('.wertyz-modal-message').hide(); }); }, setModal: function(content) { var modalContent = $('#wertyz-modal .modal-body-content'); modalContent.html(content); initWertyzTranslator(modalContent); Wertyz.ui.bind(modalContent); }, hideModal: function() { $('#wertyz-modal').modal('hide'); }, loadModal: function() { if($(this).attr('href') === 'undefined') { console.error('Modal trigger does not have href attribute', this); return; } Wertyz.showLoader(); Wertyz.utils.ajax($(this).attr('href'), 'POST').done(function(data){ if(typeof data !== 'object') { Wertyz.showModal(data); } }).fail(function(){ Wertyz.showWertyzMessage('Upps...'); Wertyz.hideModal(); }).always(function(){ Wertyz.hideLoader(); }); }, } window.Wertyz = Wertyz; //Click handlers / actions const _handlers = { dtDetailHide: function() { if(typeof this === 'undefined') { return; } let tr = $(this); let table = tr.closest('.wertyz-datatable'); let dt = table.DataTable(); let child = $(dt.row(this).child()); if(typeof child === 'undegined' || child.length == 0) { tr.removeClass('opened'); return; } let detailContent = child.find('.detail-content'); detailContent.on('hidden.bs.collapse', function(){ tr.removeClass('opened'); }); detailContent.collapse('hide'); }, dtDetail: function(r) { if(!Wertyz.checks.props(r, 'src')) { console.error('Missing required arguments!', this); return; } let tr = $(this); let table = $(this).closest('.wertyz-datatable'); let dt = table.DataTable(); let row = dt.row(tr); if(tr.hasClass('opened')) { _handlers.dtDetailHide.apply(tr); return; } tr.addClass('detail-header opened'); row.child('
').show(); let child = row.child(); child.addClass('wapp-intable-detail detail'); Wertyz.showLoader(child); Wertyz.utils.ajax(encodeURI(r.src), 'POST').done(function(data){ let detailContent = child.find('td').find('.detail-content'); detailContent.html('
' + data + '
'); Wertyz.ui.bind(child); detailContent.collapse('show'); }).fail(function(){ row.child().remove(); tr.removeClass('detail-header opened'); }).always(function(){ Wertyz.hideLoader(child); }); table.find('tr.opened').each(function(){ _handlers.dtDetailHide.apply($(this)); }); }, editRoleApplications: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-role-applications/oid/' + r.oid + '/id/' + r.id); }, editRole: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-role/oid/' + r.oid + '/id/' + r.id); }, editPhone: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-phone/oid/' + r.oid + '/id/' + r.id); }, editEmail: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-email/oid/' + r.oid + '/id/' + r.id); }, editMember: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-member/oid/' + r.oid + '/id/' + r.id); }, editMemberCredentials: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-member-credentials/oid/' + r.oid + '/id/' + r.id); }, editRolegroup: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-rolegroup/oid/' + r.oid + '/id/' + r.id); }, editApplicationIcon: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } var imageData = $(this).find('.cropit-image-editor').cropit('export', { type: 'image/jpeg', quality: 1, fillBg: '#fff', }); $(this).find('input[name=image]').val(imageData); Wertyz.utils.formSubmit($(this), '/organization/edit-application-icon/oid/' + r.oid + '/id/' + r.id); }, editOrganizationImage: function(r) { if(!Wertyz.checks.props(r, 'oid')) { console.error('Missing required arguments!', this); return; } var imageData = $(this).find('.cropit-image-editor').cropit('export', { type: 'image/jpeg', quality: 1, fillBg: '#fff', }); $(this).find('input[name=image]').val(imageData); Wertyz.utils.formSubmit($(this), '/organization/edit-image/oid/' + r.oid); }, editUserImage: function(r) { if(!Wertyz.checks.props(r, 'oid', 'uid')) { console.error('Missing required arguments!', this); return; } var imageData = $(this).find('.cropit-image-editor').cropit('export', { type: 'image/jpeg', quality: 1, fillBg: '#fff', }); $(this).find('input[name=image]').val(imageData); Wertyz.utils.formSubmit($(this), '/user/edit-image/uid/' + r.uid + '/oid/' + r.oid); }, editApplicationStarter: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/organization/edit-application-starter/oid/' + r.oid + '/id/' + r.id); }, editUser: function(r) { if(!Wertyz.checks.props(r, 'oid', 'uid')) { console.error('Missing required arguments!', this); return; } Wertyz.utils.formSubmit($(this), '/user/edit-profile/uid/' + r.uid + '/oid/' + r.oid).done(function(data){ if(typeof data !== 'object') { $('#general-tab-content').html(data); Wertyz.ui.bind($('#general-tab-content')); } }); }, refreshRoles: function(args) { Wertyz.utils.refreshContent('/organization/role-list/oid/' + args.oid, $(args.target)); }, refreshMembers: function(args) { Wertyz.utils.refreshContent('/organization/members-list/oid/' + args.oid, $(args.target)); }, refreshPhones: function(args) { Wertyz.utils.refreshContent('/organization/phones-list/oid/' + args.oid, $(args.target)); }, refreshEmails: function(args) { Wertyz.utils.refreshContent('/organization/emails-list/oid/' + args.oid, $(args.target)); }, refreshInvitedMembers: function(args) { Wertyz.utils.refreshContent('/organization/invited-members-list/oid/' + args.oid, $(args.target)); }, refreshExpiredMembers: function(args) { Wertyz.utils.refreshContent('/organization/expired-members-list/oid/' + args.oid, $(args.target)); }, refreshRolegroups: function(args) { Wertyz.utils.refreshContent('/organization/rolegroups-list/oid/' + args.oid, $(args.target)); }, refreshUserFavoriteApplications: function() { Wertyz.utils.refreshContent('/wertyz/favorite-applications-sidebar-list', $('#wertyz-sidebar #user-favorite-applications'), false); }, favoriteApplication: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } let control = $(this).addClass('pending'); let controlParent = $(this).closest('.wertyz-application-controls'); if(typeof controlParent !== 'undefined') { controlParent.addClass('pending'); } Wertyz.utils.ajax('/applicationmanager/add-favorite-application', 'POST', { oid: r.oid, id: r.id }).done(function(){ _handlers.refreshUserFavoriteApplications(); r.fn = 'removeFavoriteApplication'; control.attr('data-wertyz-click', JSON.stringify(r)); control.addClass('red').addClass('fa-heart-broken').removeClass('fa-heart'); }).always(function(){ control.removeClass('pending'); if(typeof controlParent !== 'undefined') { controlParent.removeClass('pending'); } }); }, removeFavoriteApplication: function(r) { if(!Wertyz.checks.props(r, 'oid', 'id')) { console.error('Missing required arguments!', this); return; } let control = $(this).addClass('pending'); let controlParent = $(this).closest('.wertyz-application-controls'); if(typeof controlParent !== 'undefined') { controlParent.addClass('pending'); } return Wertyz.utils.ajax('/applicationmanager/remove-favorite-application', 'POST', { oid: r.oid, id: r.id }).done(function(){ _handlers.refreshUserFavoriteApplications(); if(Wertyz.checks.props(r, 'postAction') && r.postAction == 'remove') { control.closest('.wertyz-application').fadeOut(300, function(){ $(this).remove(); }); return; } r.fn = 'favoriteApplication'; control.attr('data-wertyz-click', JSON.stringify(r)); control.removeClass('red').removeClass('fa-heart-broken').addClass('fa-heart'); }).always(function(){ control.removeClass('pending'); if(typeof controlParent !== 'undefined') { controlParent.removeClass('pending'); } }); } }; let _validators = { userLogin: function(args) { let url = '/registration/check-login'; if(typeof args !== 'undefined' && Wertyz.checks.props(args, 'oid')) { url += '/oid/' + args.oid; } return Wertyz.utils.inputCheck($(this), url); }, userEmail: function() { return Wertyz.utils.inputCheck($(this), '/registration/check-user-email'); }, organizationName: function() { return Wertyz.utils.inputCheck($(this), '/registration/check-organization-name'); }, organizationEmail: function() { return Wertyz.utils.inputCheck($(this), '/registration/check-organization-email'); }, password: function(args) { if(!Wertyz.checks.props(args, 'cmp') || typeof $(args.cmp) === 'undefined') { return; } if(!$(this).is('input[type=password]') && !$(arg.cmp).is('input[type=password]')) { return; } if(!$(args.cmp).val().length) { return; } if($(this).val() === $(args.cmp).val()) { $(this).removeClass('error'); $(args.cmp).removeClass('error'); } else { $(this).addClass('error'); } } } //Bindings $(document).ready(function(){ //Bind data-wertyz-click attribute $('#wertyz-wrap').on('click', '[data-wertyz-click]', function(e) { e.preventDefault(); let args = JSON.parse($(this).attr('data-wertyz-click') || '{}'); if(!Wertyz.checks.props(args, 'fn')) { console.warn("Wertyz click does't have function [fn] specified. Args: ", args); return; } let handler = _handlers[args.fn] || Wertyz.defaults.defaultHandler.bind(this, args.fn); //Debug info console.log('Wertyz click ----------------------------------------'); console.log('Args:', args); console.log('Handler:', handler); console.log('Scope:', this); console.log('-----------------------------------------------------'); handler.apply(this, [args]); }); $('#wertyz-wrap').on('submit', '[data-wertyz-submit]', function(e) { e.preventDefault(); let args = JSON.parse($(this).attr('data-wertyz-submit') || '{}'); if(!Wertyz.checks.props(args, 'fn')) { console.warn("Wertyz click does't have function [fn] specified. Args: ", args); return; } let handler = _handlers[args.fn] || Wertyz.defaults.defaultHandler.bind(this, args.fn); //Debug info console.log('Wertyz submit ---------------------------------------'); console.log('Args:', args); console.log('Handler:', handler); console.log('Scope:', this); console.log('-----------------------------------------------------'); handler.apply(this, [args]); }); $('#wertyz-wrap').on('click', '.wertyz-modal-trigger', function(e){ e.preventDefault(); Wertyz.loadModal.apply(this); }); $('#wertyz-modal').on('submit', 'form', function(e){ e.preventDefault(); let bindRefreshHandler = function(args) { if(Wertyz.checks.props(args, 'refresh', 'target')) { let refreshHandler = _handlers[args.refresh] || Wertyz.defaults.defaultHandler(args.refresh); $(this).on('wertyz.postSubmit', refreshHandler.bind(this, args)); } } let args = JSON.parse($(this).attr('data-wertyz-refresh') || '{}'); if(Array.isArray(args)) { args.forEach(a => bindRefreshHandler.call(this, a)); } else { bindRefreshHandler.call(this, args); } Wertyz.utils.modalFormSubmit($(this)); }); $('#wertyz-wrap').on('keyup', '[data-wertyz-validate]', function(e) { let args = JSON.parse($(this).attr('data-wertyz-validate') || '{}'); if(!Wertyz.checks.props(args, 'fn')) { console.warn('Wertyz input validator has no handler [fn] defined'); return; } let validator = _validators[args.fn] || Wertyz.defaults.defaultHandler(args.fn); //Debug info console.log('Wertyz validator ------------------------------------'); console.log('Args:', args); console.log('Handler:', validator); console.log('Scope:', this); console.log('-----------------------------------------------------'); let timeout = $(this).data('wertyz.validator.timeout'); if(typeof timeout !== 'undefined') { clearTimeout(timeout); } timeout = setTimeout(validator.bind(this, args), 500); $(this).data('wertyz.validator.timeout', timeout); }); setInterval(function(){ console.log('Wertyz keep alive...'); let url = "https://www.wertyz.com/wertyz/keep-alive"; $.post(encodeURI(url)); }, 600000); $(window).on('beforeunload', function(e){ if($.active > 0) { return "We are still working, please wait..."; } }); window.addEventListener('message', function(e){ console.log("Received", e.data, "from", e.origin); let data = null; try { data = JSON.parse(e.data); } catch(e) { console.error("Failed to parse message!"); return; } if(data === null) return; if(!'type' in data) { console.error("No type present in message data object!"); return; } switch(data.type) { case 'applicationCurrentLocation': { if(!'href' in data) return; let sessionStorage = JSON.parse(window.sessionStorage.getItem('wertyz')) || {}; sessionStorage.applicationCurrentLocation = { application: window.location.href, location: data.href }; window.sessionStorage.setItem('wertyz', JSON.stringify(sessionStorage)); break; } default: break; } }); }); })(window, document, jQuery);