openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

dialog.js (3421B)


      1 import $ from "../jquery-global.js";
      2 import View from './view.js';
      3 import Notice from "./notice.js";
      4 import Workbench from "./workbench.js";
      5 import WorkbenchNavigator from "./navigator.js";
      6 
      7 /**
      8  * The encapsulated view.
      9  */
     10 export default class Dialog {
     11 
     12 	/**
     13 	 * A dialog is a special area in the workbench for displaying and inputting data.
     14 	 * A dialog contains a view.
     15 	 */
     16 	constructor() {
     17 
     18 		this.view;
     19 
     20 		/**
     21 		 * the DOM element which contains the dialog.
     22 		 * @type {*|jQuery|HTMLElement}
     23 		 */
     24 		this.element = $('.or-dialog-content .or-view');
     25 	}
     26 
     27 	/**
     28 	 * Set Dirty-marker.
     29 	 * @param dirty true, if unsaved changes exist
     30 	 */
     31 	set isDirty( dirty ) {
     32 		if   ( dirty )
     33 			this.element.addClass('view--is-dirty');
     34 		else
     35 			this.element.removeClass('view--is-dirty');
     36 	}
     37 
     38 	/**
     39 	 * Get Dirty-marker.
     40 	 * @return true, if unsaved changes exist
     41 	 */
     42 	get isDirty() {
     43 		return this.element.hasClass('view--is-dirty');
     44 	}
     45 
     46 	/**
     47 	 * Creating a new dialog.
     48 	 *
     49 	 * @param name
     50 	 * @param action Action
     51 	 * @param method
     52 	 * @param id Id
     53 	 * @param params
     54 	 *
     55 	 * @return Promise of underlying view
     56 	 */
     57 	start( name,action,method,id,params )
     58 	{
     59 		// Attribute aus dem aktuellen Editor holen, falls die Daten beim Aufrufer nicht angegeben sind.
     60 		if (!action)
     61 			action =  Workbench.state.action;
     62 
     63 		if  (!id)
     64 			id =  Workbench.state.id;
     65 
     66 		let dialog = this;
     67 
     68 		let view = new View( action,method,id,params );
     69 
     70 		Notice.removeAllNotices();
     71 
     72 		$('.or-dialog-content .or-view').html(''); // Clear old content
     73 
     74 		$('.or-dialog-content .or-act-dialog-name').html( name );
     75 		this.show();
     76 
     77 		view.onCloseHandler.add( function() {
     78 			dialog.back();
     79 		} );
     80 
     81 		view.onChangeHandler.add( function() {
     82 			// data has changed
     83 			console.debug("Changes detected");
     84 			dialog.isDirty = true;
     85 		});
     86 
     87 		view.onSaveHandler.add( function() {
     88 			// data was saved
     89 			dialog.isDirty = false;
     90 		});
     91 
     92 		this.view = view;
     93 
     94 		Workbench.getInstance().startSpinner();
     95 
     96 		let viewPromise = this.view.start( this.element );
     97 
     98 		viewPromise.then(
     99 			() => Workbench.getInstance().stopSpinner()
    100 		);
    101 
    102 		return viewPromise;
    103 	}
    104 
    105 
    106 
    107 	show() {
    108 
    109 		//WorkbenchNavigator.navigateToNew( {'action':Workbench.state.action+'','id':Workbench.state.id } );
    110 		WorkbenchNavigator.navigateToNew( Workbench.state );
    111 
    112 		$('.or-dialog').removeClass('dialog--is-closed').addClass('dialog--is-open');
    113 
    114 		if   ( this.isDirty ) {
    115 			this.element.addClass('view--is-dirty');
    116 		}
    117 	}
    118 
    119 	back() {
    120 		console.debug("Back from dialog");
    121 		history.back();
    122 	}
    123 
    124 	hide() {
    125 		$('.or-dialog').removeClass('dialog--is-open').addClass('dialog--is-closed'); // Dialog schließen
    126 	}
    127 
    128 
    129 	/**
    130 	 * Closing the dialog.
    131 	 */
    132 	close() {
    133 
    134 		let dialog = this;
    135 
    136 		if   ( this.isDirty ) {
    137 			// ask the user if we should close this dialog
    138 			//let confirmed = window.confirm( Workbench.language.UNSAVED_CHANGES_CONFIRM );
    139 
    140 			//if   ( ! confirmed )
    141 			//	return; // do not close the dialog
    142 
    143 			let notice = new Notice();
    144 			notice.msg = Workbench.language.REOPEN_CLOSED_DIALOG;
    145 			notice.setStatus( 'warning' );
    146 			notice.timeout = 120;
    147 			notice.onClick.add( function() {
    148 				Workbench.dialog = dialog;
    149 				dialog.show();
    150 				notice.close();
    151 			});
    152 			notice.show();
    153 		}
    154 
    155 		// Remove dirty-flag from view
    156 		$('.or-dialog-content .or-view.or-view--is-dirty').removeClass('view--is-dirty');
    157 		this.hide();
    158 		//$(document).unbind('keyup',this.escapeKeyClosingHandler); // Cleanup ESC-Key-Listener
    159 	}
    160 }