<commit_before>/*
 * Copyright (c) 2014
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

/* global OC, t, n, FileList, FileActions, Files, FileSummary, BreadCrumb */
/* global dragOptions, folderDropOptions */
window.FileList = {
	appName: t('files', 'Files'),
	isEmpty: true,
	useUndo:true,
	$el: $('#filestable'),
	$fileList: $('#fileList'),
	breadcrumb: null,

	/**
	 * Instance of FileSummary
	 */
	fileSummary: null,
	initialized: false,

	// number of files per page
	pageSize: 20,

	/**
	 * Array of files in the current folder.
	 * The entries are of file data.
	 */
	files: [],

	/**
	 * Map of file id to file data
	 */
	_selectedFiles: {},

	/**
	 * Summary of selected files.
	 * Instance of FileSummary.
	 */
	_selectionSummary: null,

	/**
	 * Compare two file info objects, sorting by
	 * folders first, then by name.
	 */
	_fileInfoCompare: function(fileInfo1, fileInfo2) {
		if (fileInfo1.type === 'dir' && fileInfo2.type !== 'dir') {
			return -1;
		}
		if (fileInfo1.type !== 'dir' && fileInfo2.type === 'dir') {
			return 1;
		}
		return fileInfo1.name.localeCompare(fileInfo2.name);
	},

	/**
	 * Initialize the file list and its components
	 */
	initialize: function() {
		var self = this;
		if (this.initialized) {
			return;
		}

		// TODO: FileList should not know about global elements
		this.$el = $('#filestable');
		this.$fileList = $('#fileList');
		this.files = [];
		this._selectedFiles = {};
		this._selectionSummary = new FileSummary();

		this.fileSummary = this._createSummary();

		this.breadcrumb = new BreadCrumb({
			onClick: this._onClickBreadCrumb,
			onDrop: _.bind(this._onDropOnBreadCrumb, this),
			getCrumbUrl: function(part, index) {
				return self.linkTo(part.dir);
			}
		});

		$('#controls').prepend(this.breadcrumb.$el);

		$(window).resize(function() {
			// TODO: debounce this ?
			var width = $(this).width();
			FileList.breadcrumb.resize(width, false);
		});

		this.$fileList.on('click','td.filename a', _.bind(this._onClickFile, this));
		this.$fileList.on('change', 'td.filename input:checkbox', _.bind(this._onClickFileCheckbox, this));
		this.$el.find('#select_all').click(_.bind(this._onClickSelectAll, this));
		this.$el.find('.download').click(_.bind(this._onClickDownloadSelected, this));
		this.$el.find('.delete-selected').click(_.bind(this._onClickDeleteSelected, this));
	},

	/**
	 * Selected/deselects the given file element and updated
	 * the internal selection cache.
	 *
	 * @param $tr single file row element
	 * @param state true to select, false to deselect
	 */
	_selectFileEl: function($tr, state) {
		var $checkbox = $tr.find('input:checkbox');
		var oldData = !!this._selectedFiles[$tr.data('id')];
		var data;
		$checkbox.prop('checked', state);
		$tr.toggleClass('selected', state);
		// already selected ?
		if (state === oldData) {
			return;
		}
		data = this.elementToFile($tr);
		if (state) {
			this._selectedFiles[$tr.data('id')] = data;
			this._selectionSummary.add(data);
		}
		else {
			delete this._selectedFiles[$tr.data('id')];
			this._selectionSummary.remove(data);
		}
		this.$el.find('#select_all').prop('checked', this._selectionSummary.getTotal() === this.files.length);
	},

	/**
	 * Event handler for when clicking on files to select them
	 */
	_onClickFile: function(event) {
		var $tr = $(event.target).closest('tr');
		if (event.ctrlKey || event.shiftKey) {
			event.preventDefault();
			if (event.shiftKey) {
				var $lastTr = $(this._lastChecked);
				var lastIndex = $lastTr.index();
				var currentIndex = $tr.index();
				var $rows = this.$fileList.children('tr');

				// last clicked checkbox below current one ?
				if (lastIndex > currentIndex) {
					var aux = lastIndex;
					lastIndex = currentIndex;
					currentIndex = aux;
				}

				// auto-select everything in-between
				for (var i = lastIndex + 1; i < currentIndex; i++) {
					this._selectFileEl($rows.eq(i), true);
				}
			}
			else {
				this._lastChecked = $tr;
			}
			var $checkbox = $tr.find('input:checkbox');
			this._selectFileEl($tr, !$checkbox.prop('checked'));
			this.updateSelectionSummary();
		} else {
			var filename = $tr.attr('data-file');
			var renaming = $tr.data('renaming');
			if (!renaming) {
				FileActions.currentFile = $tr.find('td');
				var mime=FileActions.getCurrentMimeType();
				var type=FileActions.getCurrentType();
				var permissions = FileActions.getCurrentPermissions();
				var action=FileActions.getDefault(mime,type, permissions);
				if (action) {
					event.preventDefault();
					action(filename);
				}
			}
		}
	},

	/**
	 * Event handler for when clicking on a file's checkbox
	 */
	_onClickFileCheckbox: function(e) {
		var $tr = $(e.target).closest('tr');
		this._selectFileEl($tr, !$tr.hasClass('selected'));
		this._lastChecked = $tr;
		this.updateSelectionSummary();
	},

	/**
	 * Event handler for when selecting/deselecting all files
	 */
	_onClickSelectAll: function(e) {
		var checked = $(e.target).prop('checked');
		this.$fileList.find('td.filename input:checkbox').prop('checked', checked)
			.closest('tr').toggleClass('selected', checked);
		this._selectedFiles = {};
		this._selectionSummary.clear();
		if (checked) {
			for (var i = 0; i < this.files.length; i++) {
				var fileData = this.files[i];
				this._selectedFiles[fileData.id] = fileData;
				this._selectionSummary.add(fileData);
			}
		}
		this.updateSelectionSummary();<commit_msg>Fixed too generic checkbox and link selectors<commit_after>/*
 * Copyright (c) 2014
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

/* global OC, t, n, FileList, FileActions, Files, FileSummary, BreadCrumb */
/* global dragOptions, folderDropOptions */
window.FileList = {
	appName: t('files', 'Files'),
	isEmpty: true,
	useUndo:true,
	$el: $('#filestable'),
	$fileList: $('#fileList'),
	breadcrumb: null,

	/**
	 * Instance of FileSummary
	 */
	fileSummary: null,
	initialized: false,

	// number of files per page
	pageSize: 20,

	/**
	 * Array of files in the current folder.
	 * The entries are of file data.
	 */
	files: [],

	/**
	 * Map of file id to file data
	 */
	_selectedFiles: {},

	/**
	 * Summary of selected files.
	 * Instance of FileSummary.
	 */
	_selectionSummary: null,

	/**
	 * Compare two file info objects, sorting by
	 * folders first, then by name.
	 */
	_fileInfoCompare: function(fileInfo1, fileInfo2) {
		if (fileInfo1.type === 'dir' && fileInfo2.type !== 'dir') {
			return -1;
		}
		if (fileInfo1.type !== 'dir' && fileInfo2.type === 'dir') {
			return 1;
		}
		return fileInfo1.name.localeCompare(fileInfo2.name);
	},

	/**
	 * Initialize the file list and its components
	 */
	initialize: function() {
		var self = this;
		if (this.initialized) {
			return;
		}

		// TODO: FileList should not know about global elements
		this.$el = $('#filestable');
		this.$fileList = $('#fileList');
		this.files = [];
		this._selectedFiles = {};
		this._selectionSummary = new FileSummary();

		this.fileSummary = this._createSummary();

		this.breadcrumb = new BreadCrumb({
			onClick: this._onClickBreadCrumb,
			onDrop: _.bind(this._onDropOnBreadCrumb, this),
			getCrumbUrl: function(part, index) {
				return self.linkTo(part.dir);
			}
		});

		$('#controls').prepend(this.breadcrumb.$el);

		$(window).resize(function() {
			// TODO: debounce this ?
			var width = $(this).width();
			FileList.breadcrumb.resize(width, false);
		});

		this.$fileList.on('click','td.filename>a.name', _.bind(this._onClickFile, this));
		this.$fileList.on('change', 'td.filename>input:checkbox', _.bind(this._onClickFileCheckbox, this));
		this.$el.find('#select_all').click(_.bind(this._onClickSelectAll, this));
		this.$el.find('.download').click(_.bind(this._onClickDownloadSelected, this));
		this.$el.find('.delete-selected').click(_.bind(this._onClickDeleteSelected, this));
	},

	/**
	 * Selected/deselects the given file element and updated
	 * the internal selection cache.
	 *
	 * @param $tr single file row element
	 * @param state true to select, false to deselect
	 */
	_selectFileEl: function($tr, state) {
		var $checkbox = $tr.find('td.filename>input:checkbox');
		var oldData = !!this._selectedFiles[$tr.data('id')];
		var data;
		$checkbox.prop('checked', state);
		$tr.toggleClass('selected', state);
		// already selected ?
		if (state === oldData) {
			return;
		}
		data = this.elementToFile($tr);
		if (state) {
			this._selectedFiles[$tr.data('id')] = data;
			this._selectionSummary.add(data);
		}
		else {
			delete this._selectedFiles[$tr.data('id')];
			this._selectionSummary.remove(data);
		}
		this.$el.find('#select_all').prop('checked', this._selectionSummary.getTotal() === this.files.length);
	},

	/**
	 * Event handler for when clicking on files to select them
	 */
	_onClickFile: function(event) {
		var $tr = $(event.target).closest('tr');
		if (event.ctrlKey || event.shiftKey) {
			event.preventDefault();
			if (event.shiftKey) {
				var $lastTr = $(this._lastChecked);
				var lastIndex = $lastTr.index();
				var currentIndex = $tr.index();
				var $rows = this.$fileList.children('tr');

				// last clicked checkbox below current one ?
				if (lastIndex > currentIndex) {
					var aux = lastIndex;
					lastIndex = currentIndex;
					currentIndex = aux;
				}

				// auto-select everything in-between
				for (var i = lastIndex + 1; i < currentIndex; i++) {
					this._selectFileEl($rows.eq(i), true);
				}
			}
			else {
				this._lastChecked = $tr;
			}
			var $checkbox = $tr.find('td.filename>input:checkbox');
			this._selectFileEl($tr, !$checkbox.prop('checked'));
			this.updateSelectionSummary();
		} else {
			var filename = $tr.attr('data-file');
			var renaming = $tr.data('renaming');
			if (!renaming) {
				FileActions.currentFile = $tr.find('td');
				var mime=FileActions.getCurrentMimeType();
				var type=FileActions.getCurrentType();
				var permissions = FileActions.getCurrentPermissions();
				var action=FileActions.getDefault(mime,type, permissions);
				if (action) {
					event.preventDefault();
					action(filename);
				}
			}
		}
	},

	/**
	 * Event handler for when clicking on a file's checkbox
	 */
	_onClickFileCheckbox: function(e) {
		var $tr = $(e.target).closest('tr');
		this._selectFileEl($tr, !$tr.hasClass('selected'));
		this._lastChecked = $tr;
		this.updateSelectionSummary();
	},

	/**
	 * Event handler for when selecting/deselecting all files
	 */
	_onClickSelectAll: function(e) {
		var checked = $(e.target).prop('checked');
		this.$fileList.find('td.filename>input:checkbox').prop('checked', checked)
			.closest('tr').toggleClass('selected', checked);
		this._selectedFiles = {};
		this._selectionSummary.clear();
		if (checked) {
			for (var i = 0; i < this.files.length; i++) {
				var fileData = this.files[i];
				this._selectedFiles[fileData.id] = fileData;
				this._selectionSummary.add(fileData);
			}
		}
		this.updateSelectionSummary();
{'fraction_non_alphanumeric': 0.16290249433106577, 'fraction_numerical': 0.0034467120181405895, 'mean_word_length': 3.6582171525137306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6506735', 'n_tokens_mistral': 4060, 'n_tokens_neox': 3540, 'n_words': 812}