<commit_before>define('tasks_view', ['app'], function (app) {

	var TaskModel = Backbone.Model.extend({
			defaults: {
				'selected': false
			},
			url: function () {
				var id = this.has('id') ? this.get('id') : '';
				return '/task/' + id;
			},
			initialize: function () {
				this.bind('remove', function () {
					this.destroy();
				}, this);
			}
		}),

		TasksCollection = Backbone.Collection.extend({
			model: TaskModel,
			url: function () {
				return 'tasks/';
			}
		}),

		TaskView = Backbone.View.extend({
			tagName: 'li',
			template: '' +
				'<p><input type="checkbox" ' +
				'<% if (selected) { %> checked <% } %>' +
				' name="task" value="<%= id %>" /></p>' +
				'<p><%= title %></p>',
			formTemplate: '' +
				'<p><input type="checkbox" checked name="task" value="<%= id %>" /></p>' +
				'<p><input type="text" name="task_title_<%= id %>" value="<%= title %>" /></p>',

			events: { 'click :checkbox': 'selectTask' },

			initialize: function () {
				_.bindAll(this, 'selectTask');
			},

			render: function (action) {
				if (action === 'form') {
					this.$el.html(_.template(this.formTemplate, this.model.toJSON()));

				} else if (action === 'class') {
					this.className = this.model.get('new_status') || this.model.get('status');
					this.$el.attr('class', this.className);
				}

				if (action !== 'form') {
					this.$el.html(_.template(this.template, this.model.toJSON()));
				}

				this.$el.attr('task_id', this.model.get('id'));
				return this;
			},

			selectTask: function (e) {
				e.preventDefault();

				// Update selected attribute of a model. That will trigger 'change:selected' event
				// which was binded with 'selectItems' function on the main view.
				this.model.set('selected', !this.model.get('selected'));
			}
		});


	return Backbone.View.extend({
		noTaskTemplate: '<li class="no-task">There is not task to do! YaY! </li>',

		initialize: function (options) {
			var self = this;
			this.parent = options.parent;
			this.broker = options.broker;

			_.bindAll(this, 'selectItems', '_tasksFetched');

			// Define a collection where will be stored a view for each task.
			this._viewsCollection = new Backbone.Collection();

			// Define a collection where will be stored the list of tasks.
			this.tasksCollection = new TasksCollection();

			// Getting the list! Make an initial request to the backend
			// to store all task in the collection as models.
			this.tasksCollection.fetch({
				success: function () {
					self._tasksFetched();
				}
			});

			// Binding events trigger by the user.
			this.tasksCollection.bind('change:selected', this.selectItems);

			// Binding public events to our general Backbone.Event object.
			// Those will be triggered by nav_view.
			this.broker.on('task:create', this.addTask, this);
			this.broker.on('tasks:edit:view', this.editTasks, this);
			this.broker.on('tasks:edit:save', this.saveTasks, this);
			this.broker.on('tasks:status:change', this.changeStatusTasks, this);
			this.broker.on('tasks:remove', this.removeTasks, this);
		},

		_tasksFetched: function () {

			// If there's no tasks in the database we need to display any message
			// telling it. If it gets some tasks we need to create and store a view for each one.

			if (this.$el.children().length !== this.tasksCollection.length &&
				!this.$el.find('.no-task').length) {
				this.render();

			} else {
				this.tasksCollection.each(function (model) {
					// Binding events. Need to create the views.
					this._viewsCollection.add({
						'id': model.get('id'),
						'view': this._getTaskView(model)
					});
				}, this);
			}
		},

		render: function () {
			var self = this;

			// Rendering the entire list of tasks
			// Or a message if there's no tasks.

			this.$el.children().remove();
			this._viewsCollection.reset([]);

			this.tasksCollection.each(function (model) {

				var view = new TaskView({
					model: model,
					className: 'backbone tag ' + model.get('status')
				});

				self._viewsCollection.add({
					'id': model.get('id'),
					'view': view
				});

				this.$el.append(view.render().el);
			}, this);

			if (!this.tasksCollection.length) {
				this.$el.append(this.noTaskTemplate);
			}

			return this;
		},

		selectItems: function (model) {
			var self = this, view = this._viewsCollection.findWhere({'id': model.get('id')}).get('view');

			// If an item has been checked and it's the first one: trigger an event to enable editRemove bar mode.
			// Else, if an item has been unchecked and it was the last element checked: trigger an event to disable
			// editRemove mode and enable addTask instead.

			if (model.get('selected') && this.tasksCollection.where({selected: true}).length === 1) {
				this.broker.trigger('task:select', 'checked');
			} else if (!model.get('selected') && !this.tasksCollection.where({selected: true}).length) {
				this.broker.trigger('task:select', 'unchecked');

				this.editingFormEnable = false;
			}

			// Anytime the user check or uncheck an item and the 'editingForm' view or 'changeStatus' is enable
			// we need to: check/uncheck the item and re-render the view to form/read-only mode and/or restore
			// the original status of the task.

			if (this.editingFormEnable && model.get('selected')) {
				view.render('form');
			} else if (this.changingStatusEnable && model.get('selected')) {
				view.model.set('new_status', this.parent.$el.find('.select select').val());
			} else if (this.changingStatusEnable && !model.get('selected')) {
				view.model.set('new_status', undefined);
				if (!this.tasksCollection.where({selected:true}).length)  this.changingStatusEnable = false;
				view.render('class');
			} else {
				view.render();
			}
		},

		addTask: function (object) {
			var self = this, view;

			// To create a task it needs all the datas (get them in object)
			// and syncronized _viewsCollection with the new view created.
			// Also, append the task to the list generated rendering the view.

			this.tasksCollection.create(object, {
				wait: true,
				success: function (model) {
					view = new TaskView({
						model: model,
						className: model.get('status')
					});<commit_msg>Fixed other but when selecting/unselecting on changingStatus mode<commit_after>define('tasks_view', ['app'], function (app) {

	var TaskModel = Backbone.Model.extend({
			defaults: {
				'selected': false
			},
			url: function () {
				var id = this.has('id') ? this.get('id') : '';
				return '/task/' + id;
			},
			initialize: function () {
				this.bind('remove', function () {
					this.destroy();
				}, this);
			}
		}),

		TasksCollection = Backbone.Collection.extend({
			model: TaskModel,
			url: function () {
				return 'tasks/';
			}
		}),

		TaskView = Backbone.View.extend({
			tagName: 'li',
			template: '' +
				'<p><input type="checkbox" ' +
				'<% if (selected) { %> checked <% } %>' +
				' name="task" value="<%= id %>" /></p>' +
				'<p><%= title %></p>',
			formTemplate: '' +
				'<p><input type="checkbox" checked name="task" value="<%= id %>" /></p>' +
				'<p><input type="text" name="task_title_<%= id %>" value="<%= title %>" /></p>',

			events: { 'click :checkbox': 'selectTask' },

			initialize: function () {
				_.bindAll(this, 'selectTask');
			},

			render: function (action) {
				if (action === 'form') {
					this.$el.html(_.template(this.formTemplate, this.model.toJSON()));

				} else if (action === 'class') {
					this.className = this.model.get('new_status') || this.model.get('status');
					this.$el.attr('class', this.className);
				}

				if (action !== 'form') {
					this.$el.html(_.template(this.template, this.model.toJSON()));
				}

				this.$el.attr('task_id', this.model.get('id'));
				return this;
			},

			selectTask: function (e) {
				e.preventDefault();

				// Update selected attribute of a model. That will trigger 'change:selected' event
				// which was binded with 'selectItems' function on the main view.
				this.model.set('selected', !this.model.get('selected'));
			}
		});


	return Backbone.View.extend({
		noTaskTemplate: '<li class="no-task">There is not task to do! YaY! </li>',

		initialize: function (options) {
			var self = this;
			this.parent = options.parent;
			this.broker = options.broker;

			_.bindAll(this, 'selectItems', '_tasksFetched');

			// Define a collection where will be stored a view for each task.
			this._viewsCollection = new Backbone.Collection();

			// Define a collection where will be stored the list of tasks.
			this.tasksCollection = new TasksCollection();

			// Getting the list! Make an initial request to the backend
			// to store all task in the collection as models.
			this.tasksCollection.fetch({
				success: function () {
					self._tasksFetched();
				}
			});

			// Binding events trigger by the user.
			this.tasksCollection.bind('change:selected', this.selectItems);

			// Binding public events to our general Backbone.Event object.
			// Those will be triggered by nav_view.
			this.broker.on('task:create', this.addTask, this);
			this.broker.on('tasks:edit:view', this.editTasks, this);
			this.broker.on('tasks:edit:save', this.saveTasks, this);
			this.broker.on('tasks:status:change', this.changeStatusTasks, this);
			this.broker.on('tasks:remove', this.removeTasks, this);
		},

		_tasksFetched: function () {

			// If there's no tasks in the database we need to display any message
			// telling it. If it gets some tasks we need to create and store a view for each one.

			if (this.$el.children().length !== this.tasksCollection.length &&
				!this.$el.find('.no-task').length) {
				this.render();

			} else {
				this.tasksCollection.each(function (model) {
					// Binding events. Need to create the views.
					this._viewsCollection.add({
						'id': model.get('id'),
						'view': this._getTaskView(model)
					});
				}, this);
			}
		},

		render: function () {
			var self = this;

			// Rendering the entire list of tasks
			// Or a message if there's no tasks.

			this.$el.children().remove();
			this._viewsCollection.reset([]);

			this.tasksCollection.each(function (model) {

				var view = new TaskView({
					model: model,
					className: 'backbone tag ' + model.get('status')
				});

				self._viewsCollection.add({
					'id': model.get('id'),
					'view': view
				});

				this.$el.append(view.render().el);
			}, this);

			if (!this.tasksCollection.length) {
				this.$el.append(this.noTaskTemplate);
			}

			return this;
		},

		selectItems: function (model) {
			var self = this, view = this._viewsCollection.findWhere({'id': model.get('id')}).get('view');

			// If an item has been checked and it's the first one: trigger an event to enable editRemove bar mode.
			// Else, if an item has been unchecked and it was the last element checked: trigger an event to disable
			// editRemove mode and enable addTask instead.

			if (model.get('selected') && this.tasksCollection.where({selected: true}).length === 1) {
				this.broker.trigger('task:select', 'checked');
			} else if (!model.get('selected') && !this.tasksCollection.where({selected: true}).length) {
				this.broker.trigger('task:select', 'unchecked');

				this.editingFormEnable = false;
			}

			// Anytime the user check or uncheck an item and the 'editingForm' view or 'changeStatus' is enable
			// we need to: check/uncheck the item and re-render the view to form/read-only mode and/or restore
			// the original status of the task.

			if (this.editingFormEnable && model.get('selected')) {
				view.render('form');
			} else {
				if (this.changingStatusEnable && model.get('selected')) {
					view.model.set('new_status', this.parent.$el.find('.select select').val());
				} else if (this.changingStatusEnable && !model.get('selected')) {
					view.model.set('new_status', undefined);
					if (!this.tasksCollection.where({selected:true}).length)  this.changingStatusEnable = false;
				}
				view.render('class');
			}
		},

		addTask: function (object) {
			var self = this, view;

			// To create a task it needs all the datas (get them in object)
			// and syncronized _viewsCollection with the new view created.
			// Also, append the task to the list generated rendering the view.

			this.tasksCollection.create(object, {
				wait: true,
				success: function (model) {
					view = new TaskView({
						model: model,
						className: model.get('status')
					});
{'fraction_non_alphanumeric': 0.1618417869526651, 'fraction_numerical': 0.00016127731634545602, 'mean_word_length': 3.1381381381381384, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '900991', 'n_tokens_mistral': 4651, 'n_tokens_neox': 3761, 'n_words': 1045}