<commit_before>define(
	[
		openhmis.url.backboneBase + 'js/lib/jquery',
		openhmis.url.backboneBase + 'js/lib/underscore',
		openhmis.url.backboneBase + 'js/lib/backbone',
		openhmis.url.backboneBase + 'js/view/generic',
		openhmis.url.backboneBase + 'js/lib/i18n',
		openhmis.url.backboneBase + 'js/lib/backbone-forms',
		openhmis.url.backboneBase + 'js/view/editors',
		openhmis.url.cashierBase + 'js/view/editors',
		openhmis.url.cashierBase + 'js/model/bill',
		openhmis.url.cashierBase + 'js/model/cashPoint',
		'link!' + openhmis.url.cashierBase + 'css/style.css'
	],
	function($, _, Backbone, openhmis, i18n) {
		openhmis.BillLineItemView = openhmis.GenericListItemView.extend({
			initialize: function(options) {
				this.events = _.extend({}, this.events, {
					'keypress': 'onKeyPress'
				});
				openhmis.GenericListItemView.prototype.initialize.call(this, options);
				_.bindAll(this);
				if (this.form) {
					this.form.on('price:change', this.update);
					this.form.on('quantity:change', this.update);
					this.form.on('item:change', this.updateItem);
				}
			},
			
			updateItem: function(form, itemEditor) {
				var item = itemEditor.getValue();
				this.updatePriceOptions(item, form);
				if (form.fields.quantity.getValue() === 0)
					form.fields.quantity.setValue(1);
				this.update();
				this.$('.field-quantity input').focus();
			},
			
			updatePriceOptions: function(item, form) {
				item = item ? item : this.model.get("item");
				form = form ? form : this.form;
				var defaultPrice = item ? item.get("defaultPrice") : undefined;
				var price = this.model.get("price") || defaultPrice;
				if (price !== undefined)
					price = price.id;
				var options = new openhmis.GenericCollection([], { model: openhmis.ItemPrice });
				if (item)
					options.reset(item.get("prices"));
				else
					options.add(0);
				if (form) {
					form.fields.price.editor.options.options = options;
					form.fields.price.editor.render();
					if (price)
						form.fields.price.setValue(price);
				}
			},
			
			update: function() {
				if (this.updateTimeout !== undefined) clearTimeout(this.updateTimeout);
				var view = this;
				var update = function() {
					var price = view.form.getValue("price");
					var quantity = view.form.getValue("quantity");
					view.form.setValue({ total: price * quantity });
				}
				this.updateTimeout = setTimeout(update, 200);
			},
			
			onKeyPress: function(event) {
				if (event.keyCode === 13) {
					var errors = this.commitForm(event);
				}
			},
			
			onModelChange: function(model) {
				if (model.hasChanged() && model.isValid())
					this.trigger("change", this);
			},
			
			displayErrors: function(errorMap, event) {
				// If there is already another item in the collection and
				// this is not triggered by enter key, skip the error message
				if (event && event.type !== "keypress" && this.model.collection && this.model.collection.length > 0)
					return;
				
				for(var item in errorMap) {
					var $errorEl = this.$('.field-' + item + ' .editor');
					if ($errorEl.length > 0) {
						openhmis.validationMessage($errorEl, errorMap[item]);
					}<commit_msg>Focus via form editor to allow selecting of quantity field contents.  Cancel enter key event to prevent interference with HTML form controls.<commit_after>define(
	[
		openhmis.url.backboneBase + 'js/lib/jquery',
		openhmis.url.backboneBase + 'js/lib/underscore',
		openhmis.url.backboneBase + 'js/lib/backbone',
		openhmis.url.backboneBase + 'js/view/generic',
		openhmis.url.backboneBase + 'js/lib/i18n',
		openhmis.url.backboneBase + 'js/lib/backbone-forms',
		openhmis.url.backboneBase + 'js/view/editors',
		openhmis.url.cashierBase + 'js/view/editors',
		openhmis.url.cashierBase + 'js/model/bill',
		openhmis.url.cashierBase + 'js/model/cashPoint',
		'link!' + openhmis.url.cashierBase + 'css/style.css'
	],
	function($, _, Backbone, openhmis, i18n) {
		openhmis.BillLineItemView = openhmis.GenericListItemView.extend({
			initialize: function(options) {
				this.events = _.extend({}, this.events, {
					'keypress': 'onKeyPress'
				});
				openhmis.GenericListItemView.prototype.initialize.call(this, options);
				_.bindAll(this);
				if (this.form) {
					this.form.on('price:change', this.update);
					this.form.on('quantity:change', this.update);
					this.form.on('item:change', this.updateItem);
				}
			},
			
			updateItem: function(form, itemEditor) {
				var item = itemEditor.getValue();
				this.updatePriceOptions(item, form);
				if (form.fields.quantity.getValue() === 0)
					form.fields.quantity.setValue(1);
				this.update();
				form.fields.quantity.editor.focus(true);
			},
			
			updatePriceOptions: function(item, form) {
				item = item ? item : this.model.get("item");
				form = form ? form : this.form;
				var defaultPrice = item ? item.get("defaultPrice") : undefined;
				var price = this.model.get("price") || defaultPrice;
				if (price !== undefined)
					price = price.id;
				var options = new openhmis.GenericCollection([], { model: openhmis.ItemPrice });
				if (item)
					options.reset(item.get("prices"));
				else
					options.add(0);
				if (form) {
					form.fields.price.editor.options.options = options;
					form.fields.price.editor.render();
					if (price)
						form.fields.price.setValue(price);
				}
			},
			
			update: function() {
				if (this.updateTimeout !== undefined) clearTimeout(this.updateTimeout);
				var view = this;
				var update = function() {
					var price = view.form.getValue("price");
					var quantity = view.form.getValue("quantity");
					view.form.setValue({ total: price * quantity });
				}
				this.updateTimeout = setTimeout(update, 200);
			},
			
			onKeyPress: function(event) {
				if (event.keyCode === 13 /* Enter */)  {
					this.commitForm(event);
					// Prevent enter press from interfering with HTML form controls
					event.preventDefault();
				}
			},
			
			onModelChange: function(model) {
				if (model.hasChanged() && model.isValid())
					this.trigger("change", this);
			},
			
			displayErrors: function(errorMap, event) {
				// If there is already another item in the collection and
				// this is not triggered by enter key, skip the error message
				if (event && event.type !== "keypress" && this.model.collection && this.model.collection.length > 0)
					return;
				
				for(var item in errorMap) {
					var $errorEl = this.$('.field-' + item + ' .editor');
					if ($errorEl.length > 0) {
						openhmis.validationMessage($errorEl, errorMap[item]);
					}
{'fraction_non_alphanumeric': 0.15278628291488058, 'fraction_numerical': 0.004286589099816289, 'mean_word_length': 3.212121212121212, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1207256', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2098, 'n_words': 415}