partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 9a7ec8af4391a5d05ba9b7195d67ab2d7d11b28a
parent 8b764b7a5d96d7f411e89e9b72cffe5bde442080
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 30 Aug 2015 16:08:40 +0200

Added implementation for Ext.ux.TreePicker to support setValue using a model, not by ID value

Diffstat:
Asrc/PartKeepr/FrontendBundle/Resources/public/js/ExtJS/Enhancements/Ext.ux.TreePicker-setValueWithObject.js | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/FrontendBundle/Resources/views/index.html.twig | 1+
2 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/ExtJS/Enhancements/Ext.ux.TreePicker-setValueWithObject.js b/src/PartKeepr/FrontendBundle/Resources/public/js/ExtJS/Enhancements/Ext.ux.TreePicker-setValueWithObject.js @@ -0,0 +1,73 @@ +/** + * Enhancements for Ext.ux.TreePicker: + * + * - Allow setValue to be a model. If it's a model, select by the idProperty + * - Use | as separator for getPath/selectPath, because the default "/" separator doesn't work with JSON-LD IDs + */ +Ext.define("PartKeepr.ux.TreePicker", { + override: "Ext.ux.TreePicker", + + /** + * Sets the specified value into the field + * @param {Mixed} value + * @return {Ext.ux.TreePicker} this + */ + setValue: function(value) { + var me = this, + record; + + me.value = value; + + if (me.store.loading) { + // Called while the Store is loading. Ensure it is processed by the onLoad method. + return me; + } + + // try to find a record in the store that matches the value + record = value ? me.store.getNodeById(value) : me.store.getRoot(); + if (value === undefined) { + record = me.store.getRoot(); + me.value = record.getId(); + } else { + if (value.isModel) { + record = me.store.getNodeById(value.getId()); + } else { + record = me.store.getNodeById(value); + } + } + + // set the raw value to the record's display field if a record was found + me.setRawValue(record ? record.get(me.displayField) : ''); + + return me; + }, + /** + * Runs when the picker is expanded. Selects the appropriate tree node based on the value of the input element, + * and focuses the picker so that keyboard navigation will work. + * @private + */ + onExpand: function() + { + var me = this, + picker = me.picker, + store = picker.store, + value = me.value, + node; + + if (value) { + if (value.isModel) { + node = store.getNodeById(value.getId()); + } else { + node = store.getNodeById(value); + } + } + + if (!node) { + node = store.getRoot(); + } + + var path = node.getPath("@id", "|"); + + picker.selectPath(path, "@id", "|"); + } +});+ \ No newline at end of file diff --git a/src/PartKeepr/FrontendBundle/Resources/views/index.html.twig b/src/PartKeepr/FrontendBundle/Resources/views/index.html.twig @@ -70,6 +70,7 @@ '@PartKeeprFrontendBundle/Resources/public/js/Data/store/PartCategoryStore.js' '@PartKeeprFrontendBundle/Resources/public/js/ExtJS/Enhancements/Ext.tree.View-missingMethods.js' '@PartKeeprFrontendBundle/Resources/public/js/ExtJS/Enhancements/Ext.form.Basic-AssociationSupport.js' + '@PartKeeprFrontendBundle/Resources/public/js/ExtJS/Enhancements/Ext.ux.TreePicker-setValueWithObject.js' '@PartKeeprFrontendBundle/Resources/public/js/Components/Statusbar.js' '@PartKeeprFrontendBundle/Resources/public/js/Components/Auth/LoginDialog.js' '@PartKeeprFrontendBundle/Resources/public/js/Components/Part/PartImageDisplay.js'