<commit_before>// --------------------------------------------------
// Editable list view
// --------------------------------------------------
define(function (require) {
	
	// Dependencies
	var $ = require('jquery'),
		_ = require('underscore'),
		Backbone = require('backbone');
		
	// Bring in just enough jQuery UI for drag and drop
	require('decoy/plugins/jquery-ui');
	require('jquery-ui-touch-punch-amd');
	
	// Bring in the template for new rows.  Currently, the only need to do this
	// is for many-to-many row insertion
	var row_template = _.template(require('text!decoy/templates/standard-list-row.html'));
	
	// Static vars
	var app,
		dataId = 'data-model-id',
		visibleIconClass = 'glyphicon-eye-open',
		hiddenIconClass = 'glyphicon-eye-close';

	// View
	var StandardList = Backbone.View.extend({
		
		initialize: function (options) {
			_.bindAll(this);
			app = options.app;

			// Get the path to the controller.  If this is not specified via a
			// data attribtue of "controller-route" then we attempt to infer it from
			// the current URL.
			this.controllerRoute = this.$el.data('controller-route');
			if (!this.controllerRoute) {
				this.controllerRoute = window.location.pathname;
			}
			
			// Cache
			this.$deleteBtn = this.$('.delete-selected');
			this.$deleteAlert = this.$('.delete-alert');
			this.$bulkActions = this.$('.bulk-actions');
			this.$total = this.$('legend .badge, h1 .badge');
			this.$trs = this.$el.find('[' + dataId + ']');
			this.parent_controller = this.$el.data('parent-controller');
			this.position_offset = this.$el.data('position-offset');
			this.$no_results = this.$('.no-results');
			
			// Create model collection from table rows.  The URL is fetched from
			// the controller-route data attribute of the container.
			this.rows = [];
			this.collection = new Backbone.Collection();
			this.collection.url = this.controllerRoute;
			_.each(this.$trs, this.initRow);
			
			// listen for collection changes and render view
			this.collection.on('change', this.render, this);
			this.collection.on('change:featured', this.updateFeatured, this);
			this.collection.on('remove', this.onRemove);
			
			// Add drag and drop if there is position data on the first
			// row.  It expects the current position of the a row to be stored
			// as a data value on the row
			if (this.$trs.first().data('position') !== undefined) {
				this.$sortContainer = this.initSortable();
			}
		},
		
		// Init an individual row in the list
		initRow: function (row) {
			
			// Find vars
			var $row = $(row),
				modelId = $row.attr(dataId),
				$inputs = $row.find('input'),
				featured = $inputs.filter('[name=set-featured]').prop('checked'),
				$visibility = $row.find('.visibility'),
				visible_state = $visibility.find('.'+visibleIconClass).length > 0,
				position = $row.data('position'),
				parent_id = $row.data('parent-id');
			
			// Define the model data
			var data = {
				id: modelId,
				selected: false,
				featured: featured,
				position: position,
				parent_id: parent_id
			};
			
			// If this item supports visibility, add it to the model
			if ($visibility.length) {
				data.visible = visible_state;
			}
			
			// If there was a parent_controller, add it to the model so sync
			// requests can make use of it
			if (this.parent_controller) {
				data.parent_controller = this.parent_controller;
			}
			
			// Create the model and store a reference
			var model = new Backbone.Model(data);
			model.whitelist = ['position']; // Only sync position
			if ($visibility.length) model.whitelist.push('visible');
			if (this.parent_controller) model.whitelist.push('parent_controller');
			if (parent_id) model.whitelist.push('parent_id');

			// Add the model to the collection
			this.collection.push(model);
			$inputs.data('model', model);
			this.rows.push($row);

			// reset checkboxes on reload (for Firefox)
			$inputs.filter('[name=select-row]').prop('checked', false);
		},
		
		// Turn on sortability
		initSortable: function() {
			
			// Cache some selectors
			var $sortable = this.$el.find('tbody');
			
			// Tell the server of the new sorting rules by looping through
			// all rows, looking up the model for the id, and then updating
			// it's position attribute.
			var update = _.bind(function(event, ui) {
				var id,
					$sortableRows = $sortable.find('[' + dataId + ']');
				_.each($sortableRows, function(el, i) {
					id = $(el).attr(dataId);
					this.collection.get(id).set({position: (i + this.position_offset) });
				}, this);
			}, this);
			
			// Define options
			var options = {
				tolerance: 'pointer',
				revert: 100,
				containment: $sortable,

				// Prevent no-results from being sortable
				items: 'tr:not(.no-results, .bulk-actions)',

				// Create the placeholder with the right column span
				// http://stackoverflow.com/a/8707306
				placeholder: 'placeholder',
				forcePlaceholderSize: true,
				start: function(event, ui) {
					ui.placeholder.html("<td colspan='999'></td>");
					
					// For some reason, this always gets created 1px to tall.
					ui.placeholder.height(ui.helper.height()-1);

				},

				// Preserve the widths of columns during dragging by freezing them
				// in place
				// From http://cl.ly/170d0h291V10
				helper: function(e, tr) {
					tr.children().each(function(index) {
						$(this).width($(this).width());
					});
					return tr;
				},<commit_msg>No longer need to massage the height by 1px<commit_after>// --------------------------------------------------
// Editable list view
// --------------------------------------------------
define(function (require) {
	
	// Dependencies
	var $ = require('jquery'),
		_ = require('underscore'),
		Backbone = require('backbone');
		
	// Bring in just enough jQuery UI for drag and drop
	require('decoy/plugins/jquery-ui');
	require('jquery-ui-touch-punch-amd');
	
	// Bring in the template for new rows.  Currently, the only need to do this
	// is for many-to-many row insertion
	var row_template = _.template(require('text!decoy/templates/standard-list-row.html'));
	
	// Static vars
	var app,
		dataId = 'data-model-id',
		visibleIconClass = 'glyphicon-eye-open',
		hiddenIconClass = 'glyphicon-eye-close';

	// View
	var StandardList = Backbone.View.extend({
		
		initialize: function (options) {
			_.bindAll(this);
			app = options.app;

			// Get the path to the controller.  If this is not specified via a
			// data attribtue of "controller-route" then we attempt to infer it from
			// the current URL.
			this.controllerRoute = this.$el.data('controller-route');
			if (!this.controllerRoute) {
				this.controllerRoute = window.location.pathname;
			}
			
			// Cache
			this.$deleteBtn = this.$('.delete-selected');
			this.$deleteAlert = this.$('.delete-alert');
			this.$bulkActions = this.$('.bulk-actions');
			this.$total = this.$('legend .badge, h1 .badge');
			this.$trs = this.$el.find('[' + dataId + ']');
			this.parent_controller = this.$el.data('parent-controller');
			this.position_offset = this.$el.data('position-offset');
			this.$no_results = this.$('.no-results');
			
			// Create model collection from table rows.  The URL is fetched from
			// the controller-route data attribute of the container.
			this.rows = [];
			this.collection = new Backbone.Collection();
			this.collection.url = this.controllerRoute;
			_.each(this.$trs, this.initRow);
			
			// listen for collection changes and render view
			this.collection.on('change', this.render, this);
			this.collection.on('change:featured', this.updateFeatured, this);
			this.collection.on('remove', this.onRemove);
			
			// Add drag and drop if there is position data on the first
			// row.  It expects the current position of the a row to be stored
			// as a data value on the row
			if (this.$trs.first().data('position') !== undefined) {
				this.$sortContainer = this.initSortable();
			}
		},
		
		// Init an individual row in the list
		initRow: function (row) {
			
			// Find vars
			var $row = $(row),
				modelId = $row.attr(dataId),
				$inputs = $row.find('input'),
				featured = $inputs.filter('[name=set-featured]').prop('checked'),
				$visibility = $row.find('.visibility'),
				visible_state = $visibility.find('.'+visibleIconClass).length > 0,
				position = $row.data('position'),
				parent_id = $row.data('parent-id');
			
			// Define the model data
			var data = {
				id: modelId,
				selected: false,
				featured: featured,
				position: position,
				parent_id: parent_id
			};
			
			// If this item supports visibility, add it to the model
			if ($visibility.length) {
				data.visible = visible_state;
			}
			
			// If there was a parent_controller, add it to the model so sync
			// requests can make use of it
			if (this.parent_controller) {
				data.parent_controller = this.parent_controller;
			}
			
			// Create the model and store a reference
			var model = new Backbone.Model(data);
			model.whitelist = ['position']; // Only sync position
			if ($visibility.length) model.whitelist.push('visible');
			if (this.parent_controller) model.whitelist.push('parent_controller');
			if (parent_id) model.whitelist.push('parent_id');

			// Add the model to the collection
			this.collection.push(model);
			$inputs.data('model', model);
			this.rows.push($row);

			// reset checkboxes on reload (for Firefox)
			$inputs.filter('[name=select-row]').prop('checked', false);
		},
		
		// Turn on sortability
		initSortable: function() {
			
			// Cache some selectors
			var $sortable = this.$el.find('tbody');
			
			// Tell the server of the new sorting rules by looping through
			// all rows, looking up the model for the id, and then updating
			// it's position attribute.
			var update = _.bind(function(event, ui) {
				var id,
					$sortableRows = $sortable.find('[' + dataId + ']');
				_.each($sortableRows, function(el, i) {
					id = $(el).attr(dataId);
					this.collection.get(id).set({position: (i + this.position_offset) });
				}, this);
			}, this);
			
			// Define options
			var options = {
				tolerance: 'pointer',
				revert: 100,
				containment: $sortable,

				// Prevent no-results from being sortable
				items: 'tr:not(.no-results, .bulk-actions)',

				// Create the placeholder with the right column span
				// http://stackoverflow.com/a/8707306
				placeholder: 'placeholder',
				forcePlaceholderSize: true,
				start: function(event, ui) {
					ui.placeholder
						.html("<td colspan='999'></td>")
						.placeholder.height(ui.helper.height())
					;
				},

				// Preserve the widths of columns during dragging by freezing them
				// in place
				// From http://cl.ly/170d0h291V10
				helper: function(e, $tr) {
					$tr.children().each(function(index) {
						$(this).width($(this).width());
					});
					return $tr;
				},
{'fraction_non_alphanumeric': 0.16163952627683198, 'fraction_numerical': 0.0047187268689859365, 'mean_word_length': 2.7873160476524177, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5373932', 'n_tokens_mistral': 4004, 'n_tokens_neox': 3214, 'n_words': 893}