( function( $ ) { $.fn.ADropDown = function( options ) { var opts = $.extend( {}, $.fn.ADropDown.defaults, options ); var unselect_grid = null; var select_grid = null; var total_display_span = null; var static_source_data = null; //Always use this to help to set Select data var id = null; var key = 'id'; var parent_a_combo_box = null; var a_dropdown_this = this; var unselect_grid_header_array = []; var select_grid_header_array = []; var show_search_inputs = true; var local_search_mode = false; var local_search_optional_filter = []; var unselect_grid_search_map = null; var select_grid_search_map = null; var unselect_grid_sort_map = null; var select_grid_sort_map = null; var select_item = null; var tree_mode = false; var on_tree_grid_row_select = false; //#2566 - added select row callback so that trees can be used for edit-view navigation var unselect_grid_last_row = ''; var select_grid_last_row = ''; var allow_multiple_selection = true; var allow_drag_to_order = false; var pager_data = null; var paging_widget = null; var real_selected_items = null; //Set this after search in select grid; var start; var last; var next; var end; var paging_selector; var left_buttons_div; var right_buttons_div; var left_buttons_enable; var right_buttons_enable; var field; var error_tip_box; var error_string = ''; var default_height = 150; var unselect_grid_no_result_box = null; var select_grid_no_result_box = null; var box_width; var focus_in_select_grid = false; var auto_sort = false; var isChanged = false; var api = null; //Pass from owner var column_editor = null; var column_option_key = null; var display_column_settings = true; var quick_search_timer; var quick_search_typed_keys = ''; var quick_search_dic = {}; var max_height = false; var static_height = false; var resize_grids = false; //Select all records in target grid var selectAllInGrid = function( target, deSelect ) { target.resetSelection(); if ( !deSelect ) { var source_data = target.getGridParam( 'data' ); var len = source_data.length; for ( var i = 0; i < len; i++ ) { var item = source_data[i]; if ( item.hidden ) { continue; } if ( Global.isSet( item.id ) ) { target.grid.jqGrid( 'setSelection', item.id, false ); } else { target.grid.jqGrid( 'setSelection', i + 1, false ); } } target.grid.parents( '.cbox-header' ).prop( 'checked', true ); } }; Global.addCss( 'global/widgets/awesomebox/ADropDown.css' ); this.isLocalSearchMode = function() { return local_search_mode; }; this.setLocalSearchMode = function( value ) { local_search_mode = value; }; this.isChanged = function() { return isChanged; }; this.setIsChanged = function( val ) { isChanged = val; }; this.unSelectAll = function( target ) { selectAllInGrid( target, true ); }; this.getFocusInSeletGrid = function() { return focus_in_select_grid; }; this.selectAll = function() { if ( focus_in_select_grid ) { selectAllInGrid( select_grid ); } else { selectAllInGrid( unselect_grid ); } }; this.gridScrollTop = function() { unselect_grid.grid.parent().parent().scrollTop( 0 ); }; this.gridScrollDown = function() { unselect_grid.grid.parent().parent().scrollTop( 10000 ); }; this.getBoxWidth = function() { return box_width; }; this.setErrorStyle = function( errStr, show, isWarning ) { if ( isWarning ) { $( this ).addClass( 'warning-tip' ); } else { $( this ).addClass( 'error-tip' ); } error_string = errStr; if ( show ) { this.showErrorTip(); } }; this.showErrorTip = function( sec ) { if ( !Global.isSet( sec ) ) { sec = 2; } if ( !error_tip_box ) { error_tip_box = Global.loadWidgetByName( WidgetNamesDic.ERROR_TOOLTIP ); error_tip_box = error_tip_box.ErrorTipBox(); } if ( $( this ).hasClass( 'warning-tip' ) ) { error_tip_box.show( this, error_string, sec, true ); } else { error_tip_box.show( this, error_string, sec ); } }; this.hideErrorTip = function() { if ( Global.isSet( error_tip_box ) ) { error_tip_box.remove(); } }; this.clearErrorStyle = function() { $( this ).removeClass( 'error-tip' ); $( this ).removeClass( 'warning-tip' ); this.hideErrorTip(); error_string = ''; }; this.getField = function() { return field; }; // Must call after setUnSelectGridData this.setValue = function( val ) { this.setSelectGridData( val ); }; this.getValue = function() { return this.getSelectItems(); }; this.getSelectGridSortMap = function() { return select_grid_sort_map; }; this.getUnSelectGridSortMap = function() { return unselect_grid_sort_map; }; this.getUnSelectGridMap = function() { if ( !unselect_grid_search_map ) { unselect_grid_search_map = {}; } return unselect_grid_search_map; }; this.getUnSelectGrid = function() { return unselect_grid; }; this.getSelectGrid = function() { return select_grid; }; this.getSelectGridMap = function() { if ( !select_grid_search_map ) { select_grid_search_map = {}; } var ids = []; if ( allow_multiple_selection ) { var select_items = a_dropdown_this.getSelectItems(); for ( var i = 0; i < select_items.length; i++ ) { ids.push( select_items[i][key] ); } } if ( ids.length > 0 ) { select_grid_search_map.id = ids; } else { return false; } return select_grid_search_map; }; this.collectUnselectGridColumns = function() { var columns = unselect_grid.getGridParam( 'colModel' ); var len = ( ( columns ) ? columns.length : 0 ); unselect_grid_header_array = []; for ( var i = 0; i < len; i++ ) { var column_info = columns[i]; var column_header = $( this ).find( 'div #jqgh_unselect_grid_' + id + '_' + column_info.name ); unselect_grid_header_array.push( column_header.TGridHeader( { column_model: column_info } ) ); column_header.bind( 'headerClick', onUnSelectColumnHeaderClick ); } a_dropdown_this.setGridHeaderStyle( 'unselect_grid' ); function onUnSelectColumnHeaderClick( e, headerE, column_model ) { //Error: Uncaught TypeError: Cannot read property 'setCachedSortFilter' of null in /interface/html5/global/widgets/awesomebox/ADropDown.js?v=7.4.6-20141027-072624 line 286 if ( !parent_a_combo_box || !parent_a_combo_box.getAPI() ) { return; } var field = column_model.name; if ( field === 'cb' ) { //first column, check box column. return; } if ( headerE.metaKey || headerE.ctrlKey ) { a_dropdown_this.buildSortCondition( false, field, 'unselect_grid' ); } else { a_dropdown_this.buildSortCondition( true, field, 'unselect_grid' ); } parent_a_combo_box.setCachedSortFilter( unselect_grid_sort_map ); a_dropdown_this.setGridHeaderStyle( 'unselect_grid' ); parent_a_combo_box.onADropDownSearch( 'unselect_grid' ); } }; this.buildSortCondition = function( reset, field, targetName ) { var sort_map = null; var nextSort = 'desc'; if ( targetName === 'unselect_grid' ) { sort_map = unselect_grid_sort_map; } else { sort_map = select_grid_sort_map; } if ( reset ) { if ( sort_map && sort_map.length > 0 ) { var len = sort_map.length; var found = false; for ( i = 0; i < len; i++ ) { var sortItem = sort_map[i]; for ( var key in sortItem ) { if ( key === field ) { if ( sortItem[key] === 'asc' ) { nextSort = 'desc'; } else { nextSort = 'asc'; } found = true; } } if ( found ) { break; } } } sort_map = [ {} ]; sort_map[0][field] = nextSort; } else { if ( !sort_map ) { sort_map = [ {} ]; sort_map[0][field] = 'desc'; } else { len = sort_map.length; found = false; for ( var i = 0; i < len; i++ ) { sortItem = sort_map[i]; for ( var key in sortItem ) { if ( key === field ) { if ( sortItem[key] === 'asc' ) { sortItem[key] = 'desc'; } else { sortItem[key] = 'asc'; } found = true; } } if ( found ) { break; } } if ( !found ) { sort_map.push( {} ); sort_map[len][field] = 'desc'; } } } if ( targetName === 'unselect_grid' ) { unselect_grid_sort_map = sort_map; } else { select_grid_sort_map = sort_map; } }; this.setGridHeaderStyle = function( targetName ) { var headerArray = []; var sort_map = []; if ( targetName === 'unselect_grid' ) { headerArray = unselect_grid_header_array; sort_map = unselect_grid_sort_map; } else { headerArray = select_grid_header_array; sort_map = select_grid_sort_map; } var len = headerArray.length; for ( var i = 0; i < len; i++ ) { var tGridHeader = headerArray[i]; var field = tGridHeader.getColumnModel().name; tGridHeader.cleanSortStyle(); if ( sort_map ) { var sortArrayLen = sort_map.length; for ( var j = 0; j < sortArrayLen; j++ ) { var sortItem = sort_map[j]; var sortField = Global.getFirstKeyFromObject( sortItem ); if ( sortField === field ) { if ( sortArrayLen > 1 ) { tGridHeader.setSortStyle( sortItem[sortField], j + 1 ); } else { tGridHeader.setSortStyle( sortItem[sortField], 0 ); } } } } } }; this.collectSelectGridColumns = function() { var columns = select_grid.getGridParam( 'colModel' ); var len = ( ( columns ) ? columns.length : 0 ); select_grid_header_array = []; for ( var i = 0; i < len; i++ ) { var column_info = columns[i]; var column_header = $( this ).find( 'div #jqgh_select_grid_' + id + '_' + column_info.name ); select_grid_header_array.push( column_header.TGridHeader( { column_model: column_info } ) ); column_header.bind( 'headerClick', onSelectColumnHeaderClick ); } a_dropdown_this.setGridHeaderStyle( 'select_grid' ); function onSelectColumnHeaderClick( e, headerE, column_model ) { if ( !parent_a_combo_box || !parent_a_combo_box.getAPI() ) { return; } var field = column_model.name; if ( field === 'cb' ) { //first column, check box column. return; } if ( headerE.metaKey || headerE.ctrlKey ) { a_dropdown_this.buildSortCondition( false, field, 'select_grid' ); } else { a_dropdown_this.buildSortCondition( true, field, 'select_grid' ); } parent_a_combo_box.setCachedSelectedGridSortFilter( select_grid_sort_map ); a_dropdown_this.setGridHeaderStyle( 'select_grid' ); parent_a_combo_box.onADropDownSearch( 'select_grid' ); } }; //HightLight select item in UnSelect grid when !allow_multiple_selection this.setSelectItem = function( val, target_grid ) { if ( !target_grid ) { target_grid = unselect_grid; } var source_data = target_grid.getGridParam( 'data' ); val && ( select_item = val ); if ( source_data && source_data.length > 0 ) { for ( var i = 0; i < source_data.length; i++ ) { var content = source_data[i]; var temp_val = val; !val && ( temp_val = source_data[0] ); var content_key = key; if ( tree_mode && key == 'id' ) { content_key = '_id_'; } if ( content[content_key] == temp_val[key] ) { //Some times 0, sometimes '0' var content_id_key = 'id'; if ( tree_mode ) { content_id_key = '_id_'; } //Always use id to set select row, all record array should have id target_grid.grid.find( 'tr[id="' + content[content_id_key] + '"]' ).focus(); if ( target_grid.grid.hasClass( 'unselect-grid' ) ) { if ( unselect_grid_last_row ) { target_grid.grid.jqGrid( 'saveRow', unselect_grid_last_row ); } unselect_grid_last_row = content[content_id_key]; } else { if ( select_grid_last_row ) { target_grid.grid.jqGrid( 'saveRow', select_grid_last_row ); } select_grid_last_row = content[content_id_key]; } target_grid.grid.jqGrid( 'setSelection', content[content_id_key], false ); target_grid.grid.jqGrid( 'editRow', content[content_id_key], true ); val && ( select_item = content ); return false; } } this.setTotalDisplaySpan(); } }; this.getUnSelectGridData = function() { return unselect_grid.getGridParam( 'data' ); }; this.getSelectItem = function() { return select_item; }; this.getSelectItems = function() { //Save last edit row if there is editable row in grid cell; if ( unselect_grid_last_row.length > 0 ) { unselect_grid.grid.jqGrid( 'saveRow', unselect_grid_last_row ); unselect_grid_last_row = ''; } if ( select_grid_last_row.length > 0 ) { select_grid.grid.jqGrid( 'saveRow', select_grid_last_row ); select_grid_last_row = ''; } var retval = null; if ( show_search_inputs && real_selected_items && real_selected_items.length > 0 ) { retval = real_selected_items; } else { retval = select_grid.getGridParam( 'data' ); //Set this when setSelectGridItems } //Make sure we never return null, and always at least an empty array. This helps prevent JS excptions when we run .length on the return value. if ( Global.isArray( retval ) == false ) { return Array(); } return retval; }; this.setRealSelectItems = function( all_records, selected_ids ) { real_selected_items = []; if ( selected_ids != TTUUID.zero_id && selected_ids.length > 0 ) { for ( var n = 0; n < all_records.length; n++ ) { if ( selected_ids.indexOf( all_records[n].id ) != -1 ) { real_selected_items.push( all_records[n] ); } } } return real_selected_items; }; this.getAllowMultipleSelection = function() { return allow_multiple_selection; }; this.getResizeGrids = function() { return resize_grids; }; this.setResizeGrids = function( val ) { resize_grids = val; if ( val ) { this.setGridColumnsWidths(); } }; this.getTreeMode = function() { return tree_mode; }; //Must Set this after set Columns this.setUnselectedGridData = function( val ) { static_source_data = val; if ( !tree_mode ) { unselect_grid.setData( val ); this.setTotalDisplaySpan(); } else { this.reSetUnSelectGridTreeData( val ); } this.setUnSelectGridDragAble(); this.setGridsHeight(); }; /** * This function calculates the optimal widths for awesomebox columns * and whether they should overflow or shrink to fit */ this.setGridColumnsWidths = function() { var primary_grid = unselect_grid; var secondary_grid = select_grid; //don't swap the grids. always size on left grid. var p_data = primary_grid.getData(); var s_data = secondary_grid.getData(); if ( s_data && s_data.length != 0 && p_data && p_data.length == 0 ) { //swap grids if no data in primary. var temp = primary_grid; primary_grid = secondary_grid; secondary_grid = temp; } //Make sure what dropdown is expanded, we do not spill over the right edge of the browser causing scrollbars to appear. var max_grid_width = ( ( ( ( Global.bodyWidth() - $( this ).offset().left ) - 100 ) / 2 ) ); var default_width = primary_grid.setGridColumnsWidth( null, { max_grid_width: max_grid_width } ); var colModel = primary_grid.getColumnModel(); secondary_grid.setGridColumnsWidth( colModel ); var width = 200; var offset = 28; //awesomeboxes with more than 1 column need to adjust for td borders. var total_headers_width = 0; if ( colModel ) { for ( var i = 0; i < colModel.length; i++ ) { total_headers_width += colModel[i].widthOrg + 1; //collect the (calculated) column widths (+1 for border } if ( colModel[0].name == 'cb' ) { offset += 2; } } if ( tree_mode ) { offset = 26; } else { offset = 22; } //only shrink smaller grids to fit container if ( total_headers_width <= ( width ) || colModel.length < 5 ) { //prevent resize on search. width = primary_grid.grid.parents( '.unselect-grid-div, .select-grid-div' ).width() - offset; } else { width = total_headers_width; //primary_grid.grid.parents( '.unselect-grid-div' ).width() - offset; if ( default_width > width ) { width = default_width; } } primary_grid.setGridWidth( width ); secondary_grid.setGridWidth( width ); this.resizeUnSelectSearchInputs(); this.resizeSelectSearchInputs(); }; this.getPagerData = function() { return pager_data; }; //Always setPager data no matter static options or api. this.setPagerData = function( value ) { pager_data = value; if ( LocalCacheData.paging_type === 0 ) { if ( paging_widget.parent().length > 0 ) { paging_widget.remove(); } paging_widget.css( 'width', unselect_grid.width() ); unselect_grid.append( paging_widget ); paging_widget.click( function() { $this.onPaging(); } ); if ( !pager_data || pager_data.is_last_page || pager_data.last_page_number < 0 ) { paging_widget.css( 'display', 'none' ); } else { paging_widget.css( 'display', 'inline-block' ); } } else { if ( !pager_data || pager_data.last_page_number < 0 ) { left_buttons_div.css( 'display', 'none' ); right_buttons_div.css( 'display', 'none' ); } else { left_buttons_div.css( 'display', 'inline-block' ); right_buttons_div.css( 'display', 'inline-block' ); if ( pager_data.is_last_page === true ) { right_buttons_div.addClass( 'disabled' ); right_buttons_div.addClass( 'disabled-image' ); right_buttons_enable = false; } else { right_buttons_div.removeClass( 'disabled' ); right_buttons_div.removeClass( 'disabled-image' ); right_buttons_enable = true; } if ( pager_data.is_first_page ) { left_buttons_div.addClass( 'disabled' ); left_buttons_div.addClass( 'disabled-image' ); left_buttons_enable = false; } else { left_buttons_div.removeClass( 'disabled' ); left_buttons_div.removeClass( 'disabled-image' ); left_buttons_enable = true; } } } a_dropdown_this.setTotalDisplaySpan(); }; this.onPaging = function() { parent_a_combo_box.onADropDownSearch( 'unselect_grid', 'next' ); }; this.reSetUnSelectGridTreeData = function( val ) { var scroll_position = unselect_grid.grid.parent().parent().scrollTop(); var grid_data = unselect_grid.getData(); if ( grid_data && grid_data.length > 0 && val && val.length > 0 ) { //If in tree_mode, don't clear the unselected grid, since we just bold/unbold the selected items otherwise the "tree" layout would be lost. unselect_grid.setData( val, !tree_mode ); } else { var col_model = unselect_grid.getGridParam( 'colModel' ); if ( !unselect_grid.grid.is( ':visible' ) ) { return; } unselect_grid.grid.jqGrid( 'GridUnload' ); unselect_grid = $( this ).find( '.unselect-grid' ); unselect_grid = unselect_grid.grid = new TTGrid( $( this ).find( '.unselect-grid' ).attr( 'id' ), { container_selector: '.unselect-grid-div', altRows: true, datastr: val, datatype: 'jsonstring', sortable: false, width: 440, onResizeGrid: resize_grids, //maxHeight: default_height, colNames: [], rowNum: 10000, colModel: col_model, ondblClickRow: a_dropdown_this.onUnSelectGridDoubleClick, gridview: true, treeGrid: true, treeGridModel: 'adjacency', treedatatype: 'local', ExpandColumn: 'name', multiselect: allow_multiple_selection, multiselectPosition: 'none', winMultiSelect: allow_multiple_selection, onCellSelect: function( id, k, el, e ) { if ( $( e.target ).prop( 'class' ).indexOf( 'ui-icon-triangle' ) != -1 ) { return false; } id = Global.convertToNumberIfPossible( id ); if ( !allow_multiple_selection ) { var source_data = unselect_grid.getGridParam( 'data' ); $.each( source_data, function( index, content ) { if ( tree_mode && key == 'id' ) { key = '_id_'; } if ( content[key] == id ) { select_item = content; isChanged = true; if ( !LocalCacheData.currently_collapsing_navigation_tree_element ) { //#2583 - must allow null or false a_dropdown_this.trigger( 'close', [a_dropdown_this] ); } return; } } ); } if ( on_tree_grid_row_select ) { on_tree_grid_row_select( id, LocalCacheData.currently_collapsing_navigation_tree_element ); } }, beforeSelectRow: function( iRow, e ) { var $td = $( e.target ).closest( 'tr.jqgrow>td' ), iCol = $td.length > 0 ? $td[0].cellIndex : -1, p = $( this ).jqGrid( 'getGridParam' ), cm = iCol >= 0 ? p.colModel[iCol] : null; if ( cm != null && cm.name === p.ExpandColumn && $( e.target ).closest( '.tree-wrap' ).length > 0 ) { return false; // prevent row selection } return true; }, jsonReader: { repeatitems: false, root: function( obj ) { return obj; }, page: function( obj ) { return 1; }, total: function( obj ) { return 1; }, records: function( obj ) { return obj.length; } }, onResizeGrid: resize_grids }, col_model ); } scroll_position > 0 && unselect_grid.grid.parent().parent().scrollTop( scroll_position ); this.setGridsHeight(); var select_items = this.getSelectItems(); $( this ).find( 'tr' ).removeClass( 'selected-tree-cell' ); for ( var i = ( select_items.length - 1 ); i >= 0; i-- ) { $( this ).find( '.unselect-grid-div' ).find( 'tr#' + select_items[i].id ).addClass( 'selected-tree-cell' ); } }; this.setGridsHeight = function() { //Calculate the max possible size of awesomebox. if ( static_height ) { unselect_grid.grid.setGridHeight( static_height ); if ( allow_multiple_selection ) { select_grid.grid.setGridHeight( static_height ); } return; } if ( !parent_a_combo_box ) { return; } var top_offset = parent_a_combo_box.offset().top; var bottom_offset = Global.bodyHeight() - top_offset - 30; var new_height = top_offset > bottom_offset ? top_offset : bottom_offset; new_height = new_height - 130; var source_data = parent_a_combo_box.getStaticSourceData(); if ( !source_data ) { new_height = default_height; } else { var source_height = source_data.length * 23; if ( source_height < default_height ) { new_height = default_height; } else if ( source_height > default_height && source_height < new_height ) { new_height = source_height; } } if ( max_height && max_height < new_height ) { new_height = max_height; } unselect_grid.grid.setGridHeight( new_height ); if ( allow_multiple_selection ) { select_grid.grid.setGridHeight( new_height ); } this.setPosition( top_offset, new_height ); }; this.setPosition = function( top_offset, new_height ) { if ( top_offset + new_height + 130 < Global.bodyHeight() ) { a_dropdown_this.parent().css( 'top', top_offset + 25 ); } else { if ( new_height != default_height ) { a_dropdown_this.parent().css( 'top', ( top_offset - new_height - 125 ) ); } else { a_dropdown_this.parent().css( 'top', ( top_offset - new_height - 125 ) ); } } }; this.onTreeCellFormat = function( cell_value, related_data, row ) { var selected_items = a_dropdown_this.getSelectItems(); for ( var i = 0, m = selected_items.length; i < m; i++ ) { var item = selected_items[i]; if ( item.name === cell_value ) { return '' + cell_value + ''; } } return cell_value; }; //Do this before set data this.setColumns = function( val ) { for ( var a = 0; a < val.length; a++ ) { val[a].resizable = false; } unselect_grid.grid.jqGrid( 'GridUnload' ); unselect_grid = $( this ).find( '.unselect-grid' ); unselect_grid.getGridParam(); var unselect_grid_search_div = $( this ).find( '.unselect-grid-search-div' ); var select_grid_search_div = $( this ).find( '.select-grid-search-div' ); var gridWidth = $( '.unselect-grid-border-div' ).width() - 2; // single-column width if ( show_search_inputs ) { gridWidth -= 15; } gridWidth = val.length * 125; if ( gridWidth < 438 ) { box_width = gridWidth = 438; } if ( val.length > 1 ) { box_width = gridWidth; if ( allow_multiple_selection && gridWidth > ( Global.bodyWidth() / 2 - 30 - 15 ) ) { box_width = ( Global.bodyWidth() / 2 - 30 - 15 ); } else if ( !allow_multiple_selection && gridWidth > ( Global.bodyWidth() - 30 - 15 ) ) { box_width = ( Global.bodyWidth() - 30 - 15 ); } this.find( '.unselect-grid-div' ).width( null ); this.find( '.unselect-grid-border-div' ).width( null ); this.find( '.select-grid-div' ).width( null ); this.find( '.select-grid-border-div' ).width( null ); if ( show_search_inputs ) { unselect_grid_search_div.css( 'width', null ); select_grid_search_div.css( 'width', null ); } } else { box_width = gridWidth; } if ( !tree_mode ) { unselect_grid = new TTGrid( $( unselect_grid ).attr( 'id' ), { container_selector: '.unselect-grid-div', altRows: true, data: [], datatype: 'local', sortable: false, width: gridWidth, //maxHeight: default_height, colNames: [], rowNum: 10000, keep_scroll_place: true, ondblClickRow: a_dropdown_this.onUnSelectGridDoubleClick, colModel: val, multiselect: allow_multiple_selection, multiboxonly: allow_multiple_selection, viewrecords: true, editurl: 'clientArray', resizeStop: function() { a_dropdown_this.resizeUnSelectSearchInputs(); }, onCellSelect: function( id ) { id = Global.convertToNumberIfPossible( id ); if ( !allow_multiple_selection ) { var source_data = unselect_grid.getGridParam( 'data' ); $.each( source_data, function( index, content ) { if ( key !== 'id' ) { if ( content['id'] == id ) { select_item = content; isChanged = true; a_dropdown_this.trigger( 'close', [a_dropdown_this] ); return false; } } else { if ( content[key] == id ) { select_item = content; isChanged = true; a_dropdown_this.trigger( 'close', [a_dropdown_this] ); return false; } } } ); } if ( unselect_grid_last_row ) { unselect_grid.grid.jqGrid( 'saveRow', unselect_grid_last_row ); } unselect_grid.grid.jqGrid( 'editRow', id, true ); unselect_grid_last_row = id; a_dropdown_this.setTotalDisplaySpan(); function getSelectValue() { var len = source_data.length; } }, onResizeGrid: resize_grids }, val ); } else { if ( tree_mode ) { var tree_columns = _.map( val, _.clone ); _.map( tree_columns, function( item ) { item.formatter = a_dropdown_this.onTreeCellFormat; } ); } unselect_grid = new TTGrid( $( unselect_grid ).attr( 'id' ), { container_selector: '.unselect-grid-div', altRows: true, datastr: [], datatype: 'jsonstring', sortable: false, width: gridWidth, maxHeight: default_height, colNames: [], rowNum: 10000, colModel: tree_columns, ondblClickRow: a_dropdown_this.onUnSelectGridDoubleClick, onCellSelect: function() { a_dropdown_this.onUnSelectGridSelectRow(); }, gridview: true, treeGrid: true, treeGridModel: 'adjacency', treedatatype: 'local', ExpandColumn: 'name', multiselect: false, //allow_multiple_selection, jsonReader: { repeatitems: false, root: function( obj ) { return obj; }, page: function( obj ) { return 1; }, total: function( obj ) { return 1; }, records: function( obj ) { return obj.length; } }, onSelectRow: function( id ) { if ( on_tree_grid_row_select ) { on_tree_grid_row_select( id ); } id = Global.convertToNumberIfPossible( id ); }, onResizeGrid: resize_grids }, tree_columns ); } this.collectUnselectGridColumns(); //Make each column as THeader plugin and save them if ( show_search_inputs ) { if ( !parent_a_combo_box || !parent_a_combo_box.getAPI() ) { this.setLocalSearchMode( true ); //If no API allow local searching of the dropdown items. } this.buildUnSelectSearchInputs(); //Build search input above columns } select_grid.grid.jqGrid( 'GridUnload' ); select_grid = $( this ).find( '.select-grid' ); select_grid = new TTGrid( $( select_grid ).attr( 'id' ), { container_selector: '.select-grid-div', altRows: true, data: [], datatype: 'local', sortable: false, width: gridWidth, //maxHeight: default_height, colNames: [], rowNum: 10000, ondblClickRow: this.onSelectGridDoubleClick, colModel: val, multiselect: allow_multiple_selection, multiboxonly: allow_multiple_selection, viewrecords: true, keep_scroll_place: true, resizeStop: function() { a_dropdown_this.resizeSelectSearchInputs(); }, onCellSelect: function( id ) { if ( id ) { if ( select_grid_last_row ) { select_grid.grid.jqGrid( 'saveRow', select_grid_last_row ); } select_grid.grid.jqGrid( 'editRow', id, true ); select_grid_last_row = id; } }, onResizeGrid: resize_grids }, val ); this.collectSelectGridColumns(); //Make each column as THeader plugin and save them if ( show_search_inputs ) { if ( !parent_a_combo_box || !parent_a_combo_box.getAPI() ) { this.setLocalSearchMode( true ); //If no API allow local searching of the dropdown items. } this.buildSelectSearchInputs(); //Build search input above columns } }; this.buildSelectSearchInputs = function() { var len = select_grid_header_array.length; var search_div = $( this ).find( '.select-grid-search-div' ); var first_column_width = 0; var search_input_array = []; for ( var i = 0; i < len; i++ ) { var header = select_grid_header_array[i]; if ( i === 0 ) { first_column_width = header.getWidth(); continue; } else if ( allow_multiple_selection && i === 1 ) { var search_input = $( '' ); search_input.css( 'width', header.getWidth() + first_column_width ); } else { search_input = $( '' ); search_input.css( 'width', header.getWidth() ); } search_input.on( 'drop', function( e ) { e.preventDefault(); } ); search_input.ASearchInput( { column_model: header.getColumnModel() } ); //Make it as ASearchInout Widget; search_div.append( search_input ); search_input_array.push( search_input ); //Set cached seach_input data back, usually in navigation_mode if ( select_grid_search_map ) { search_input.setFilter( select_grid_search_map ); } search_input.bind( 'searchEnter', function( e, searchVal, field ) { if ( a_dropdown_this.getValue().length < 1 ) { return; } if ( !select_grid_search_map ) { select_grid_search_map = {}; } delete select_grid_search_map.id; if ( !searchVal ) { delete select_grid_search_map[field]; } else { select_grid_search_map[field] = searchVal; } if ( a_dropdown_this.isLocalSearchMode() ) { a_dropdown_this.localSearch( 'select', select_grid_search_map ); } else { parent_a_combo_box.setCachedSelectGridSearchInputsFilter( Global.clone( select_grid_search_map ) ); parent_a_combo_box.onADropDownSearch(); } } ); } var close_btn = $( '' ); //close_btn.width( unselect_grid_header_array[0].getWidth() + 2 ); //close_btn.width( 22 ); search_div.prepend( close_btn ); var $this = this; close_btn.click( function() { //clear search inputs for select box select_grid_search_map = {}; if ( a_dropdown_this.isLocalSearchMode() ) { a_dropdown_this.localSearch( 'select', select_grid_search_map ); } else { parent_a_combo_box.setCachedSelectGridSearchInputsFilter( select_grid_search_map ); parent_a_combo_box.onADropDownSearch(); } for ( var i = 0; i < search_input_array.length; i++ ) { var s_i = search_input_array[i]; s_i.clearValue(); } } ); }; this.localSearch = function( grid_type, grid_search_map, optional_filter ) { if ( !optional_filter ) { //Replicates API Search functionality where newly added records are shown even if they don't match the search criteria. optional_filter = []; local_search_optional_filter = []; } //Merge in repeated optional search, so that user adding 1 by 1 will continue to see all new items. local_search_optional_filter = local_search_optional_filter.concat( optional_filter ); let search_grid = null; let search_items = []; if ( grid_type === 'select' ) { search_grid = select_grid; } else { search_grid = unselect_grid; } search_grid.resetSelection(); //Reset all checked items to replicate behavior of API search. search_items = search_grid.getData(); //Need to search against multiple terms and columns. let meetAllSearchCriteria = ( item, grid_search_map ) => { for ( let key in grid_search_map ) { if ( grid_search_map.hasOwnProperty( key ) ) { let value = grid_search_map[key].trim(); if ( !item[key] || item[key].toLowerCase().includes( value.toLowerCase() ) === false ) { return false; } } } return true; }; for ( let i = 0; i < search_items.length; i++ ) { if ( _.isEmpty( grid_search_map ) || local_search_optional_filter.some( item => item.id === search_items[i].id ) || meetAllSearchCriteria( search_items[i], grid_search_map ) ) { $( search_grid.grid ).find( '[id="' + search_items[i].id + '"]' ).show(); search_items[i].hidden = false; } else { $( search_grid.grid ).find( '[id="' + search_items[i].id + '"]' ).hide(); search_items[i].hidden = true; //Hidden items are not selectable even with "Select all" } } }; this.resizeUnSelectSearchInputs = function() { var search_div = $( this ).find( '.unselect-grid-search-div' ); var search_inputs = search_div.find( '.search-input' ); var first_column_width; var unselect_grid_search_div = $( this ).find( '.unselect-grid-search-div' ); var len = search_inputs.length; var header; var search_input; if ( allow_multiple_selection ) { first_column_width = unselect_grid_header_array[0].width(); if ( len == 1 ) { header = unselect_grid_header_array[1]; search_input = $( search_inputs[0] ); search_input.css( 'width', header.getWidth() ); } else { for ( var i = 0; i < len; i++ ) { header = unselect_grid_header_array[i + 1]; search_input = $( search_inputs[i] ); if ( i == ( len - 1 ) ) { search_input.css( 'width', header.getWidth() ); } else { search_input.css( 'width', header.getWidth() + 1 ); } } } } else { for ( i = 0; i < len; i++ ) { header = unselect_grid_header_array[i]; search_input = $( search_inputs[i] ); if ( i === 0 ) { search_input.css( 'width', header.getWidth() - 22 ); } else if ( i == ( len - 1 ) ) { search_input.css( 'width', header.getWidth() + 1 ); } else { search_input.css( 'width', header.getWidth() + 1 ); } } } var unselect_grid_width = unselect_grid.grid.parents( '.ui-jqgrid-jquery-ui' ).width() ? unselect_grid.grid.parent().parent().width() : 100; unselect_grid.setGridWidth( unselect_grid_width ); //var unselect_grid_width = unselect_grid.getWidth(); var unselect_grid_search_div_width = unselect_grid_width; if ( tree_mode ) { unselect_grid_width = unselect_grid.getWidth(); } var outer_box_width = unselect_grid_width + 22; var inner_box_width = unselect_grid_width + 4; var max_width = ( $( 'body' ).width() - 30 ); if ( allow_multiple_selection ) { max_width = ( ( $( 'body' ).width() / 2 ) - 30 ); } if ( outer_box_width > max_width ) { outer_box_width = max_width; inner_box_width = max_width - 17; } unselect_grid.grid.parents( '.unselect-grid-div' ).css( 'width', outer_box_width ); //outer blue box width unselect_grid.grid.parents( '.unselect-grid-border-div' ).css( 'width', inner_box_width ); //red border div. unselect_grid_search_div.css( 'width', unselect_grid_search_div_width ); }; this.resizeSelectSearchInputs = function() { var search_div = $( this ).find( '.select-grid-search-div' ); var search_inputs = search_div.find( '.search-input' ); var first_column_width; var select_grid_search_div = $( this ).find( '.select-grid-search-div' ); var len = search_inputs.length; first_column_width = select_grid_header_array[0].width() + 5; if ( len == 1 ) { header = unselect_grid_header_array[1]; search_input = $( search_inputs[0] ); search_input.css( 'width', header.getWidth() ); } else { for ( var i = 0; i < len; i++ ) { var header = select_grid_header_array[i + 1]; var search_input = $( search_inputs[i] ); if ( i == ( len - 1 ) ) { search_input.css( 'width', header.getWidth() ); } else { search_input.css( 'width', header.getWidth() + 1 ); } } } var newWidth = select_grid.grid.parents( '.ui-jqgrid-jquery-ui' ).width() ? select_grid.grid.parent().parent().width() : 100; select_grid.setGridWidth( newWidth ); var outer_box_width = newWidth + 22; var inner_box_width = newWidth + 4; var max_width = ( ( $( 'body' ).width() / 2 ) - 30 ); if ( outer_box_width > max_width ) { outer_box_width = max_width; inner_box_width = max_width - 17; } select_grid.grid.parents( '.select-grid-div' ).css( 'width', outer_box_width ); //outer blue box width select_grid.grid.parents( '.select-grid-border-div' ).css( 'width', inner_box_width ); //red border div. select_grid_search_div.css( 'width', newWidth ); }; this.buildUnSelectSearchInputs = function() { var len = unselect_grid_header_array.length; var search_div = $( this ).find( '.unselect-grid-search-div' ); var first_column_width = 0; var search_input_array = []; for ( var i = 0; i < len; i++ ) { var header = unselect_grid_header_array[i]; if ( allow_multiple_selection && i === 0 ) { first_column_width = header.getWidth(); continue; } else if ( allow_multiple_selection && i === 1 ) { var search_input = $( '' ); var width = header.getWidth() + first_column_width - 2; search_input.css( 'width', width ); } else { search_input = $( '' ); if ( i == ( len - 1 ) ) { search_input.css( 'width', header.getWidth() + 1 + 'px' ); } else { search_input.css( 'width', header.getWidth() + 'px' ); } } search_input.on( 'drop', function( e ) { e.preventDefault(); } ); search_input.ASearchInput( { column_model: header.getColumnModel() } ); //Make it as ASearchInout Widget; search_div.append( search_input ); search_input_array.push( search_input ); //Set cached seach_input data back, unsualy in navigation_mode if ( unselect_grid_search_map ) { search_input.setFilter( unselect_grid_search_map ); } //Do Column Search search_input.bind( 'searchEnter', function( e, searchVal, field ) { if ( !unselect_grid_search_map ) { unselect_grid_search_map = {}; } if ( !searchVal ) { delete unselect_grid_search_map[field]; } else { unselect_grid_search_map[field] = searchVal; } if ( a_dropdown_this.isLocalSearchMode() ) { //Search in local data may not always have a parent_a_combo_box such as in the case of Permission Groups. a_dropdown_this.localSearch( 'unselect', unselect_grid_search_map ); } else { parent_a_combo_box.setCachedSearchInputsFilter( unselect_grid_search_map ); parent_a_combo_box.onADropDownSearch( 'unselect_grid' ); } } ); } var close_btn = $( '' ); if ( allow_multiple_selection ) { //close_btn.width( 22 ) } else { //close_btn.width( 14 ); } search_div.prepend( close_btn ); close_btn.click( function() { unselect_grid_search_map = {}; if ( a_dropdown_this.isLocalSearchMode() ) { a_dropdown_this.localSearch( 'unselect', unselect_grid_search_map ); } else { parent_a_combo_box.setCachedSearchInputsFilter( unselect_grid_search_map ); parent_a_combo_box.onADropDownSearch( 'unselect_grid' ); } for ( var i = 0; i < search_input_array.length; i++ ) { var s_i = search_input_array[i]; s_i.clearValue(); } } ); }; //Set select item when not allow multiple selection // *IMPORTANT* This cannot be defined as this.setSelectItem() as it will break. setSelectItem = function( val ) { select_item = val; }; //Search Reesult in select grid. it's not effect the selectitems when getSelectItems this.setSelectGridSearchResult = function( val ) { if ( !real_selected_items || real_selected_items.length == 0 ) { //Clone the array with .slice(), this fixes the bug where you have an empty dropdown (no selected items) //You move 5 items to the right side, then search within those items to show only 1, then clear the search, and only 1 item would still be shown. real_selected_items = this.getSelectItems().slice(); } select_grid.setData( val ); }; //Must Set this after setUnselectedGridData for now //Remove select items form allColumn array this.setSelectGridData = function( val, searchResult ) { if ( parent_a_combo_box && parent_a_combo_box.getAPI() ) { val = Global.formatGridData( val, parent_a_combo_box.getAPI().key_name ); } if ( Object.prototype.toString.call( static_source_data ) !== '[object Array]' || static_source_data.length < 1 ) { static_source_data = []; } //Uncaught TypeError: Cannot read property 'length' of undefined if ( !val ) { val = []; } var all_columns = static_source_data.slice(); //Copy from Static data var i; var j; var select_item; var tmp_select_items; var all_columns_len; if ( all_columns && all_columns.length > 0 ) { var selectItemLen = val.length; if ( !auto_sort ) { for ( i = 0; i < selectItemLen; i++ ) { select_item = val[i]; if ( !Global.isSet( select_item[key] ) ) { select_item = []; select_item[key] = val[i]; if ( !Global.isSet( tmp_select_items ) ) { tmp_select_items = []; } } all_columns_len = all_columns.length; for ( j = 0; j < all_columns_len; j++ ) { var fromAllColumn = all_columns[j]; if ( fromAllColumn[key] == select_item[key] ) { //saved search select items may don't have ids if it's saved from flex, so set it back if ( !select_item.hasOwnProperty( 'id' ) && fromAllColumn.hasOwnProperty( 'id' ) ) { select_item.id = fromAllColumn.id; } if ( Global.isSet( tmp_select_items ) ) { tmp_select_items.push( fromAllColumn ); } if ( !tree_mode ) { all_columns.splice( j, 1 ); } break; } } } } else { all_columns_len = all_columns.length; for ( j = 0; j < all_columns_len; j++ ) { fromAllColumn = all_columns[j]; for ( i = 0; i < selectItemLen; i++ ) { select_item = val[i]; if ( !Global.isSet( select_item[key] ) ) { select_item = []; select_item[key] = val[i]; if ( !Global.isSet( tmp_select_items ) ) { tmp_select_items = []; } } // we have both string case and number case. sometimes number will be 'xx'. So use == make sure all match if ( fromAllColumn[key] == select_item[key] ) { //saved search select items may don't have ids if it's saved from flex, so set it back if ( !select_item.hasOwnProperty( 'id' ) && fromAllColumn.hasOwnProperty( 'id' ) ) { select_item.id = fromAllColumn.id; } if ( Global.isSet( tmp_select_items ) ) { tmp_select_items.push( fromAllColumn ); } if ( !tree_mode ) { all_columns.splice( j, 1 ); all_columns_len = all_columns_len - 1; j = j - 1; } break; } } } } } // for all static options, that don't need get reald data, the length should always be match, use temp array because val don't //contains full info. if ( tmp_select_items && tmp_select_items.length === val.length || ( val.length > 0 && !val[0].hasOwnProperty( key ) ) ) { val = tmp_select_items; } // val = ( Global.isSet( tmp_select_items ) ) ? tmp_select_items : val; //don't refresh select grid if it's calling from onDropDownsearch whcih doing search in search input if ( !searchResult ) { //select_grid.clearGridData(); //FIXES BUG #1998: The api call returns true when the data it's looking for is deleted. This causes the grid to add a blank row to the unselected side when clear is clicked. if ( typeof val === 'object' ) { select_grid.setData( val ); } } if ( !tree_mode ) { unselect_grid.setData( all_columns ); this.setTotalDisplaySpan(); } else { a_dropdown_this.reSetUnSelectGridTreeData( all_columns ); } a_dropdown_this.setSelectGridDragAble(); a_dropdown_this.setUnSelectGridDragAble(); }; this.setUnSelectGridHighlight = function( array ) { unselect_grid.resetSelection(); $.each( array, function( index, content ) { unselect_grid.grid.jqGrid( 'setSelection', content, false ); } ); }; this.showNoResultCover = function( target_grid ) { this.removeNoResultCover( target_grid ); var no_result_box = Global.loadWidgetByName( WidgetNamesDic.NO_RESULT_BOX ); no_result_box.NoResultBox( { related_view_controller: this } ); var grid_div; if ( target_grid === 'unselect_grid' ) { no_result_box.attr( 'id', id + target_grid + '_no_result_box' ); grid_div = $( this ).find( '#gbox_unselect_grid_' + id ); unselect_grid_no_result_box = no_result_box; } else { no_result_box.attr( 'id', id + target_grid + '_no_result_box' ); grid_div = $( this ).find( '#gbox_select_grid_' + id ); select_grid_no_result_box = no_result_box; } grid_div.append( no_result_box ); }; this.removeNoResultCover = function( target_grid ) { if ( target_grid === 'unselect_grid' ) { if ( unselect_grid_no_result_box ) { unselect_grid_no_result_box.remove(); unselect_grid_no_result_box = null; } } else { if ( select_grid_no_result_box ) { select_grid_no_result_box.remove(); select_grid_no_result_box = null; } } }; this.setSelectGridHighlight = function( array ) { select_grid.resetSelection(); $.each( array, function( index, content ) { select_grid.grid.jqGrid( 'setSelection', content, false ); } ); }; this.setUnSelectGridDragAble = function() { var highlight_Rows = null; var trs = unselect_grid.grid.find( 'tr.ui-widget-content' ).attr( 'draggable', 'true' ); trs.unbind( 'dragstart' ).bind( 'dragstart', function( event ) { var target = $( event.target ); var container = $( '