Browse Source

epesi.js as class

experimental
Paweł Jedwabny 4 years ago
parent
commit
bac1f26b56
2 changed files with 81 additions and 67 deletions
  1. +79
    -60
      src/epesi.js
  2. +2
    -7
      src/index.js

+ 79
- 60
src/epesi.js View File

@@ -10,72 +10,85 @@ import axios from 'axios';
import qs from 'qs';
import ConfirmLeave from './confirmLeave';

const Epesi = {
loader: new Loader(),
confirmLeave: new ConfirmLeave(),
default_indicator:'loading...',
procOn:0,
client_id:0,
process_file:'process.php',
indicator:'epesiStatus',
indicator_text:'epesiStatusText',
updateIndicator: function() {
document.getElementById(Epesi.indicator).style.display = Epesi.procOn ? '' : 'none';
if (!Epesi.procOn) document.getElementById('main_content').style.display = '';
},
updateIndicatorText: function(text) {
document.getElementById(Epesi.indicator_text).innerHTML = text;
},
history_add:function(id){
class Epesi {
loader = new Loader();
confirmLeave = new ConfirmLeave();
default_indicator = 'loading...';
procOn = 0;
client_id = 0;
process_file = 'process.php';
indicator = 'epesiStatus';
indicator_text = 'epesiStatusText';


updateIndicator = () => {
document.getElementById(this.indicator).style.display = this.procOn ? '' : 'none';
if (!this.procOn) document.getElementById('main_content').style.display = '';
};

updateIndicatorText = (text) => {
document.getElementById(this.indicator_text).innerHTML = text;
document.getElementById(this.indicator_text).innerHTML = text;
};

history_add = (id) => {
window.history.pushState({history_id: id}, '');
},
init:function(cl_id,path,params) {
Epesi.client_id=cl_id;
Epesi.process_file=path;
};

init = (cl_id, path, params) => {
this.client_id=cl_id;
this.process_file=path;

axios.defaults.headers.common['X-Client-ID'] = cl_id;

Epesi.history_add(0);
jQuery(document).ajaxSend((ev, xhr, settings) => {
xhr.setRequestHeader('X-Client-ID', this.client_id);
});

this.history_add(0);
if(typeof params == 'undefined')
params = '';
Epesi.request(params,0);
this.request(params,0);

window.addEventListener('popstate', ({state: {history_id}}) => Epesi.request('', history_id));
},
request: async function (url, history) {
Epesi.procOn++;
Epesi.updateIndicator();
window.addEventListener('popstate', ({state: {history_id}}) => this.request('', history_id));
};

request = async (url, history) => {
this.procOn++;
this.updateIndicator();

let keep_focus_field = null;
if (document.activeElement) keep_focus_field = document.activeElement.getAttribute('id');

try {
let response = await axios.post(Epesi.process_file, qs.stringify({history, url}));
let response = await axios.post(this.process_file, qs.stringify({history, url}));
jQuery(document).trigger('e:loading');
eval(response.data);
} catch (err) {
Epesi.text(err.message, 'error_box', 'p')
this.text(err.message, 'error_box', 'p')
}

Epesi.procOn--;
Epesi.updateIndicator();
Epesi.append_js("jQuery(document).trigger('e:load')");
this.procOn--;
this.updateIndicator();
this.append_js("jQuery(document).trigger('e:load')");
if (keep_focus_field !== null) {
let element = document.getElementById(keep_focus_field);
if(element) element.focus();
}
},
href: function(url, indicator, mode, disableConfirmLeave = false) {
if (!disableConfirmLeave && !Epesi.confirmLeave.check()) return;
if(Epesi.procOn === 0 || mode === 'allow'){
!indicator ? Epesi.updateIndicatorText(Epesi.default_indicator) : Epesi.updateIndicatorText(indicator);
Epesi.request(url);
};

href = (url, indicator, mode, disableConfirmLeave = false) => {
if (!disableConfirmLeave && !this.confirmLeave.check()) return;
if(this.procOn === 0 || mode === 'allow'){
!indicator ? this.updateIndicatorText(this.default_indicator) : this.updateIndicatorText(indicator);
this.request(url);
} else if(mode === 'queue') {
setTimeout(() => Epesi.href(url, indicator, mode), 500);
setTimeout(() => this.href(url, indicator, mode), 500);
}
},
submit_form: function(formName, modulePath, indicator) {
Epesi.confirmLeave.freeze(formName);
}

submit_form = (formName, modulePath, indicator) => {
this.confirmLeave.freeze(formName);
let form = document.querySelector(`form[name="${formName}"]`);
let submited = form.querySelector(`input[name="submited"]`);

@@ -87,8 +100,9 @@ const Epesi = {
_chj(url, indicator, '');

submited.value = 0;
},
text: function(html, element_id, type = 'i') {
};

text = (html, element_id, type = 'i') => {
let element = document.getElementById(element_id);
if(!element) return;

@@ -103,24 +117,29 @@ const Epesi = {
element.insertAdjacentHTML('beforeend', html);
break;
}
},
load_js:function(file) {
Epesi.loader.load_js(file);
},
append_js:function(texti) {
Epesi.loader.execute_js(texti);
},
append_js_script:function(texti) {
};

load_js = file => {
this.loader.load_js(file);
};

append_js = texti => {
this.loader.execute_js(texti);
};

append_js_script = texti => {
console.warn('DEPRECATED: use Loader.execute_js instead');
Loader.insertScript(texti);
},
js_loader:function() {
};

js_loader = () => {
console.warn('DEPRECATED: load is invoked implicitly');
Epesi.loader.load();
},
load_css:function(file) {
Epesi.loader.load_css(file);
this.loader.load();
};

load_css =file => {
this.loader.load_css(file);
}
};
}

export default Epesi;

+ 2
- 7
src/index.js View File

@@ -36,12 +36,6 @@ window.wait_while_null = (id, action) => {
setTimeout('wait_while_null(\'' + addslashes(id) + '\', \'' + addslashes(action) + '\')', 200);
};

window._chj = Epesi.href;

jQuery(document).ajaxSend(function (ev, xhr, settings) {
xhr.setRequestHeader('X-Client-ID', Epesi.client_id);
});

window.getTotalTopOffet = e => {
let ret = 0;
while (e != null) {
@@ -61,4 +55,5 @@ window.is_visible = function (element) {
return true;
};

window.Epesi = Epesi;
window.Epesi = new Epesi();
window._chj = window.Epesi.href;

Loading…
Cancel
Save