/* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling * Copyright 2011 by the AToMPM team and licensed under the LGPL * See COPYING.lesser and README.md in the root of this project for full details */ GUIUtils = function(){ /** * This is the old getElementById function */ this.$$ = function( id ){ return document.getElementById( id ); }; /** * Converts from page centric X coordinates to canvas centric X coordinates */ this.convertToCanvasX = function (event) { return event.layerX; }; /** * Converts from page centric Y coordinates to canvas centric Y coordinates */ this.convertToCanvasY = function (event) { return event.layerY; }; /** * Disables the dock bar */ this.disableDock = function(){ $('#div_dock').attr('class', 'dock disabled_dock'); }; /** * Enables the dock bar */ this.enableDock = function(){ $('#div_dock').attr('class', 'dock'); }; /** * Constructs and returns a checked checkbox */ this.getCheckbox = function(checked){ var cb = $(''); cb.attr("type", 'checkbox'); cb.prop("checked", checked); return cb; }; // TODO: replace the bundled function with an actual object generation. The // current method is sloppy. /** * Constructs and returns an input field given an attribute's type and value, * bundled in the return value is a function to retrieve the input fields * content as well as its initial value * * For Maps and Lists, onfocus/onblur toggle a default entry to be shown. */ this.getInputField = function (type,value){ /* recursively expand specialTypes, if any */ function explodeType(t) { var exploded = __specialTypes[t] || t; while( exploded.match(/\$/) ) exploded = exploded.replace( /(\$.+?)([,\]>])/g, function(s,p1,p2){return __specialTypes[p1]+p2;}); return exploded; } /* return a default value for the given exploded type string */ function defaultEntry(et) { if( et == 'string' || et == 'code' ) return ""; else if( et == 'int' ) return 0; else if( et == 'boolean' ) return true; else if( et == 'double' ) return 0.0; else if( et.match(/^ENUM/) ) return et; else if( (matches=et.match(/^list<(.*)>$/)) ) return [defaultEntry(matches[1])]; else if( (matches=et.match(/^map<\[(.*?)\],\[(.*)\]>$/)) ) { var m = {}, keys = matches[1].split(','), types = [], depth = 0, index = 0; for( var i=0; i' ) depth--; if( matches[2].charAt(i) == ',' && depth == 0 ) { types.push( matches[2].substring(index,i) ); index = i+1; } } types.push( matches[2].substring(index) ); for( var i=0; i$/)) ) { var m = {}; m[defaultEntry(matches[1])] = defaultEntry(matches[2]); return m; } } if( type == 'code' ) var input = GUIUtils.getTextInput(utils.jsond(value,'\n')), getinput = function(_){return _.val();}; else if( type.match(/^map/) || type.match(/^list/) ) { var input = GUIUtils.getTextInput( utils.jsond(utils.jsons(value,undefined,' '))), getinput = function(_){ return utils.jsonp(utils.jsone(_.val())); }; var de = defaultEntry( explodeType(type) ), isMap = type.match(/^map/), matches = undefined; input.focus( (isMap ? function() { var newVal = utils.mergeDicts([getinput(input),de]); input.val( utils.jsond(utils.jsons(newVal,undefined,' '))); } : function() { var newVal = getinput(input).concat(de); input.val( utils.jsond(utils.jsons(newVal,undefined,' '))); })); input.blur( (isMap ? function() { var val = getinput(input); utils.splitDict(val,utils.keys(de)); input.val( utils.jsond(utils.jsons(val,undefined,' '))); } : function() { var val = getinput(input); if( utils.jsons(utils.tail(val)) == utils.jsons(de[0]) ) val.pop(); input.val( utils.jsond(utils.jsons(val,undefined,' '))); })); } else if( type.match(/^ENUM/) ) { var vals = type.match(/^ENUM\((.*)\)$/)[1], input = GUIUtils.getSelector(vals.split(','),false,[value]), getinput = function(_){return HttpUtils.getSelectorSelection(_)[0];}; } else if( type.match(/^boolean$/) ) { var input = GUIUtils.getCheckbox(value), getinput = function(_){return _.prop("checked");}; } else if( type.match(/^\$/) ) return GUIUtils.getInputField(__specialTypes[type],value); else if ((matches = type.match("^file<(.*)>"))) { var input = GUIUtils.getFileInput(value,matches[1],"code_style string_input",1), getinput = function(_){return _.val();}; } else var input = GUIUtils.getTextInput(value,"code_style string_input",1), getinput = (type == 'string' ? function(_){return _.val();} : function(_){return utils.jsonp(_.val());}); input.title = explodeType(type); return {'input':input, 'getinput':getinput, 'oldVal':getinput(input)}; }; /** * Constructs and returns a element * @param multipleChoice - if true, allows for multiple options to be selected * @param defaultSelection - sets the default selection for the list * @param numVisibleOptions - sets the number of visible options */ this.getSelector = function(choices,multipleChoice,defaultSelection,numVisibleOptions){ var select = $(' element * * @param text - the default text for the input element * @param className - the default class name for the input element * @param width - the default width of the element. If this is omitted * then the defaults to 400px wide. */ this.getStringInput = function(text,className,width){ var input = $(''); input.attr("type", 'text'); input.attr("class", className || 'default_style'); input.val( text ); input.css("width", width || '400px'); return input; }; this.getFileInput = function(code,pattern,className,rows){ var string_input = this.getTextInput(code, className, rows); var extra_el = $('