<commit_before>/*!
 * SWIF-OpenSTC
 * Copyright 2013-2014 Siclic <<EMAIL>>
 * Licensed under AGPL-3.0 (https://www.gnu.org/licenses/agpl.txt)
 */

define([
	'app',

	'advancedSelectBoxView',

	'consumablesCollection',

], function(app, AdvancedSelectBoxView, ConsumablesCollection){

	'use strict';


	/******************************************
	* ConsumablesSelectView form
	*/
	var ConsumablesSelectView = Backbone.View.extend({


		templateHTML : 'templates/form-components/consumablesSelect.html',


		selectedConsumables: [],

		events: {
			'click a[data-action="remove"]'    : 'removeRow',
			'click a[data-action="duplicate"]' : 'duplicateRow'
		},


		// Row template //
		rowTemplate :
			'<tr data-id="<%= id %>" data-text="<%= text %>" class="row-item"> \
				<td class="col-sm-4"> \
					<span><%= text %></span> \
					<p class="row-actions invisible"> \
						<a href="#" data-action="duplicate" data-toggle="tooltip" data-original-title="'+_.capitalize(app.lang.actions.duplicate)+'"> <i class="fa fa-files-o fa-fw fa-lg"></i> </a> \
						<a href="#" data-action="remove" data-toggle="tooltip" data-original-title="'+_.capitalize(app.lang.actions.delete)+'"> <i class="fa fa-trash-o fa-fw fa-lg"></i> </a> \
					</p> \
				</td> \
				<td class="col-sm-4"><input type="text" class="fieldDQE form-control input-sm"></td> \
				<td class="col-sm-2"><input type="number" step="1" min="1" class="fieldQuantity form-control input-sm" value="1"></td> \
				<td class="col-sm-2"> \
					<div class="input-group input-group-sm"> \
						<input type="text" class="fieldUnitPrice form-control input-sm"> \
						<span class="input-group-addon"><i class="fa fa-eur"></i></span> \
					</div> \
				</td> \
			</tr>',


		/** View Initialization
		*/
		initialize: function(options){

			this.serviceID = options.serviceID;

			this.render();
		},



		/** View Render
		*/
		render: function(){
			var self = this;

			// Retrieve the template //
			$.get(this.templateHTML, function(templateData){

				var template = _.template(templateData, {
					lang: app.lang
				});

				$(self.el).html(template);


				// Create the advance selectBox view and listen the change event //
				self.advancedSelectBoxCatConsumables = new AdvancedSelectBoxView({el: $('#field_consumables'), url: ConsumablesCollection.prototype.url });
				self.advancedSelectBoxCatConsumables.render();
				self.advancedSelectBoxCatConsumables.select2.off().on('select2-selecting', function(e){ self.addConsumable(e); } );

			});

			return this;
		},



		/** Function to add consumables inside the <table>
		*/
		addConsumable: function(e){

			$(this.el).find('.tableConsumables.hide').removeClass('hide');

			var consumable = e.object;

			$(this.el).find('.consumablesRows').append(_.template(this.rowTemplate, consumable));
			$(this.el).find('*[data-toggle="tooltip"]').tooltip({ delay: { show: 250, hide: 0 }});
		},



		/** Function to get all the consumables inside the <table>
		*/
		getConsumables: function(){
			var self = this;

			_.each($(this.el).find('.consumablesRows tr'), function(row){

				var consum = {
					id        : $(row).data('id'),
					dqe       : $(row).find('input.fieldDQE').val(),
					quantity  : $(row).find('input.fieldQuantity').val(),
					unit_price: $(row).find('input.fieldUnitPrice').val()
				};

				self.selectedConsumables.push(consum);
			});

			return self.selectedConsumables;
		},



		/** Delete a row from the <table>
		*/
		removeRow: function(e){
			e.preventDefault();
<commit_msg>BUG FIX : reset consumables on initialize view<commit_after>/*!
 * SWIF-OpenSTC
 * Copyright 2013-2014 Siclic <<EMAIL>>
 * Licensed under AGPL-3.0 (https://www.gnu.org/licenses/agpl.txt)
 */

define([
	'app',

	'advancedSelectBoxView',

	'consumablesCollection',

], function(app, AdvancedSelectBoxView, ConsumablesCollection){

	'use strict';


	/******************************************
	* ConsumablesSelectView form
	*/
	var ConsumablesSelectView = Backbone.View.extend({


		templateHTML : 'templates/form-components/consumablesSelect.html',


		selectedConsumables: [],

		events: {
			'click a[data-action="remove"]'    : 'removeRow',
			'click a[data-action="duplicate"]' : 'duplicateRow'
		},


		// Row template //
		rowTemplate :
			'<tr data-id="<%= id %>" data-text="<%= text %>" class="row-item"> \
				<td class="col-sm-4"> \
					<span><%= text %></span> \
					<p class="row-actions invisible"> \
						<a href="#" data-action="duplicate" data-toggle="tooltip" data-original-title="'+_.capitalize(app.lang.actions.duplicate)+'"> <i class="fa fa-files-o fa-fw fa-lg"></i> </a> \
						<a href="#" data-action="remove" data-toggle="tooltip" data-original-title="'+_.capitalize(app.lang.actions.delete)+'"> <i class="fa fa-trash-o fa-fw fa-lg"></i> </a> \
					</p> \
				</td> \
				<td class="col-sm-4"><input type="text" class="fieldDQE form-control input-sm"></td> \
				<td class="col-sm-2"><input type="number" step="1" min="1" class="fieldQuantity form-control input-sm" value="1"></td> \
				<td class="col-sm-2"> \
					<div class="input-group input-group-sm"> \
						<input type="text" class="fieldUnitPrice form-control input-sm"> \
						<span class="input-group-addon"><i class="fa fa-eur"></i></span> \
					</div> \
				</td> \
			</tr>',


		/** View Initialization
		*/
		initialize: function(options){

			this.serviceID = options.serviceID;
			this.selectedConsumables = [];
			this.render();
		},



		/** View Render
		*/
		render: function(){
			var self = this;

			// Retrieve the template //
			$.get(this.templateHTML, function(templateData){

				var template = _.template(templateData, {
					lang: app.lang
				});

				$(self.el).html(template);


				// Create the advance selectBox view and listen the change event //
				self.advancedSelectBoxCatConsumables = new AdvancedSelectBoxView({el: $('#field_consumables'), url: ConsumablesCollection.prototype.url });
				self.advancedSelectBoxCatConsumables.render();
				self.advancedSelectBoxCatConsumables.select2.off().on('select2-selecting', function(e){ self.addConsumable(e); } );

			});

			return this;
		},



		/** Function to add consumables inside the <table>
		*/
		addConsumable: function(e){

			$(this.el).find('.tableConsumables.hide').removeClass('hide');

			var consumable = e.object;

			$(this.el).find('.consumablesRows').append(_.template(this.rowTemplate, consumable));
			$(this.el).find('*[data-toggle="tooltip"]').tooltip({ delay: { show: 250, hide: 0 }});
		},



		/** Function to get all the consumables inside the <table>
		*/
		getConsumables: function(){
			var self = this;

			_.each($(this.el).find('.consumablesRows tr'), function(row){

				var consum = {
					id        : $(row).data('id'),
					dqe       : $(row).find('input.fieldDQE').val(),
					quantity  : $(row).find('input.fieldQuantity').val().replace(',', '.'),
					unit_price: $(row).find('input.fieldUnitPrice').val().replace(',', '.')
				};

				self.selectedConsumables.push(consum);
			});

			return self.selectedConsumables;
		},



		/** Delete a row from the <table>
		*/
		removeRow: function(e){
			e.preventDefault();

{'fraction_non_alphanumeric': 0.20274049217002238, 'fraction_numerical': 0.006431767337807606, 'mean_word_length': 3.487452948557089, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 81, 'https://': 2, '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': '7582956', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2330, 'n_words': 435}