<commit_before>(function(angular, undefined) {
'use strict';

// module: django.shop, TODO: move this into a summary JS file
var djangoShopModule = angular.module('django.shop.product', ['ui.bootstrap']);

djangoShopModule.controller('AddToCartCtrl', ['$scope', '$http', '$window', '$modal',
                                               function($scope, $http, $window, $modal) {
	var updateUrl = $window.location.pathname + '/add-to-cart' + $window.location.search;

	this.loadContext = function() {
		$http.get(updateUrl).success(function(context) {
			console.log('loaded product:');
			console.log(context);
			$scope.context = context;
		}).error(function(msg) {
			console.error('Unable to get context: ' + msg);
		});
	}

	$scope.updateContext = function() {
		$http.post(updateUrl, $scope.context).success(function(context) {
			console.log('loaded product:');
			console.log(context);
			$scope.context = context;
		}).error(function(msg) {
			console.error('Unable to update context: ' + msg);
		});
	}

	$scope.addToCart = function(cart_url) {
		$modal.open({
			templateUrl: 'AddToCartModalDialog.html',
			controller: 'ModalInstanceCtrl',
			resolve: {
				modal_context: function() {
					return {cart_url: cart_url, context: $scope.context};
				}
			}
		}).result.then(function(next_url) {
			console.log(next_url);
		});
	}

}]);

djangoShopModule.controller('ModalInstanceCtrl', ['$scope', '$http', '$modalInstance', 'modal_context',
                                        function($scope, $http, $modalInstance, modal_context) {
	console.log(modal_context);
	$scope.proceed = function(next_url) {
		$http.post(modal_context.cart_url, $scope.context).success(function() {
			$modalInstance.close(next_url);
		}).error(function() {
			// TODO: tell us something went wrong
			$modalInstance.dismiss('cancel');
		});
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};

	$scope.context = angular.copy(modal_context.context);
}]);


// Directive <add-to-cart>
// handle dialog box on the product's detail page to add a product to the cart
djangoShopModule.directive('addToCart', function() {
	return {
		restrict: 'EAC',
		controller: 'AddToCartCtrl',<commit_msg>avoid to retrigger cart calculation<commit_after>(function(angular, undefined) {
'use strict';

// module: django.shop, TODO: move this into a summary JS file
var djangoShopModule = angular.module('django.shop.product', ['ui.bootstrap']);

djangoShopModule.controller('AddToCartCtrl', ['$scope', '$http', '$window', '$modal',
                                               function($scope, $http, $window, $modal) {
	var updateUrl = $window.location.pathname + '/add-to-cart' + $window.location.search;
	var isLoading = false;

	this.loadContext = function() {
		$http.get(updateUrl).success(function(context) {
			console.log('loaded product:');
			console.log(context);
			$scope.context = context;
		}).error(function(msg) {
			console.error('Unable to get context: ' + msg);
		});
	}

	$scope.updateContext = function() {
		if (isLoading)
			return;
		isLoading = true;
		$http.post(updateUrl, $scope.context).success(function(context) {
			isLoading = false;
			console.log('loaded product:');
			console.log(context);
			$scope.context = context;
		}).error(function(msg) {
			isLoading = false;
			console.error('Unable to update context: ' + msg);
		});
	}

	$scope.addToCart = function(cart_url) {
		$modal.open({
			templateUrl: 'AddToCartModalDialog.html',
			controller: 'ModalInstanceCtrl',
			resolve: {
				modal_context: function() {
					return {cart_url: cart_url, context: $scope.context};
				}
			}
		}).result.then(function(next_url) {
			$window.location.href = next_url;
		});
	}

}]);

djangoShopModule.controller('ModalInstanceCtrl', ['$scope', '$http', '$modalInstance', 'modal_context',
                                        function($scope, $http, $modalInstance, modal_context) {
	console.log(modal_context);
	$scope.proceed = function(next_url) {
		$http.post(modal_context.cart_url, $scope.context).success(function() {
			$modalInstance.close(next_url);
		}).error(function() {
			// TODO: tell us something went wrong
			$modalInstance.dismiss('cancel');
		});
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};

	$scope.context = angular.copy(modal_context.context);
}]);


// Directive <add-to-cart>
// handle dialog box on the product's detail page to add a product to the cart
djangoShopModule.directive('addToCart', function() {
	return {
		restrict: 'EAC',
		controller: 'AddToCartCtrl',
{'fraction_non_alphanumeric': 0.1636323755209476, 'fraction_numerical': 0.0, 'mean_word_length': 3.8822269807280514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1531059', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1401, 'n_words': 276}