===== smartformHelper ===== \\ Ansammlung nützlicher Funktionen welche oftmals im Einsatz sind. Die Funktionen sind größtenteils so gehalten, dass sie bei anderen Kunden ohne viel Aufwand verwendet werden können. \\ >>Achtung: jQuery wird dringend benötigt!! === Sourcen === TODO === Beispiel HTML === === Beispiel Custom === var head = document.getElementsByTagName("head")[0]; var customUrl = "/loom-portal/custom/BELIEBIGERORDNER/"; scriptTag = document.createElement('script'); scriptTag.setAttribute("type", "text/javascript"); scriptTag.setAttribute("src", customUrl + "smartformHelper.js"); head.appendChild(scriptTag); (function($, scope){ var helper = gadget.functions.initFormularFunctions.bind(scope)(); /* Beispiele Funktionsaufrufe: */ //setValue helper.setValue("ID","WERT"); //getValue var value = helper.getValue("ID"); //ajaxCall helper.ajaxCall(Type, URL, dataType, contentType, callback); //stringContains var bool = helper.stringContains("SUCHWERT", "SUCHMENGE"); var bool2 = helper.stringContains("SUCHWERT", ["ARRAY","AUS","STRINGS","DAS","DURCHSUCHT","WERDEN","SOLL"]); //selectUsersByGroup helper.selectUsersByGroup( $("select") ); //forceRedraw helper.forceRedraw( $("select") ); //getUserName var firstname_lastname = helper.getUserName( gadget.getEntity("currentUser") ); //getUserNameReverse var lastname_firstname = helper.getUserNameReverse( gadget.getEntity("currentUser") ); //getUserDepartment var department = helper.getUserDepartment( gadget.getEntity("currentUser") ); //getDateString var ddMMyyyy = helper.getDateString( new Date() ); //getTime var hhmm = helper.getTime( new Date() ); //populateSelectFromArray helper.populateSelectFromArray("ID", ["Wert1","Wert2"], ["Wert1"]); //populateSelectFromCSV helper.populateSelectFromCSV(); //checkValidInput var bool = helper.checkValidInput($("input")); //alert helper.alert("TITLE","MESSAGE",function(){ //tu was }) //confirm helper.confirm("TITLE", "TEXT", "BUTTON OK TEXT", "BUTTON ABBRECHEN TEXT", function(decision){ if(decision) // tu was }) })( this.form.ownerDocument.defaultView != null) ? this.form.ownerDocument.defaultView.jQuery : this.form.ownerDocument.parentWindow.jQuery , this); Dabei zu beachten sind Folgende Punkte: * Aus Performancegründen wird für die selectUsersByGroup auf Queries zurückgegriffen. Diese sind im JavaScript mit definiert und können im Supermandanten angelegt werden. * Für populateSelectFromCSV wird PAPAparse.min.js benötigt. === smartformHelper Code === smartformHelper.js /* Method Index setValue: Sets value, merges local and inserts value in smartformfield if there is one (Params: id, value) ---------------------------------------------------------- getValue: Returns blank if undefined (Params: id) ---------------------------------------------------------- ajaxCall: Führt Ajaxcall aus und gibt Response an eine Callback weiter (Params: Type, URL, dataType, contentType, callback) ---------------------------------------------------------- stringContains: Prüft ob Zeichenkette vars in Wert var vorhanden ist. vars kann ein String oder ein Array aus mehreren Strings sein (Params: variable1, variable(s)2) ---------------------------------------------------------- selectUsersByGroup: Fill dropdowns with all users or users from group. Exepts Classname of the dropdowns HTML: Params: classname ---------------------------------------------------------- getUserName: Returns Firstname, Lastname from user got. If undefined returns username. (Param: User) ---------------------------------------------------------- getUserDepartment: Returns department of userGot (Param: user) ---------------------------------------------------------- getDateString: returns date as string in format dd.MM.yyyy (Params: Date) ---------------------------------------------------------- getTime: returns time as string in format hh:mm (Params: Date) ---------------------------------------------------------- populateSelectFromArray: fills an Selectfield with the given arrays Params: id of the select array: array filled plain text or objects. If objects, all attributes are taken attributeList: if only certain attributes of objects should be taken ---------------------------------------------------------- populateSelectFromCSV: fills an Selectfield with the CSV File from URL ########papaparse.min.js mandatory########## Params: - ---------------------------------------------------------- checkValidInput: checks if regex from field param matches. alerts message and clears field if not. Sets value on change example: checkValidInput( $(".fields") ) Params: filds */ gadget.functions.initFormularFunctions = function(){ var $ = (this.form.ownerDocument.defaultView!=null) ? this.form.ownerDocument.defaultView.jQuery : this.form.ownerDocument.parentWindow.jQuery; var scope = this; var helper = { /* Sets value, merges local and inserts value in smartformfield if there is one Params: id, value */ setValue: function(idGot, value){ if(typeof value == "undefined" || value == null) return false; value = value.toString(); var identifier = idGot.replace(/[/g,"[").replace(/\]/g,"\\]").replace(/\?/g,"\\?"); var field = $("#"+identifier); if($(field).find("option:selected").attr("default")){ scope.entity.setValue(idGot,""); }else{ scope.entity.setValue(idGot,value); } scope.entity.mergeLocal(true); if(field.length>0){ switch($(field).prop("tagName")){ case "INPUT": if($(field).attr("type")=="radio") $("input[name="+identifier+"][value="+value+"]").prop("checked",true); if($(field).attr("type")=="checkbox"){ if(value == "true") $(field).prop("checked",true); else $(field).prop("checked",false); } else $(field).val(value); break; case "SPAN": $(field).prop("innerHTML",value); break; case "TEXTAREA": $(field).val(value); break; case "SELECT": $(field).val(value); default: break; } } }, /* Returns blank if undefined Params: id */ getValue: function(idGot){ return ( typeof scope.entity.getValue(idGot) == "undefined") ? "" : scope.entity.getValue(idGot); }, /* Führt Ajaxcall aus und gibt Response an eine Callback weiter Params: Type, URL, dataType, contentType, callback */ ajaxCall: function(type, url, dataType, contentType, callback){ $.ajax({ type: type, url: url, dataType: dataType, contentType: contentType }).done(function(response){ if(typeof callback == "function") { callback.bind(this)(response) } else{ return response; } }.bind(this)).fail(function(){ if(typeof callback == "function") { callback.bind(this)(null) } else{ return null; } }.bind(this)); }, /* Prüft ob Zeichenkette vars in Wert var vorhanden ist. vars kann ein String oder ein Array aus mehreren Strings sein Params: variable1, variable(s)2 */ stringContains: function(va, vars){ var c=false; switch(typeof vars){ case "object": $(vars).each(function(i, v){ if(va.indexOf(v)>-1){ c=true; return false; } }); break; case "string": if(va.indexOf(vars)>-1){ c=true; } break; default: c = false; } return c; }, /* Fill dropdowns with all users or users from group. Exepts Classname of the dropdowns HTML: Params: classname FOR PERFORMANCE REASONS ADD QUERIES TO SUPER/ADMIN Name: getUsersByGroup Query: SELECT i.ID_, i.NAME_, i.NAMEFIRST, i.NAMELAST, i.EMAIL, c.DEPARTMENT, c.COMPANYID, c.COMPANYNAME from loom_identity i, loom_identity m, loom_identity g, loom_systemconfiguration c where i.CLASS = 'USER' and i.ARCHIV_ '0' and i.USERPROFILE_ID_ = c.ID_ and i.CLIENT_ID_ = ${SYS.CURRENT_CLIENT} and g.CLASS = 'GROUP' and m.CLASS = 'MEMBERSHIP' and m.PARENT_ID_ = g.ID_ and m.USER_ID_ = i.ID_ and g.NAME_ = ? Name: getAllUsers Query: SELECT i.ID_, i.NAME_, CASE WHEN i.NAMEFIRST IS NULL THEN '' ELSE i.NAMEFIRST END NAMEFIRST, CASE WHEN i.NAMELAST IS NULL THEN '' ELSE i.NAMELAST END NAMELAST, i.EMAIL, c.DEPARTMENT, c.COMPANYID, c.COMPANYNAME FROM loom_identity i, loom_systemconfiguration c where i.CLASS = 'USER' and i.CLIENT_ID_ = ${SYS.CURRENT_CLIENT} and i.USERPROFILE_ID_ = c.ID_ and i.ARCHIV_ = '0' ORDER BY NAMELAST,NAMEFIRST desc */ selectUsersByGroup: function(classGot){ // Init all selects $('.'+classGot).each(function(i, select){ $(select).attr("loaded","false"); $(select).empty(); var val = helper.getValue( $(select).attr("id") + "RealName" ); if(val) { $(this).append($("
").dialog({ dragable:false, modal: true, resizeable: false, show: {effect: "fadeIn"}, title: titleGot, maxwidth: 600, open: function(){ $(this).html(textGot); $(".ui-dialog-titlebar-close").css("display","none"); }, buttons: { Ok: function(){ $(this).dialog("close"); $(this).dialog("destroy"); if(typeof callback == "function") eval(callback()); } }, create:function () { $(this).closest(".ui-dialog").find(".ui-dialog-buttonset button").addClass("btn btn-primary"); } }); }, confirm: function(titleGot, textGot, btnOKText, btnCancelText, callback){ //bootbox.alert(textGot); $("
").dialog({ dragable:false, modal: true, resizeable: false, show: {effect: "fadeIn"}, title: titleGot, open: function(){ $(this).html(textGot); $(".ui-dialog-titlebar-close").css("display","none"); }, buttons: { Ok: function(){ $(this).dialog("close"); $(this).dialog("destroy"); if(typeof callback == "function") eval(callback(true)); }, Abbrechen: function(){ $(this).dialog("close"); $(this).dialog("destroy"); if(typeof callback == "function") eval(callback(false)); } }, create:function () { $(this).closest(".ui-dialog").find(".ui-dialog-buttonset button").first().addClass("btn btn-primary"); $(this).closest(".ui-dialog").find(".ui-dialog-buttonset button").first().next().addClass("btn btn-danger"); if(btnOKText) $(this).closest(".ui-dialog").find(".ui-dialog-buttonset button").first().html(btnOKText); if(btnCancelText) $(this).closest(".ui-dialog").find(".ui-dialog-buttonset button").first().next().html(btnCancelText); } }); } } return helper; }