Skip to content
Snippets Groups Projects
Select Git revision
  • d64b9f998d843f2e36e3d58e53bf11a9c977966a
  • master default protected
  • postgres_integration
  • s3compatible
  • intros
  • bootstrap4
  • modules
7 results

moderator.js

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    moderator.js 11.90 KiB
    var moderator = {
    	api: {
    		csrf_token: '',
    		init: function () {
    			$("#modal_apierror").on('hidden.bs.modal', function () {
    				$(".modal-body", $("#modal_apierror"))[0].innerHTML = '';
    				$(".modal-body", $("#modal_apierror"))[0].innerHTML = '';
    			});
    		},
    		set: function(path,value,reload) {
    			var req = {};
    			req[path] = value;
    			moderator.api.set_multi(req,reload)
    		},
    		set_map: function(table, key1, key2, value) {
    			var dict = {};
    			var url;
    			if (value) {
    				url = "/internal/set/"+table+"/"+key1+"/"+key2;
    			} else {
    				url = "/internal/unset/"+table+"/"+key1+"/"+key2;
    			}
    			dict['_csrf_token'] = moderator.api.csrf_token;
    			$.ajax({
    				method: "POST",
    				url: url,
    				dataType: "text",
    				contentType: "application/json",
    				data: JSON.stringify(dict),
    				error: moderator.api.handleapierror
    			});
    		},
    		set_multi: function(dict,reload) {
    			dict['_csrf_token'] = moderator.api.csrf_token;
    			$.ajax({
    				method: "POST",
    				url: "/internal/edit",
    				dataType: "text",
    				contentType: "application/json",
    				data: JSON.stringify(dict),
    				error: moderator.api.handleapierror,
    				success: function () {
    					if (reload) {
    						window.location.reload();
    					}
    				}
    			});
    		},
    		add_new: function(value,type,reload) {
    			value['_csrf_token'] = moderator.api.csrf_token;
    			$.ajax({
    				method: "POST",
    				url: "/internal/new/"+type,
    				dataType: "text",
    				contentType: "application/json",
    				data: JSON.stringify(value),
    				error: moderator.api.handleapierror,
    				success: function () {
    					if (reload) {
    						window.location.reload();
    					}
    				}
    			})
    		},
    		handleapierror: function(jqXHR, textStatus, errorThrow) {
    			console.log("Fehler: "+errorThrow)	
    			$(".modal-body", $("#modal_apierror"))[0].innerHTML += "<p>" + errorThrow + ": " + textStatus + "</p>";
    			$("#modal_apierror").modal()
    		},
    		gethttp: function (url){
    			$.ajax({
    				method: "GET",
    				url: url,
    				dataType: "text",
    			})
    		},
    		setcsrftoken: function (token) {
    			moderator.api.csrf_token = token;
    		}
    
    	},
    	editor: {
    		fromhtml: function (str) {
    			return str.replace(/<br>/g, '\n')
    		},
    		tohtml: function (str) {
    			return str.replace(/\n/g, '<br>')
    		},
    		init: function() {
    			var editable = $('.moderator_editor');
    			for (var i=0; i<editable.length; i++) {
    				var e = $(editable[i]);
    				$('.moderator_editor_sign',e).popover(
    					{
    						title: e.data('path'),
    						html: true,
    						trigger: 'click',
    						container: 'body',
    						content: function() {
    							return '<div class="row form-group">'+
    									'<span class="col-xs-12">'+
    										'<textarea class="form-control editor_value ctrl_enter_submit" rows="3" data-path="'+$(this.parentElement).data('path')+'">'+moderator.editor.fromhtml(this.parentElement.getElementsByClassName('moderator_editor_value')[0].innerHTML)+'</textarea>'+
    									'</span>'+
    									'<span class="col-xs-12">'+
    										'<input class="btn btn-default pull-right" type="submit" data-path="'+$(this.parentElement).data('path')+'" data-reload="'+$(this.parentElement).data('reload')+'" value="save" onClick="moderator.editor.editbtnclick(this)">'+
    									'</span>'+
    								'</div>';
    						}
    					}		
    				);
    				$('.moderator_editor_sign',e).on('shown.bs.popover', function(){
    					$('.ctrl_enter_submit').focus();
    					$('.ctrl_enter_submit').keydown(function (e) {
    
    						if (e.ctrlKey && e.keyCode == 13) {
    							$(this.parentNode.nextElementSibling.childNodes[0]).trigger("click");
    						}
    					});
    				});
    			}
    		},
    		editbtnclick: function (src) {
    			var path  = $($(src)[0]).data('path');
    			var reload  = $($(src)[0]).data('reload');
    			var value = $('.editor_value',src.parentElement.parentElement).val();
    
    
    			var editable = $('.moderator_editor');
    			for (var i=0; i<editable.length; i++) {
    				var e = $(editable[i]);
    
    				if (e.data('path') == path) {
    					$('.moderator_editor_sign',e).click();
    					$(".moderator_editor_value",e).html(moderator.editor.tohtml(value));
    				}
    			}
    			moderator.api.set(path,value,reload);
    		},
    		changeboxclick: function(src) {
    			var value = $(src)[0].checked;
    			var path = $(src).data('path');
    			moderator.api.set(path,value ? true : false);
    		},
    		deletebtnclick: function(src) {
    			if (confirm('Really delete this?')) { 
    				moderator.api.set($(src).data('path'),true,true);
    			}
    		}
    	},
    	permissioneditor: {
    		permissions: [],
    		setpermissions: function(value) {
    			var idlist = [];
    			for (i in value) {
    				if (idlist.indexOf(value[i]['id']) == -1) {
    					idlist[idlist.length] = value[i]['id'];
    					moderator.permissioneditor.permissions[moderator.permissioneditor.permissions.length] = value[i];
    				}
    			}
    		},
    		init: function () {
    			$('#editperm').on('show.bs.modal', function (e) {
    				var srcel = e.relatedTarget;
    				if (!srcel) {
    					return 0;
    				}
    				var id = $(srcel).data('id');
    				var type = $(srcel).data('type');
    				$('#editpermdiv').data('id',id);
    				$('#editpermdiv').data('type',type);
    				var permElems = []
    				for (i in moderator.permissioneditor.permissions) {
    					if ((moderator.permissioneditor.permissions[i][type+'_id'] == id)) {
    						var perm = {};
    						perm.type = moderator.permissioneditor.permissions[i]['type'];
    						perm.param1 = moderator.permissioneditor.permissions[i]['param1'];
    						perm.param2 = moderator.permissioneditor.permissions[i]['param2'];
    						perm.id = moderator.permissioneditor.permissions[i]['id'];
    
    						permstring = '';
    						switch (perm.type) {
    							case 'password':
    								permstring = '("'+perm.param1+'":"'+perm.param2+'")'
    								break;
    							case 'public':
    								permstring = '';
    								break;
    							case 'none':
    								permstring = '(Kein Zugriff)';
    								break;
    							case 'rwth':
    								permstring = '(RWTH-intern)'
    								break;
    							case 'fsmpi':
    								permstring = '(FS-intern)'
    								break;
    							case 'l2p':
    								permstring = '(' + perm.param1 + ')'
    								break;
    							case 'moodle':
    								permstring = '(' + perm.param1 + ')'
    								break;
    						}
    						var optionEl = document.createElement('option');
    						optionEl.dataset.id = perm.id;
    						optionEl.dataset.type = perm.type;
    						optionEl.dataset.param1 = perm.param1;
    						optionEl.dataset.param2 = perm.param2;
    
    						optionEl.text = '#' + perm.id + ' ' + perm.type + ' ' + permstring;
    						permElems.push(optionEl)
    					}
    				}
    				document.querySelector('#permissionlist').replaceChildren(...permElems);
    			});
    
    		},
    		selectpermission: function (element) {
    			var perm = $('#permissionlist option:selected').data();
    			if (!perm) {
    				return;
    			}
    			var container = $('#editpermdiv');
    			moderator.permissioneditor.typechangehandler(perm.type, container);
    			switch(perm.type) {
    				case 'password':
    					$(".authuser", container).val(perm.param1);
    					$(".authpassword", container).val(perm.param2);
    					break
    				case 'l2p':
    					$(".authl2p", container).val(perm.param1);
    					break
    				case 'moodle':
    					$(".authmoodle", container).val(perm.param1);
    					break
    			}
    			$(".authtype option[value="+perm.type+"]").prop("selected", true);
    		},
    		delbtnclick: function (element) {
    			moderator.api.set("perm."+$("#permissionlist option:selected", element.parentElement).data('id')+".deleted",true,true);
    		},
    		addbtnclick: function (element) {
    			var container = element.parentElement;
    			var perm = moderator.permissioneditor.getpermform(container);
    			dict = {}
    			dict['type'] = perm.type;
    			dict['param1'] = perm.param1;
    			dict['param2'] = perm.param2;
    			dict[$(container).data('type')+'_id'] = $(container).data('id');
    			moderator.api.add_new(dict,'perm',true);
    			var option = $('<option>', {
    				"text": perm.type+' '+( perm.type == "password" ? ' ("'+perm.param1+'":"'+perm.param2+'")' : '' ) ,
    				"data-auth": JSON.stringify(perm)
    			});
    			$("#permissionlist",element.parentElement).append(option);
    		},
    		updatebtnclick: function (element) {
    			var container = element.parentElement
    			var perm = moderator.permissioneditor.getpermform(container);
    			var id = $("#permissionlist option:selected", element.parentElement).data('id');
    			if (!id) {
    				return;
    			}
    			var dict = {};
    			for (i in perm) {
    				dict['perm.'+id+'.'+i] = perm[i];
    			}
    			console.log(dict);
    			moderator.api.set_multi(dict,true)
    		},
    		getpermform: function (container) {
    			var perm = {};
    			perm.type = $(".authtype", container).val();
    			switch(perm.type) {
    				case 'password':
    					perm.param1 = $(".authuser", container).val();
    					perm.param2 = $(".authpassword", container).val();
    					break
    				case 'l2p':
    					perm.param1 = $(".authl2p", container).val();
    					break
    				case 'moodle':
    					perm.param1 = $(".authmoodle", container).val();
    					break
    			}
    			return perm;
    		},
    		typechange: function (element) {
    			$("#permissionlist option:selected").prop("selected", false);
    			moderator.permissioneditor.typechangehandler($(element).val(),element.parentElement);
    		},
    		typechangehandler: function (type, container) {
    			$(".authuser", container).val('');
    			$(".authpassword", container).val('');
    			$(".authl2p", container).val('');
    			$(".authmoodle", container).val('');
    			switch (type) {
    				case 'password':
    					$(".passwordinput",container).show();
    					$(".authl2p",container).hide();
    					$(".authmoodle",container).hide();
    					break;
    				case 'l2p':
    					$(".passwordinput",container).hide();
    					$(".authl2p",container).show();
    					$(".authmoodle",container).hide();
    					break;
    				case 'moodle':
    					$(".passwordinput",container).hide();
    					$(".authl2p",container).hide();
    					$(".authmoodle",container).show();
    					break;
    				default:
    					$(".passwordinput",container).hide();
    					$(".authl2p",container).hide();
    					$(".authmoodle",container).hide();
    					break;
    			}
    		},
    		randompw: function () {;
    			var array = new Uint8Array(10);
    			window.crypto.getRandomValues(array);
    			var result = '';
    			var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    			for (var i = 0; i< array.length; i++) {
    				result += chars.charAt(array[i] % chars.length);
    			}
    			return result;
    		}
    	},
    	plots: {
    		init: function() {
    			$(window).on("resize", moderator.plots.resize);
    			$(".plotlyresize").on("click", moderator.plots.resize);
    			moderator.plots.createplots(".plot-view")
    		},
    		resize: function() {
    			$(".plot-view").each(function () {Plotly.Plots.resize(this)});
    		},
    		createsingleplot: function(htmlobj, id) {
    			if (!htmlobj.id && id)
    				htmlobj.id = "plot-"+id;
    			$(htmlobj).html('<div class="plot-loader"></div>');
    			$.ajax({
    				divobj: htmlobj,
    				autorefresh: htmlobj.dataset.reload,
    				method: "GET",
    				url: htmlobj.dataset.url,
    				dataType: "json",
    				error: function (jqXHR, textStatus, errorThrow) {
    					$(this.divobj).html('<div class="plot-error">'+errorThrow+'</div>');
    				},
    				success: function (traces) {
    					var layout = {margin: {l: 30, r: 30, t: 10, b: 70, pad: 0}};
    					for (var i = 0; i < traces.length; i ++) {
    						traces[i].type = this.divobj.dataset.type;
    					}
    					if (this.divobj.dataset.type == "pie")
    						layout.showlegend = false;
    					traces.sort(function (a, b) {
    						asum = 0;
    						bsum = 0;
    						for (var i = 0; i < a.y.length; i++)
    							asum += a.y[i]
    						for (var i = 0; i < b.y.length; i++)
    							bsum += b.y[i]
    						return bsum-asum;
    					});
    					for (var i = 0; i < traces.length; i++) {
    						if (i > 15) {
    							traces[i].visible = "legendonly";
    						}
    					}
    					$(this.divobj).html("");
    					Plotly.newPlot(this.divobj.id, traces, layout, { "modeBarButtonsToRemove": ['sendDataToCloud','hoverCompareCartesian'], "displaylogo": false});
    				},
    				complete: function() {
    					if (this.autorefresh) {
    						setTimeout(
    							function(obj){
    								moderator.plots.createsingleplot(obj);
    							}, this.autorefresh, this.divobj);
    					}
    				}
    			});
    		},
    		createplots: function (selector) {
    			var l = $(selector);
    			for (var i = 0; i < l.length; i ++) {
    				moderator.plots.createsingleplot(l[i], i);
    			}
    		},
    	},
    	init: function () {
    		moderator.api.init();
    		moderator.editor.init();
    		moderator.permissioneditor.init();
    		moderator.plots.init();
    	}
    };
    
    $( document ).ready( function () {
    	moderator.init(); 
    } );