📄 selection.js
字号:
/*
* Isomorphic SmartClient
* Version 6.5 (2008-04-30)
* Copyright(c) 1998-2007 Isomorphic Software, Inc. All rights reserved.
* "SmartClient" is a trademark of Isomorphic Software, Inc.
*
* licensing@smartclient.com
*
* http://smartclient.com/license
*/
//> @class Selection//// Maintains a 'selected' subset of a List or Array of objects, such as records in a record// set, or widgets in a selectable header.<br><br>//// Includes methods for selecting objects and checking which objects are selected, and also for// selecting objects as a result of mouse events, including drag selection support.<br>// The selection object is used automatically to handle selection APIs on +link{class:ListGrid}// and +link{class:TreeGrid} instances.<br><br>////// @visibility external// @treeLocation Client Reference/System//<//// create the Selection class//isc.ClassFactory.defineClass("Selection");// add default properties to the classisc.Selection.addProperties({ //> @attr selection.selectionProperty (String : null : [IRA]) // Property to use to mark records as selected. // <P> // Defaults to an auto-generated property name that starts with an underscore. // @visibility serverSelection //< //selectionProperty:null, //>@attr selection.data (Array | List : null : [IRWA]) // The set of data for which selection is being managed. If not specified at init time // this can be set up via the <code>selection.setData()</code> method. // @visibility serverSelection //< // _dirty - manages whether we need to update the cache of selected records. _dirty:true });isc.Selection.addClassProperties({ //> @type SelectionStyle // Different styles of selection that a list, etc. might support // @visibility external // @group selection // // @value isc.Selection.NONE don't select at all NONE : "none", // @value isc.Selection.SINGLE select only one item at a time SINGLE: "single", // @value isc.Selection.MULTIPLE select one or more items MULTIPLE: "multiple", // @value isc.Selection.SIMPLE select one or more items as a toggle // so you don't need to hold down control keys to select // more than one object SIMPLE: "simple", //< // for generating unique IDs for each Selection _selectionID : 0 });isc.Selection.addMethods({//> @method selection.init() (A)// Initialize this selection instance.<br>// Note: if the <code>data</code> property is not set at init time, it should be passed to// the selection using the <code>selection.setData</code> method// @group selection// @param [all arguments] (object) objects with properties to override from default// @visibility serverSelection//<init : function () { // get unique ID and selection properties if (!this.selectionProperty) this.selectionProperty = "_selection_"+isc.Selection._selectionID++; // set the data object so we get notification for add and delete, etc. // NOTE: if the data object wasn't set, use a new arrays this.setData((this.data ? this.data : []));},//> @method selection.setData()// Initialize selection data.<br><br>// Call this method to associate the selection with a different data object.<br>// <i>Note: No need to call this if the contents of the selection's data is modified</i>// @group selection// @param newData (array) new data to maintain selection in// @visibility serverSelection//<setData : function (newData) { // if we are currently pointing to data, stop observing it if (this.data != null) this.ignoreData(this.data); // remember the new data this.data = newData; // observe the new data so we will update automatically when it changes if (this.data != null) this.observeData(this.data);}, //> @method selection.observeData() (A)// Observe methods on the data so we change our state.// Called automatically by selection.setData().// Observes the data.dataChanged() method to invalidate the selection cache// @group selection////// @param data (array) new data to be observed// @visibility internal//<observeData : function (data) { this.observe(data, "dataChanged", "observer.dataChanged()"); if (data.dataArrived) this.observe(data, "dataArrived", "observer.dataChanged()");},//> @method selection.ignoreData() (A)// Stop observing methods on data when it goes out of scope.// Called automatically by setData// @group selection//// @param data (array) old data to be ignored// @visibility internal//<ignoreData : function (data) { this.ignore(data, "dataChanged");},dataChanged : function () { this.markForRedraw();},//> @method selection.markForRedraw()// Mark the selection as dirty, so it will be recalculated automatically // @group selection// @visibility internal//<markForRedraw : function () { this._dirty = true;}, //> @method selection.isSelected()// Return true if a particular item is selected// @group selection//// @param item (object) object to check // @return (boolean) true == object is selected// false == object is not selected// @visibility external//<isSelected : function (item){ if (item == null) return false; if (isc.isAn.XMLNode(item)) return "true" == item.getAttribute(this.selectionProperty); return item[this.selectionProperty];},//> @method selection.anySelected()// Whether at least one item is selected// @group selection//// @return (boolean) true == at least one item is selected// false == nothing at all is selected// @visibility external//<anySelected : function () { // return if the selection is non-empty return this.getSelection().length > 0;},//> @method selection.multipleSelected()// Whether multiple items are selected// @group selection//// @return (boolean) true == more than one item is selected// false == no items are selected, or only one item is selected// @visibility external//<multipleSelected : function () { return this.getSelection().length > 1;},//> @method selection.getSelection()// Return an ordered array of all of the selected items// @group selection//// @return (array) list of selected items// @visibility external//<getSelection : function () { // if the selection is dirty, cache it again if (this._dirty) this.cacheSelection(); // return the cached selection list return this._cache;},//> @method selection.getSelectedRecord()// Return the first item in the list that is selected.<br><br>//// Note that this should only be used if you know that one only one item// may be selected, or you really don't care about items after the first one.<br><br>//// To get all selected objects, use <code>+link{method:selection.getSelection()}</code>// @group selection//// @return (object) first selected record, or null if nothing selected// @visibility external//<getSelectedRecord : function () { var selection = this.getSelection(); if (selection && selection.length > 0) return selection[0];},//> @method selection.cacheSelection() (A)// Cache the selected records since we operate on it so often.// Sets an internal variable _cache to hold the selection.// @group selection// @visibility internal//<cacheSelection : function () { // create a new array to hold the cached selection this._cache = []; var data = this.data, isRS = isc.isA.ResultSet != null && isc.isA.ResultSet(data), length = data.getLength(); if (isRS && !data.lengthIsKnown()) { this._dirty = false; return; } // iterate over the records of the list, selecting those that have the selection property set for (var i = 0; i < length; i++) { // don't trigger fetches if working with a remote dataset if (isRS && !data.rowIsLoaded(i)) continue; var item = data.get(i); if (item != null && this.isSelected(item)) { this._cache[this._cache.length] = item } } // note that the selection is no longer dirty this._dirty = false;},//> @method selection.setSelected() (A)// Select or deselect a particular item.<br><br>// All other selection routines go through this one, so by observing this routine you can// monitor all selection changes.// @group selection//// @param item (object) object to select// @param newState (boolean) turn selection on or off // // @return (boolean) true == selection actually changed, false == no change// @visibility external//<setSelected : function (item, newState) { // if the item is null, just return if (item == null) return false; // if the item is not enabled, just return if (item.enabled == false) return false; var property = this.selectionProperty, isNode = isc.isAn.XMLNode(item); // default to selecting the item if (newState == null) newState = true; // get the oldState of the item, for detecting changes var oldState = isNode ? item.getAttribute(property) : item[property]; if (oldState == null) oldState = false; // set the state of the item if (isNode) { item.setAttribute(property, (newState == true) + ""); //this.logWarn("set attribute on: " + this.echoLeaf(item) + " to: " + newState + // ", now reads: " + item.getAttribute(property)); } else { item[property] = newState; } // remember that this was the last item to be selected this.lastSelectionItem = item; this.lastSelectionState = newState; // if no change to state of item, simply return false if (newState == oldState) return false; // note that the selection is dirty so it can be recalculated this.markForRedraw(); // return true to indicate that there was a change in the selection state return true;},//> @method selection.select()// Select a particular item// @group selection//// @param item (object) object to select // @return (boolean) true == selection actually changed, false == no change// @visibility external//<select : function (item) { return this.setSelected(item, true);},//> @method selection.deselect()// Deselect a particular item// @group selection//// @param item (object) object to select // @return (boolean) true == selection actually changed, false == no change// @visibility external//<deselect : function (item) { return this.setSelected(item, false);},//> @method selection.selectSingle()// Select a single item and deselect everything else// @group selection//// @param item (object) object to select // @return (boolean) true == selection actually changed, false == no change// @visibility external//<selectSingle : function (item) { this.deselectAll(); return this.select(item);},//> @method selection.selectList()// Select an array of items (subset of the entire list)// @group selection//// @param list (object[]) array of objects to select // @return (boolean) true == selection actually changed, false == no change// @visibility external//<selectList : function (list, newState) { if (newState == null) newState = true; if (!list) return false; var anyChanged = false, length = list.getLength(); for (var i = 0; i < length; i++) { var item = list.get(i); if (this.isSelected(item) == newState) continue; anyChanged = this.setSelected(item, newState) || anyChanged; } return anyChanged;},//> @method selection.deselectList()// Deselect an array of items (subset of the entire list)// @group selection//// @param list (object[]) array of objects to select // @return (boolean) true == selection actually changed, false == no change// @visibility external
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -