openrat-cms

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

jquery-plugin-orLinkify.js (3395B)


      1 import $ from "../jquery-global.js";
      2 import Workbench from "../openrat/workbench.js";
      3 import Api       from "../openrat/api.js";
      4 
      5 /**
      6  * JQuery-Plugin, enable clicking on an area.
      7  * It searches for an anchor (<a href="..." />) in the child elements and virtually clicks on it.
      8  */
      9 export default function( options )
     10 {
     11 	// Create some defaults, extending them with any options that were provided
     12 	var settings = $.extend( {
     13 		'openAction' : function(name,action,id) {
     14 			Workbench.getInstance().openNewAction( name,action,id );
     15 		}
     16 	}, options);
     17 
     18 	$(this).addClass('linkified');
     19 
     20     // Disable all links in this linkified area.
     21     // The user is already able to open the link in a new tab.
     22 	if  ( $(this).is('a') )
     23 		$(this).addClass('act-prevented-link').click( function(event) {
     24 			event.preventDefault();
     25 		} );
     26 	else
     27 		$(this).find('a').addClass('act-prevented-sublink').click( function(event) {
     28 			event.preventDefault();
     29 		} );
     30 
     31     return $(this).click(function(event)
     32 	{
     33 
     34 		// Searching for the first link in all children.
     35 		let $el = $(this);
     36 		let $link;
     37 
     38 		if   ( $el.is('a') )
     39 			$link = $el;
     40 		else
     41 			$link = $el.find('a').first();
     42 
     43 		let type = $link.attr('data-type');
     44 
     45 		// Inaktive Men├╝punkte sind nat├╝rlich nicht anklickbar.
     46 		if	( $link.parent().hasClass('dropdown-entry--inactive') )
     47 			return;
     48 
     49 		switch( type )
     50 		{
     51 			/**
     52 			 * Creating a temporary form element for submitting a POST request.
     53 			 */
     54 			case 'post':
     55 
     56 				let api = new Api();
     57 				let formData = new FormData();
     58 
     59 				let params = JSON.parse( $link.attr('data-data')  );
     60 				params.output = 'json';
     61 
     62 				// Add formular data...
     63 				Object.keys( params ).forEach( (key) => {
     64 					formData.append( key, params[key] );
     65 				} );
     66 				if   (!formData.get('id') )
     67 					formData.set('id',Workbench.state.id);
     68 				if   (!formData.get('action') )
     69 					formData.set('action',Workbench.state.action);
     70 
     71 				// Submit the form.
     72 				api.sendData( formData );
     73 
     74 				if   ( $link.data('afterSuccess') === 'reload' )
     75 					Workbench.getInstance().reloadViews();
     76 				if   ( $link.data('afterSuccess') === 'reloadAll' )
     77 					Workbench.getInstance().reloadAll();
     78 
     79 				break;
     80 
     81 			case 'edit':
     82 			case 'dialog':
     83 				let dialog = Workbench.getInstance().createDialog();
     84 				let name   = $link.attr('data-name');
     85 				if   ( !name )
     86 					name = $link.text(); // get the name from the combined text of all children.
     87 
     88 				let extraValue = Workbench.htmlDecode($link.attr('data-extra'));
     89 				let extraData  = JSON.parse(extraValue);
     90 
     91 				dialog.start(name,$link.attr('data-action'),$link.attr('data-method'),$link.attr('data-id'),extraData );
     92 				break;
     93 
     94 			case 'external':
     95 				window.open( $link.attr('data-url'),' _blank' );
     96 				break;
     97 			case 'window':
     98 				window.location.href = View.createUrl($link.attr('data-action'), $link.attr('data-method'), $link.attr('data-id'));
     99 				break;
    100 
    101 			case 'popup':
    102 				Workbench.popupWindow = window.open( $link.attr('data-url'), 'Popup', 'location=no,menubar=no,scrollbars=yes,toolbar=no,resizable=yes');
    103 				break;
    104 
    105 			case 'help':
    106 				help($link,$link.attr('data-url'),$link.attr('data-suffix') );
    107 				break;
    108 
    109 			case 'fullscreen':
    110 				fullscreen($link);
    111 				break;
    112 
    113 			case 'open':
    114 				settings.openAction( $link.text().trim(),$link.attr('data-action'),$link.attr('data-id') );
    115 				break;
    116 
    117 			default:
    118 				throw "UI error: Unknown link type: "+type+" in link "+$link.html();
    119 		}
    120 	});
    121 };
    122 
    123 
    124