<commit_before>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
({
	CONSTANTS : {ASC: 'ASC', DESC: 'DESC', DESC_PREFIX: "-", CONTAINER_ELEMENT_ID: 'listSorter'},
		
	doInit : function(cmp) {
		this.initSorterTrigger(cmp);
		this.initDataProvider(cmp);
		this.initItems(cmp, cmp.get('v.items'), cmp.get('v.defaultSelectedItems'));
	},
	
	initSorterTrigger : function(cmp) {		
		var trigger = cmp.get('v.trigger'),
			triggerLabel = cmp.get('v.triggerLabel');
		
		if (trigger && trigger.length > 0) {
			if (triggerLabel) {
				trigger[0].set("v.label", triggerLabel);
			}
			trigger[0].addHandler('click', cmp, 'c.onOpen');

		} else if (triggerLabel) {
			
			var menuTrigger = $A.createComponentFromConfig({
	            componentDef : { descriptor : "markup://ui:menuTriggerLink" },
	            attributes : { values : { label: triggerLabel } } 
	        });

			menuTrigger.addHandler('click', cmp, 'c.onOpen');
			cmp.set('v.trigger', menuTrigger);
		}
	},
	
	initDataProvider: function(cmp) {
        var dataProviders = cmp.get("v.dataProvider");
        
        if ($A.util.isArray(dataProviders)) {
        	cmp._dataProviders = dataProviders;
            for (var i = 0; i < dataProviders.length; i++) {
            	//add handler
                dataProviders[i].addHandler("onchange", cmp, "c.handleDataChange");
                dataProviders[i].addHandler("error", cmp, "c.handleDataError");
            }
        }
    },
    
    initItems : function(cmp, items, selectedItems) {
		if (!cmp._sortOrderMap) {
			cmp._sortOrderMap = {};
		}
	    
		if (items && items.length > 0) {
			//init sortOrderMap for default selected items
			var filteredItems = [], sList = [], fieldName, label;
            var i;
			if (selectedItems) {
				for (i=0; i< selectedItems.length; i++) {
					cmp._sortOrderMap[selectedItems[i].fieldName] = {order: selectedItems[i].ascending ? this.CONSTANTS.ASC : this.CONSTANTS.DESC, selected: true};				
				}
			}
			var indx = 0;
			for (i=0; i<items.length; i++) {
				if (typeof items[i].isSortable === 'undefined' || items[i].isSortable === true) {
					fieldName = items[i].fieldName;
					label = items[i].label;
					
					if (!cmp._sortOrderMap[fieldName]) {
						//default to ASC order
						cmp._sortOrderMap[fieldName] = {order: this.CONSTANTS.ASC, index: indx, selected: false}; 
					} else {
						cmp._sortOrderMap[fieldName].index = indx;
						//add to selected list
						sList.push({value: fieldName, label: label, index: indx});
					}
					filteredItems.push({fieldName : fieldName, label: label});
					indx++;
				}
			}
			
			cmp._selectedItems = sList;
			cmp.set('v.defaultSelectedItems', selectedItems);
			cmp.set('v.items', filteredItems);
			//show apply button only when item is not empty
			$A.util.removeClass(cmp.find("set").getElement(), "hidden", "visible");
		}
	},

	
	handleOnOpen : function(cmp, force) {
		var items = cmp.get('v.items');	
		if ((cmp.get('v.visible') || !cmp.isRendered()) && !force) {
			return;
		}
		this.attachEventHandler(cmp);		
		var selected = this.getDefaultSortBy(cmp);
		if (selected && selected.length > 0 && items && items.length > 0) {
			//update selected item sort orders
			for (var i=0; i< selected.length; i++) {
				if (cmp._sortOrderMap[selected[i].fieldName]) {
					cmp._sortOrderMap[selected[i].fieldName].order = selected[i].ascending ? this.CONSTANTS.ASC : this.CONSTANTS.DESC;
				}
			}
			//reset selectedItems to default
			this.setSelectedItems(cmp, selected);
			//select menu item
			this.selectMenuItem(cmp, selected);
		}
		cmp.set('v.visible', true);
		this.appendElementToBody(cmp);
		this.updateSize(cmp);
	},
	
	handleOnCancel : function(cmp) {
		this.removeEventHandler(cmp);
		this.reset(cmp);
		cmp.set('v.visible', false, true);
		this.setVisible(cmp, false);
		
		var action = cmp.get('v.onCancel');
        if (action) {<commit_msg>[W-4478087] prevent listSorter errors when it is not rendered and dom is accessed (#2130)<commit_after>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
({
	CONSTANTS : {ASC: 'ASC', DESC: 'DESC', DESC_PREFIX: "-", CONTAINER_ELEMENT_ID: 'listSorter'},
		
	doInit : function(cmp) {
		this.initSorterTrigger(cmp);
		this.initDataProvider(cmp);
		this.initItems(cmp, cmp.get('v.items'), cmp.get('v.defaultSelectedItems'));
	},
	
	initSorterTrigger : function(cmp) {		
		var trigger = cmp.get('v.trigger'),
			triggerLabel = cmp.get('v.triggerLabel');
		
		if (trigger && trigger.length > 0) {
			if (triggerLabel) {
				trigger[0].set("v.label", triggerLabel);
			}
			trigger[0].addHandler('click', cmp, 'c.onOpen');

		} else if (triggerLabel) {
			
			var menuTrigger = $A.createComponentFromConfig({
	            componentDef : { descriptor : "markup://ui:menuTriggerLink" },
	            attributes : { values : { label: triggerLabel } } 
	        });

			menuTrigger.addHandler('click', cmp, 'c.onOpen');
			cmp.set('v.trigger', menuTrigger);
		}
	},
	
	initDataProvider: function(cmp) {
        var dataProviders = cmp.get("v.dataProvider");
        
        if ($A.util.isArray(dataProviders)) {
        	cmp._dataProviders = dataProviders;
            for (var i = 0; i < dataProviders.length; i++) {
            	//add handler
                dataProviders[i].addHandler("onchange", cmp, "c.handleDataChange");
                dataProviders[i].addHandler("error", cmp, "c.handleDataError");
            }
        }
    },
    
    initItems : function(cmp, items, selectedItems) {
		if (!cmp._sortOrderMap) {
			cmp._sortOrderMap = {};
		}
	    
		if (items && items.length > 0) {
			//init sortOrderMap for default selected items
			var filteredItems = [], sList = [], fieldName, label;
            var i;
			if (selectedItems) {
				for (i=0; i< selectedItems.length; i++) {
					cmp._sortOrderMap[selectedItems[i].fieldName] = {order: selectedItems[i].ascending ? this.CONSTANTS.ASC : this.CONSTANTS.DESC, selected: true};				
				}
			}
			var indx = 0;
			for (i=0; i<items.length; i++) {
				if (typeof items[i].isSortable === 'undefined' || items[i].isSortable === true) {
					fieldName = items[i].fieldName;
					label = items[i].label;
					
					if (!cmp._sortOrderMap[fieldName]) {
						//default to ASC order
						cmp._sortOrderMap[fieldName] = {order: this.CONSTANTS.ASC, index: indx, selected: false}; 
					} else {
						cmp._sortOrderMap[fieldName].index = indx;
						//add to selected list
						sList.push({value: fieldName, label: label, index: indx});
					}
					filteredItems.push({fieldName : fieldName, label: label});
					indx++;
				}
			}
			
			cmp._selectedItems = sList;
			cmp.set('v.defaultSelectedItems', selectedItems);
			cmp.set('v.items', filteredItems);
			//show apply button only when item is not empty
			$A.util.removeClass(cmp.find("set").getElement(), "hidden", "visible");
		}
	},

	
	handleOnOpen : function(cmp, force) {
		var items = cmp.get('v.items');
		//changed to OR !force to make sure component is rendered before doing
		//dom calculations, in this component force is always true
		if (cmp.get('v.visible') || !cmp.isRendered() || !force) {
			return;
		}
		this.attachEventHandler(cmp);		
		var selected = this.getDefaultSortBy(cmp);
		if (selected && selected.length > 0 && items && items.length > 0) {
			//update selected item sort orders
			for (var i=0; i< selected.length; i++) {
				if (cmp._sortOrderMap[selected[i].fieldName]) {
					cmp._sortOrderMap[selected[i].fieldName].order = selected[i].ascending ? this.CONSTANTS.ASC : this.CONSTANTS.DESC;
				}
			}
			//reset selectedItems to default
			this.setSelectedItems(cmp, selected);
			//select menu item
			this.selectMenuItem(cmp, selected);
		}
		cmp.set('v.visible', true);
		this.appendElementToBody(cmp);
		this.updateSize(cmp);
	},
	
	handleOnCancel : function(cmp) {
		this.removeEventHandler(cmp);
		this.reset(cmp);
		cmp.set('v.visible', false, true);
		this.setVisible(cmp, false);
		
		var action = cmp.get('v.onCancel');
        if (action) {
{'fraction_non_alphanumeric': 0.1462663587374904, 'fraction_numerical': 0.005388760585065435, 'mean_word_length': 3.1093538183461367, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4442323', 'n_tokens_mistral': 3308, 'n_tokens_neox': 2868, 'n_words': 696}