view.js (3731B)
1 import $ from '../jquery-global.js'; 2 import Callback from "./callback.js"; 3 import Form from "./form.js"; 4 import Notice from "./notice.js"; 5 import Workbench from "./workbench.js"; 6 7 /** 8 * View. 9 * A view is a part of the page. An Action is loaded into this view. 10 * 11 * @param action 12 * @param method 13 * @param id 14 * @param params 15 * @constructor 16 */ 17 export default class View { 18 19 constructor( action,method,id,params ) { 20 this.action = action; 21 this.method = method; 22 this.id = id; 23 this.params = params; 24 25 this.onCloseHandler = new Callback(); 26 27 this.onChangeHandler = new Callback(); 28 this.onSaveHandler = new Callback(); 29 } 30 31 before() { 32 33 }; 34 35 /** 36 * @param element 37 * @returns {Promise} 38 */ 39 start( element ) { 40 41 this.before(); 42 this.element = element; 43 return this.loadView(); 44 } 45 46 afterLoad() { 47 48 } 49 50 close() { 51 52 this.onCloseHandler.fire(); 53 } 54 55 56 fireViewLoadedEvents(element) { 57 58 Callback.afterViewLoadedHandler.fire( element ); 59 } 60 61 62 /** 63 * Loads the content of this view 64 * 65 * @returns Promise 66 */ 67 async loadView() { 68 69 let url = View.createUrl(this.action, this.method, this.id, this.params); // URL für das Laden erzeugen. 70 let element = this.element; 71 let view = this; 72 73 //$(this.element).addClass('loader'); 74 console.debug( view ); 75 76 try { 77 let response = await fetch( url,{ 78 method: 'GET', 79 headers: { 80 'Accept': 'text/html', 81 } 82 } ); 83 $(element).html(""); 84 85 if ( ! response.ok ) { 86 87 $(element).html('<div class="or-view-central"><i class="or-image-icon or-image-icon--method-logout" /></div>'); 88 89 if ( response.status == 403 ) { 90 throw "Permission denied"; 91 } 92 else if ( response.status == 503 ) 93 throw "server error"; 94 else 95 throw "failed to load the view"; 96 97 } 98 99 let data = await response.text(); 100 101 if ( ! data ) 102 data = ''; 103 104 $(element).html(data); 105 106 $(element).find('form').each( function() { 107 108 let form = new Form(); 109 110 form.onChangeHandler.add( () => { view.onChangeHandler.fire() } ); 111 form.onSaveHandler .add( () => { view.onSaveHandler .fire() } ); 112 form.onCloseHandler .add( () => { view.close() } ); 113 114 form.forwardHandler.add( (action, subaction, id, data) => { 115 view.action = action; 116 view.method = subaction; 117 view.id = id; 118 view.params = data; 119 view.loadView(); 120 } ); 121 122 form.initOnElement(this); 123 }); 124 125 view.fireViewLoadedEvents( element ); 126 } 127 catch( cause ) { 128 129 console.error( {view:view, url:url, cause: cause} ); 130 131 let notice = new Notice(); 132 notice.setStatus('error'); 133 //notice.msg = Workbench.language.ERROR; 134 notice.msg = Workbench.language.NOTHING_DONE; 135 notice.log = cause; 136 notice.show(); 137 } 138 finally { 139 //$(element).removeClass("loader"); 140 } 141 } 142 143 144 145 146 /** 147 * Erzeugt eine URL, um die gewünschte Action vom Server zu laden. 148 * 149 * @param action 150 * @param subaction 151 * @param id 152 * @param extraid 153 * @returns string 154 */ 155 static createUrl(action, subaction, id= 0, extraid = {}) { 156 let url = './?'; 157 158 if(action) 159 url += '&action='+action; 160 if(subaction) 161 url += '&subaction='+subaction; 162 if(id) 163 url += '&id='+id; 164 165 if ( extraid instanceof FormData ) { 166 for (let pair of extraid.entries()) { 167 // value is already encoded. 168 // this does not support multiple values. 169 url += '&' + pair[0] + '=' + pair[1]; 170 } 171 } 172 else if ( extraid instanceof Object ) { 173 174 Object.keys(extraid).forEach( (key) => { 175 url += '&' + key + '=' + extraid[key]; 176 }); 177 } 178 else 179 throw "Illegal argument"; 180 181 return url; 182 } 183 184 185 }