<commit_before>(function()
{
	var credentialsToHtml = function(credentials)
	{
		var html = '';
		
		if(typeof credentials == 'object')
		{
			for(var key in credentials)
			{
				html += '<p class="pre-label">' + key + '</p>';
				html += '<pre>' + credentials[key] + '</pre>';
			}
			
			html += '<p class="pre-label">JSON</p>';
			html += '<pre>' + JSON.stringify(credentials, null, 4) + '</pre>';
		}
		
		return html;
	};
	
	var serviceBindingPumpkin = new Pumpkin();
	serviceBindingPumpkin.addWork('getServiceInstanceForServiceBinding', function()
	{
		var next = this.next;
		var error = this.error;
		
		CF.async({url : this.data.serviceBinding.entity.service_instance_url}, function(result)
		{
			if(result)
			{
				if(result.entity)
				{
					next(result);
				}
				else
				{
					error(result.description ? result.description : JSON.stringify(result.error));
				}
			}
			else
			{
				error('Service instance is not found.');
			}
			
		}, function(error)
		{
			error(error);
		});
	});
	
	serviceBindingPumpkin.addWork('getServicePlan', function(serviceInstance)
	{
		var next = this.next;
		var error = this.error;
		
		CF.async({url : '/v2/service_plans/' + serviceInstance.entity.service_plan_guid}, function(result)
		{
			if(result)
			{
				var servicePlan = result;
				if(servicePlan.entity)
				{
					CF.async({url : servicePlan.entity.service_url}, function(result)
					{
						if(result)
						{
							next({serviceInstance : serviceInstance, servicePlan : servicePlan, service : result});
						}
						else
						{
							next({serviceInstance : serviceInstance, servicePlan : servicePlan});
						}
					},
					function(error)
					{
						next({serviceInstance : serviceInstance, servicePlan : servicePlan, service : error});
					});
				}
				else
				{
					next({serviceInstance : serviceInstance, servicePlan : servicePlan});
				}
			}
			else
			{
				next({serviceInstance : serviceInstance});
			}
		},
		function(error)
		{
			next({serviceInstance : serviceInstance, servicePlan : error});
		});
	});
	
	var serviceListPumpkin = new Pumpkin();
	serviceListPumpkin.addWork('getServiceInstances', function()
	{
		var that = this;
		var next = this.next;
		var space = $('#' + _global.hash.space).get(0);
		CF.async({url : '/v2/spaces/' + space.item.metadata.guid + '/service_instances'}, function(result)
		{
			if(result)
			{
				that.data.services = result;
				next();
			}
			else
			{
				next();
			}
		},
		function(error)
		{
			next();
		});
	});
	
	serviceListPumpkin.addWork('getUserProvidedServiceInstances', function()
	{
		var that = this;
		var next = this.next;
		CF.async({url : '/v2/user_provided_service_instances'}, function(result)
		{
			if(result)
			{
				that.data.userProvidedServices = result;
				next();
			}
			else
			{
				next();
			}
		},
		function(error)
		{
			next();
		});
	});
	
	var bindingService = function(context, serviceBinding, callback)
	{
		var credentials = serviceBinding.entity.credentials;
		serviceBindingPumpkin.setData({serviceBinding : serviceBinding});
		serviceBindingPumpkin.execute(['getServiceInstanceForServiceBinding', 'getServicePlan'], function(params)
		{
			var serviceInstance = params.serviceInstance;
			var servicePlan = params.servicePlan;
			var service = params.service;
			
			var template = $('#appServiceTemplate').html();
			
			template = template.replace('{manageUrl}', serviceInstance.entity.dashboard_url)
					.replace('{name}', serviceInstance.entity.name)
					.replace('{credentials}', credentialsToHtml(credentials));
			
			var imageUrl = '';
			var supportUrl = '';
			var docsUrl = '';
			var description = '';
			
			if(service)
			{
				if(service.entity)
				{
					var extra = service.entity.extra;
					description = service.entity.label;
					
					if(extra)
					{
						extra = JSON.parse(extra);
						
						if(extra.imageUrl)
							imageUrl = extra.imageUrl;
						
						supportUrl = extra.supportUrl;
						docsUrl = extra.documentationUrl;
					}
				}
			}
			
			template = $(template.replace('{imgUrl}', imageUrl).replace('{description}', description).replace('{supportUrl}', supportUrl).replace('{docsUrl}', docsUrl));
			
			template.get(0).item = {serviceInstance : serviceInstance, servicePlan : servicePlan, service : service};
			
			$(template).find('.credentials').on('click', function()
			{
				if(!this.isShown)
				{
					$(this).find('span:first').text('Hide credentials');
					$(this).parent().next().show();
					
					this.isShown = true;
				}
				else
				{
					$(this).find('span:first').text('Show credentials');
					$(this).parent().next().hide();
					this.isShown = false;
				}
			});
			
			var unbindButton = template.find('.unbind');
			confirmButton(unbindButton, function(done)
			{
				CF.async({url : '/v2/service_bindings/' + serviceBinding.metadata.guid, method : 'DELETE'}, function(result)
				{
					var length = unbindButton.parent().parent().parent().find('tr').length;
					unbindButton.parent().parent().remove();
					
					$(context).find('.service-select').append('<option value="' + serviceInstance.metadata.guid + '">' + serviceInstance.entity.name + '</option>');
					
					if(length == 1)
					{
						$(context).find('.service-table tbody').html('<tr><td colspan="4" class="center-align">no service bindings</td></tr>');
					}
				},
				function(error)
				{
					$('<p>' + error + '</p>').insertAfter(confirmButton);
				});
			});
			
			$(context).find('.service-table tbody').append(template);
			
			callback();
		},
		function(workName, error)
		{
			$(context).find('.service-table tbody').append('<tr><td colspan="4" style="text-align: center;">' + error + '</td></tr>');
		});
	};
	
	var pumpkin = new Pumpkin();
	pumpkin.addWork('drawServiceList', function()
	{
		var context = this.data.context;
		
		var that = this;
		var next = this.next;
		CF.async({url : this.data.app.entity.service_bindings_url}, function(result)
		{
			$(context).find('.service-table tbody').html('');
			if(result)
			{
				if(result.resources)
				{
					var serviceList = result.resources;
					that.data.serviceList = serviceList;
					
					$(context).find('.service-table tbody').html('');
					
					var forEach = new ForEach();
					forEach.async(serviceList, function(service, index)
					{
						var done = this.done;
						bindingService(context, service, done);
					},
					function()
					{
						$(context).find('.service-table .credentials').on('click', function()
						{
							if(!this.isShown)
							{
								$(this).find('span:first').text('Hide credentials');
								$(this).parent().next().show();
								
								this.isShown = true;
							}
							else
							{
								$(this).find('span:first').text('Show credentials');
								$(this).parent().next().hide();
								this.isShown = false;
							}
						});
						
						if(serviceList.length == 0)
						{
							$(context).find('.service-table tbody').append('<tr><td colspan="4" style="text-align: center;">no service bindings</td></tr>');
						}
						
						next();
					});
				}
				else<commit_msg>Fixed bugs.<commit_after>(function()
{
	var credentialsToHtml = function(credentials)
	{
		var html = '';
		
		if(typeof credentials == 'object')
		{
			for(var key in credentials)
			{
				html += '<p class="pre-label">' + key + '</p>';
				html += '<pre>' + credentials[key] + '</pre>';
			}
			
			html += '<p class="pre-label">JSON</p>';
			html += '<pre>' + JSON.stringify(credentials, null, 4) + '</pre>';
		}
		
		return html;
	};
	
	var serviceBindingPumpkin = new Pumpkin();
	serviceBindingPumpkin.addWork('getServiceInstanceForServiceBinding', function()
	{
		var next = this.next;
		var error = this.error;
		
		CF.async({url : this.data.serviceBinding.entity.service_instance_url}, function(result)
		{
			if(result)
			{
				if(result.entity)
				{
					next(result);
				}
				else
				{
					error(result.description ? result.description : JSON.stringify(result.error));
				}
			}
			else
			{
				error('Service instance is not found.');
			}
			
		}, function(error)
		{
			error(error);
		});
	});
	
	serviceBindingPumpkin.addWork('getServicePlan', function(serviceInstance)
	{
		var next = this.next;
		var error = this.error;
		
		CF.async({url : '/v2/service_plans/' + serviceInstance.entity.service_plan_guid}, function(result)
		{
			if(result)
			{
				var servicePlan = result;
				if(servicePlan.entity)
				{
					CF.async({url : servicePlan.entity.service_url}, function(result)
					{
						if(result)
						{
							next({serviceInstance : serviceInstance, servicePlan : servicePlan, service : result});
						}
						else
						{
							next({serviceInstance : serviceInstance, servicePlan : servicePlan});
						}
					},
					function(error)
					{
						next({serviceInstance : serviceInstance, servicePlan : servicePlan, service : error});
					});
				}
				else
				{
					next({serviceInstance : serviceInstance, servicePlan : servicePlan});
				}
			}
			else
			{
				next({serviceInstance : serviceInstance});
			}
		},
		function(error)
		{
			next({serviceInstance : serviceInstance, servicePlan : error});
		});
	});
	
	var serviceListPumpkin = new Pumpkin();
	serviceListPumpkin.addWork('getServiceInstances', function()
	{
		var that = this;
		var next = this.next;
		var space = $('#' + _global.hash.space).get(0);
		CF.async({url : '/v2/spaces/' + space.item.metadata.guid + '/service_instances'}, function(result)
		{
			if(result)
			{
				that.data.services = result;
				next();
			}
			else
			{
				next();
			}
		},
		function(error)
		{
			next();
		});
	});
	
	serviceListPumpkin.addWork('getUserProvidedServiceInstances', function()
	{
		var that = this;
		var next = this.next;
		CF.async({url : '/v2/user_provided_service_instances'}, function(result)
		{
			if(result)
			{
				that.data.userProvidedServices = result;
				next();
			}
			else
			{
				next();
			}
		},
		function(error)
		{
			next();
		});
	});
	
	var bindingService = function(context, serviceBinding, callback)
	{
		var credentials = serviceBinding.entity.credentials;
		serviceBindingPumpkin.setData({serviceBinding : serviceBinding});
		serviceBindingPumpkin.execute(['getServiceInstanceForServiceBinding', 'getServicePlan'], function(params)
		{
			var serviceInstance = params.serviceInstance;
			var servicePlan = params.servicePlan;
			var service = params.service;
			
			var template = $('#appServiceTemplate').html();
			
			template = template.replace('{manageUrl}', serviceInstance.entity.dashboard_url)
					.replace('{name}', serviceInstance.entity.name)
					.replace('{credentials}', credentialsToHtml(credentials));
			
			var imageUrl = '';
			var supportUrl = '';
			var docsUrl = '';
			var description = '';
			
			if(service)
			{
				if(service.entity)
				{
					var extra = service.entity.extra;
					description = service.entity.label;
					
					if(extra)
					{
						extra = JSON.parse(extra);
						
						if(extra.imageUrl)
							imageUrl = extra.imageUrl;
						
						supportUrl = extra.supportUrl;
						docsUrl = extra.documentationUrl;
					}
				}
			}
			
			template = $(template.replace('{imgUrl}', imageUrl).replace('{description}', description).replace('{supportUrl}', supportUrl).replace('{docsUrl}', docsUrl));
			
			template.get(0).item = {serviceInstance : serviceInstance, servicePlan : servicePlan, service : service};
			
			$(template).find('.credentials').on('click', function()
			{
				if(!this.isShown)
				{
					$(this).find('span:first').text('Hide credentials');
					$(this).parent().next().show();
					
					this.isShown = true;
				}
				else
				{
					$(this).find('span:first').text('Show credentials');
					$(this).parent().next().hide();
					this.isShown = false;
				}
			});
			
			var unbindButton = template.find('.unbind');
			confirmButton(unbindButton, function(done)
			{
				CF.async({url : '/v2/service_bindings/' + serviceBinding.metadata.guid, method : 'DELETE'}, function(result)
				{
					var length = unbindButton.parent().parent().parent().find('tr').length;
					unbindButton.parent().parent().remove();
					
					$(context).find('.service-select').append('<option value="' + serviceInstance.metadata.guid + '">' + serviceInstance.entity.name + '</option>');
					
					if(length == 1)
					{
						$(context).find('.service-table tbody').html('<tr><td colspan="4" class="center-align">no service bindings</td></tr>');
					}
				},
				function(error)
				{
					$('<p>' + error + '</p>').insertAfter(confirmButton);
				});
			});
			
			$(context).find('.service-table tbody').append(template);
			
			callback();
		},
		function(workName, error)
		{
			$(context).find('.service-table tbody').append('<tr><td colspan="4" style="text-align: center;">' + error + '</td></tr>');
		});
	};
	
	var pumpkin = new Pumpkin();
	pumpkin.addWork('drawServiceList', function()
	{
		var context = this.data.context;
		
		var that = this;
		var next = this.next;
		CF.async({url : this.data.app.entity.service_bindings_url}, function(result)
		{
			$(context).find('.service-table tbody').html('');
			if(result)
			{
				if(result.resources)
				{
					var serviceList = result.resources;
					that.data.serviceList = serviceList;
					
					$(context).find('.service-table tbody').html('');
					
					var forEach = new ForEach();
					forEach.async(serviceList, function(service, index)
					{
						var done = this.done;
						bindingService(context, service, done);
					},
					function()
					{
//						$(context).find('.service-table .credentials').on('click', function()
//						{
//							if(!this.isShown)
//							{
//								$(this).find('span:first').text('Hide credentials');
//								$(this).parent().next().show();
//								
//								this.isShown = true;
//							}
//							else
//							{
//								$(this).find('span:first').text('Show credentials');
//								$(this).parent().next().hide();
//								this.isShown = false;
//							}
//						});
						
						if(serviceList.length == 0)
						{
							$(context).find('.service-table tbody').append('<tr><td colspan="4" style="text-align: center;">no service bindings</td></tr>');
						}
						
						next();
					});
				}
				else
{'fraction_non_alphanumeric': 0.17105263157894737, 'fraction_numerical': 0.0016887137630171687, 'mean_word_length': 2.8507179626117582, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, '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': '1882480', 'n_tokens_mistral': 5974, 'n_tokens_neox': 4403, 'n_words': 718}