view.min.js (2578B)
1 import $ from '../jquery-global.min.js'; 2 import Callback from "./callback.min.js"; 3 import Form from "./form.min.js"; 4 import Notice from "./notice.min.js"; 5 import Workbench from "./workbench.min.js"; 6 export default class View { 7 constructor( action,method,id,params ) { 8 this.action = action; 9 this.method = method; 10 this.id = id; 11 this.params = params; 12 this.onCloseHandler = new Callback(); 13 this.onChangeHandler = new Callback(); 14 this.onSaveHandler = new Callback(); 15 } 16 before() { 17 }; 18 start( element ) { 19 this.before(); 20 this.element = element; 21 return this.loadView(); 22 } 23 afterLoad() { 24 } 25 close() { 26 this.onCloseHandler.fire(); 27 } 28 fireViewLoadedEvents(element) { 29 Callback.afterViewLoadedHandler.fire( element ); 30 } 31 async loadView() { 32 let url = View.createUrl(this.action, this.method, this.id, this.params); 33 let element = this.element; 34 let view = this; 35 console.debug( view ); 36 try { 37 let response = await fetch( url,{ 38 method: 'GET', 39 headers: { 40 'Accept': 'text/html', 41 } 42 } ); 43 $(element).html(""); 44 if ( ! response.ok ) { 45 $(element).html('<div class="or-view-central"><i class="or-image-icon or-image-icon--method-logout" /></div>'); 46 if ( response.status == 403 ) { 47 throw "Permission denied"; 48 } 49 else if ( response.status == 503 ) 50 throw "server error"; 51 else 52 throw "failed to load the view"; 53 } 54 let data = await response.text(); 55 if ( ! data ) 56 data = ''; 57 $(element).html(data); 58 $(element).find('form').each( function() { 59 let form = new Form(); 60 form.onChangeHandler.add( () => { view.onChangeHandler.fire() } ); 61 form.onSaveHandler .add( () => { view.onSaveHandler .fire() } ); 62 form.onCloseHandler .add( () => { view.close() } ); 63 form.forwardHandler.add( (action, subaction, id, data) => { 64 view.action = action; 65 view.method = subaction; 66 view.id = id; 67 view.params = data; 68 view.loadView(); 69 } ); 70 form.initOnElement(this); 71 }); 72 view.fireViewLoadedEvents( element ); 73 } 74 catch( cause ) { 75 console.error( {view:view, url:url, cause: cause} ); 76 let notice = new Notice(); 77 notice.setStatus('error'); 78 notice.msg = Workbench.language.NOTHING_DONE; 79 notice.log = cause; 80 notice.show(); 81 } 82 finally { 83 } 84 } 85 static createUrl(action, subaction, id= 0, extraid = {}) { 86 let url = './?'; 87 if(action) 88 url += '&action='+action; 89 if(subaction) 90 url += '&subaction='+subaction; 91 if(id) 92 url += '&id='+id; 93 if ( extraid instanceof FormData ) { 94 for (let pair of extraid.entries()) { 95 url += '&' + pair[0] + '=' + pair[1]; 96 } 97 } 98 else if ( extraid instanceof Object ) { 99 Object.keys(extraid).forEach( (key) => { 100 url += '&' + key + '=' + extraid[key]; 101 }); 102 } 103 else 104 throw "Illegal argument"; 105 return url; 106 } 107 }