From: erickson Date: Fri, 23 Jan 2009 22:20:49 +0000 (+0000) Subject: rearranging some classes around for better re-use. added an initial auto-grid, need... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=fa51853379088d13fc141eb85e41c294c32c2041;p=Evergreen.git rearranging some classes around for better re-use. added an initial auto-grid, need to add some default data formatting depending on datatype git-svn-id: svn://svn.open-ils.org/ILS/trunk@11940 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/web/js/dojo/openils/widget/AutoFieldWidget.js b/Open-ILS/web/js/dojo/openils/widget/AutoFieldWidget.js new file mode 100644 index 0000000000..1f3d5cfb97 --- /dev/null +++ b/Open-ILS/web/js/dojo/openils/widget/AutoFieldWidget.js @@ -0,0 +1,136 @@ +if(!dojo._hasResource['openils.widget.AutoFieldWidget']) { + dojo.provide('openils.widget.AutoFieldWidget'); + dojo.require('openils.Util'); + dojo.require('openils.User'); + dojo.require('fieldmapper.IDL'); + + dojo.declare('openils.widget.AutoFieldWidget', null, { + + async : false, + + /** + * args: + * idlField -- Field description object from fieldmapper.IDL.fmclasses + * fmObject -- If available, the object being edited. This will be used + * to set the value of the widget. + * fmClass -- Class name (not required if idlField or fmObject is set) + * fmField -- Field name (not required if idlField) + * parentNode -- If defined, the widget will be appended to this DOM node + * dijitArgs -- Optional parameters object, passed directly to the dojo widget + * orgLimitPerms -- If this field defines a set of org units and an orgLimitPerms + * is defined, the code will limit the org units in the set to those + * allowed by the permission + */ + constructor : function(args) { + for(var k in args) + this[k] = args[k]; + + // find the field description in the IDL if not provided + if(!this.idlField) { + if(this.fmObject) + this.fmClass = this.fmObject.classname; + var fields = fieldmapper.IDL.fmclasses[this.fmClass][fields]; + for(var f in fields) + if(fields[f].name == this.fmField) + this.idlField = fields[f]; + } + }, + + /** + * Turn the value from the dojo widget into a value oils understands + */ + getFormattedValue : function() { + var value = this.widget.attr('value'); + switch(this.idlField.datatype) { + case 'bool': + return (value) ? 't' : 'f' + case 'timestamp': + return dojo.date.stamp.toISOString(value); + default: + return value; + } + }, + + build : function(onload) { + this.onload = onload; + this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null; + + switch(this.idlField.datatype) { + + case 'id': + dojo.require('dijit.form.TextBox'); + this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode); + this.widget.setDisabled(true); // never allow editing of IDs + break; + + case 'org_unit': + this._buildOrgSelector(); + break; + + case 'money': + dojo.require('dijit.form.CurrencyTextBox'); + this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode); + break; + + case 'timestamp': + dojo.require('dijit.form.DateTextBox'); + dojo.require('dojo.date.stamp'); + this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode); + if(this.widgetValue != null) + this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue); + break; + + case 'bool': + dojo.require('dijit.form.CheckBox'); + this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode); + this.widgetValue = openils.Util.isTrue(this.widgetValue); + break; + + default: + dojo.require('dijit.form.TextBox'); + this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode); + } + + if(!this.async) this._widgetLoaded(); + return this.widget; + }, + + /** + * For widgets that run asynchronously, provide a callback for finishing up + */ + _widgetLoaded : function(value) { + if(this.fmObject) + this.widget.attr('value', this.widgetValue); + if(this.onload) + this.onload(this.widget, self); + }, + + _buildOrgSelector : function() { + dojo.require('fieldmapper.OrgUtils'); + dojo.require('openils.widget.FilteringTreeSelect'); + this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode); + this.widget.searchAttr = 'shortname'; + this.widget.labelAttr = 'shortname'; + this.widget.parentField = 'parent_ou'; + + // if we have a limit perm, find the relevent orgs (async) + if(this.orgLimitPerms && this.orgLimitPerms.length > 0) { + this.async = true; + var user = new openils.User(); + var self = this; + user.getPermOrgList(this.orgLimitPerms, + function(orgList) { + self.widget.tree = orgList; + self.widget.startup(); + self._widgetLoaded(); + } + ); + + } else { + this.widget.tree = fieldmapper.aou.globalOrgTree; + this.widget.startup(); + } + } + }); +} + diff --git a/Open-ILS/web/js/dojo/openils/widget/AutoGrid.js b/Open-ILS/web/js/dojo/openils/widget/AutoGrid.js new file mode 100644 index 0000000000..8d3a6fe1bb --- /dev/null +++ b/Open-ILS/web/js/dojo/openils/widget/AutoGrid.js @@ -0,0 +1,31 @@ +if(!dojo._hasResource['openils.widget.AutoGrid']) { + dojo.provide('openils.widget.AutoGrid'); + dojo.require('dojox.grid.DataGrid'); + dojo.require('openils.widget.AutoWidget'); + dojo.require('openils.Util'); + + dojo.declare( + 'openils.widget.AutoGrid', + [dojox.grid.DataGrid, openils.widget.AutoWidget], + { + startup : function() { + this.inherited(arguments); + this.initAutoEnv(); + var existing = (this.structure) ? this.structure[0].cells[0] : []; + var fields = []; + for(var f in this.sortedFieldList) { + var field = this.sortedFieldList[f]; + if(!field || field.virtual) continue; + var entry = existing.filter(function(i){return (i.field == field.name)})[0]; + if(entry) entry.name = field.label; + else entry = {field:field.name, name:field.label}; + fields.push(entry); + } + this.setStructure([{cells: [fields]}]); + this.setStore(this.buildAutoStore()); + }, + } + ); + openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory; +} + diff --git a/Open-ILS/web/js/dojo/openils/widget/AutoWidget.js b/Open-ILS/web/js/dojo/openils/widget/AutoWidget.js index ad9df60cc0..6b200069ba 100644 --- a/Open-ILS/web/js/dojo/openils/widget/AutoWidget.js +++ b/Open-ILS/web/js/dojo/openils/widget/AutoWidget.js @@ -1,136 +1,90 @@ if(!dojo._hasResource['openils.widget.AutoWidget']) { dojo.provide('openils.widget.AutoWidget'); - dojo.require('openils.Util'); - dojo.require('openils.User'); + dojo.require('dojo.data.ItemFileWriteStore'); + dojo.require('fieldmapper.dojoData'); dojo.require('fieldmapper.IDL'); + // common superclass to auto-generated UIs dojo.declare('openils.widget.AutoWidget', null, { - async : false, - - /** - * args: - * idlField -- Field description object from fieldmapper.IDL.fmclasses - * fmObject -- If available, the object being edited. This will be used - * to set the value of the widget. - * fmClass -- Class name (not required if idlField or fmObject is set) - * fmField -- Field name (not required if idlField) - * parentNode -- If defined, the widget will be appended to this DOM node - * dijitArgs -- Optional parameters object, passed directly to the dojo widget - * orgLimitPerms -- If this field defines a set of org units and an orgLimitPerms - * is defined, the code will limit the org units in the set to those - * allowed by the permission - */ - constructor : function(args) { - for(var k in args) - this[k] = args[k]; + fieldOrder : null, // ordered list of field names, optional. + sortedFieldList : [], // holds the sorted IDL defs for our fields + fmObject : null, // single fielmapper object + fmObjectList : null, // list of fieldmapper objects + fmClass : '', // our fieldmapper class + + // locates the relevent IDL info + initAutoEnv : function() { + if(this.fmObjectList && this.fmObjectList.length) + this.fmClass = this.fmObjectList[0].classname; + if(this.fmObject) + this.fmClass = this.fmObject.classname; + this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass]; + this.buildSortedFieldList(); + }, - // find the field description in the IDL if not provided - if(!this.idlField) { + buildAutoStore : function() { + var list = []; + if(this.fmObjectList) { + list = this.fmObjectList; + } else { if(this.fmObject) - this.fmClass = this.fmObject.classname; - var fields = fieldmapper.IDL.fmclasses[this.fmClass][fields]; - for(var f in fields) - if(fields[f].name == this.fmField) - this.idlField = fields[f]; + list = [this.fmObject]; } + return new dojo.data.ItemFileWriteStore( + {data:fieldmapper[this.fmClass].toStoreData(list)}); }, - /** - * Turn the value from the dojo widget into a value oils understands - */ - getFormattedValue : function() { - var value = this.widget.attr('value'); - switch(this.idlField.datatype) { - case 'bool': - return (value) ? 't' : 'f' - case 'timestamp': - return dojo.date.stamp.toISOString(value); - default: - return value; - } - }, + buildSortedFieldList : function() { + this.sortedFieldList = []; - build : function(onload) { - this.onload = onload; - this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null; + if(this.fieldOrder) { - switch(this.idlField.datatype) { + for(var idx in this.fieldOrder) { + var name = this.fieldOrder[idx]; + for(var idx2 in this.fmIDL.fields) { + if(this.fmIDL.fields[idx2].name == name) { + this.sortedFieldList.push(this.fmIDL.fields[idx2]); + break; + } + } + } - case 'id': - dojo.require('dijit.form.TextBox'); - this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode); - this.widget.setDisabled(true); // never allow editing of IDs - break; - - case 'org_unit': - this._buildOrgSelector(); - break; - - case 'money': - dojo.require('dijit.form.CurrencyTextBox'); - this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode); - break; - - case 'timestamp': - dojo.require('dijit.form.DateTextBox'); - dojo.require('dojo.date.stamp'); - this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode); - if(this.widgetValue != null) - this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue); - break; - - case 'bool': - dojo.require('dijit.form.CheckBox'); - this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode); - this.widgetValue = openils.Util.isTrue(this.widgetValue); - break; - - default: - dojo.require('dijit.form.TextBox'); - this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode); - } - - if(!this.async) this._widgetLoaded(); - return this.widget; - }, - - /** - * For widgets that run asynchronously, provide a callback for finishing up - */ - _widgetLoaded : function(value) { - if(this.fmObject) - this.widget.attr('value', this.widgetValue); - if(this.onload) - this.onload(this.widget, self); - }, + // if the user-defined order does not list all fields, + // shove the extras on the end. + var anonFields = []; + for(var idx in this.fmIDL.fields) { + var name = this.fmIDL.fields[idx].name; + if(this.fieldOrder.indexOf(name) < 0) { + anonFields.push(this.fmIDL.fields[idx]); + } + } - _buildOrgSelector : function() { - dojo.require('fieldmapper.OrgUtils'); - dojo.require('openils.widget.FilteringTreeSelect'); - this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode); - this.widget.searchAttr = 'shortname'; - this.widget.labelAttr = 'shortname'; - this.widget.parentField = 'parent_ou'; - - // if we have a limit perm, find the relevent orgs (async) - if(this.orgLimitPerms && this.orgLimitPerms.length > 0) { - this.async = true; - var user = new openils.User(); - var self = this; - user.getPermOrgList(this.orgLimitPerms, - function(orgList) { - self.widget.tree = orgList; - self.widget.startup(); - self._widgetLoaded(); + anonFields = anonFields.sort( + function(a, b) { + if(a.label > b.label) return 1; + if(a.label < b.label) return -1; + return 0; } ); + this.sortedFieldList = this.sortedFieldList.concat(anonFields); + } else { - this.widget.tree = fieldmapper.aou.globalOrgTree; - this.widget.startup(); - } - } + // no sort order defined, sort all fields on display label + + for(var f in this.fmIDL.fields) + this.sortedFieldList.push(this.fmIDL.fields[f]); + this.sortedFieldList = this.sortedFieldList.sort( + // by default, sort on label + function(a, b) { + if(a.label > b.label) return 1; + if(a.label < b.label) return -1; + return 0; + } + ); + } + }, }); } diff --git a/Open-ILS/web/js/dojo/openils/widget/EditDialog.js b/Open-ILS/web/js/dojo/openils/widget/EditDialog.js index c48f6f4ddd..39ebc6407c 100644 --- a/Open-ILS/web/js/dojo/openils/widget/EditDialog.js +++ b/Open-ILS/web/js/dojo/openils/widget/EditDialog.js @@ -1,9 +1,19 @@ +/** +var dialog = new openils.widget.EditDialog({ + fmObject: survey, + fieldOrder: ['id', 'name', 'description', 'start_date', 'end_date'] +}); +dialog.startup(); +dialog.show(); +*/ + + + if(!dojo._hasResource['openils.widget.EditDialog']) { dojo.provide('openils.widget.EditDialog'); dojo.require('openils.widget.EditPane'); dojo.require('dijit.Dialog'); - /** * Given a fieldmapper object, this builds a pop-up dialog used for editing the object */ diff --git a/Open-ILS/web/js/dojo/openils/widget/EditPane.js b/Open-ILS/web/js/dojo/openils/widget/EditPane.js index 7e976fd439..b92c951321 100644 --- a/Open-ILS/web/js/dojo/openils/widget/EditPane.js +++ b/Open-ILS/web/js/dojo/openils/widget/EditPane.js @@ -1,23 +1,18 @@ if(!dojo._hasResource['openils.widget.EditPane']) { dojo.provide('openils.widget.EditPane'); dojo.require('openils.widget.AutoWidget'); + dojo.require('openils.widget.AutoFieldWidget'); dojo.require('fieldmapper.Fieldmapper'); dojo.require('dijit.layout.ContentPane'); dojo.require('openils.Util'); - dojo.require('openils.User'); - dojo.require('fieldmapper.IDL'); dojo.require('openils.PermaCrud'); dojo.declare( 'openils.widget.EditPane', - [dijit.layout.ContentPane], + [dijit.layout.ContentPane, openils.widget.AutoWidget], { - fmClass : '', - fmObject : null, mode : 'update', - fieldOrder : null, // ordered list of field names, optional. fieldList : [], // holds the field name + associated widget - sortedFieldList : [], // holds the sorted IDL defs for our fields onPostApply : null, // apply callback onCancel : null, // cancel callback hideActionButtons : false, @@ -33,8 +28,7 @@ if(!dojo._hasResource['openils.widget.EditPane']) { */ startup : function() { this.inherited(arguments); - this.fmClass = (this.fmObject) ? this.fmObject.classname : this.fmClass; - this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass]; + this.initAutoEnv(); var table = document.createElement('table'); var tbody = document.createElement('tbody'); @@ -45,8 +39,6 @@ if(!dojo._hasResource['openils.widget.EditPane']) { if(this.fmIDL.permacrud && this.fmIDL.permacrud[this.mode]) this.limitPerms = this.fmIDL.permacrud[this.mode].perms; - this._buildSortedFieldList() - for(var f in this.sortedFieldList) { var field = this.sortedFieldList[f]; if(!field || field.virtual) continue; @@ -60,7 +52,7 @@ if(!dojo._hasResource['openils.widget.EditPane']) { row.appendChild(valTd); tbody.appendChild(row); - var widget = new openils.widget.AutoWidget({ + var widget = new openils.widget.AutoFieldWidget({ idlField : field, fmObject : this.fmObject, parentNode : valTd, @@ -113,57 +105,6 @@ if(!dojo._hasResource['openils.widget.EditPane']) { } }, - _buildSortedFieldList : function() { - this.sortedFieldList = []; - - if(this.fieldOrder) { - - for(var idx in this.fieldOrder) { - var name = this.fieldOrder[idx]; - for(var idx2 in this.fmIDL.fields) { - if(this.fmIDL.fields[idx2].name == name) { - this.sortedFieldList.push(this.fmIDL.fields[idx2]); - break; - } - } - } - - // if the user-defined order does not list all fields, - // shove the extras on the end. - var anonFields = []; - for(var idx in this.fmIDL.fields) { - var name = this.fmIDL.fields[idx].name; - if(this.fieldOrder.indexOf(name) < 0) { - anonFields.push(this.fmIDL.fields[idx]); - } - } - - anonFields = anonFields.sort( - function(a, b) { - if(a.label > b.label) return 1; - if(a.label < b.label) return -1; - return 0; - } - ); - - this.sortedFieldList = this.sortedFieldList.concat(anonFields); - - } else { - // no sort order defined, sort all fields on display label - - for(var f in this.fmIDL.fields) - this.sortedFieldList.push(this.fmIDL.fields[f]); - this.sortedFieldList = this.sortedFieldList.sort( - // by default, sort on label - function(a, b) { - if(a.label > b.label) return 1; - if(a.label < b.label) return -1; - return 0; - } - ); - } - }, - performEditAction : function(opts) { var pcrud = new openils.PermaCrud(); var fields = this.getFields();