Subversion Repositories SmartDukaan

Rev

Rev 36892 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21627 kshitij.so 1
$(function() {
36890 aman 2
 
27754 amit.gupta 3
	$(document).on('click', ".contact-us", function() {
22354 ashik.ali 4
		console.log("Contact Us Button Clicked...")
5
		contactUs("main-content");
6
	});
36890 aman 7
 
8
	// ----- Add / Edit / Delete contact entries (authorised users only) -----
9
 
10
	// Open the modal in "add" mode for the clicked section.
11
	$(document).on('click', ".contact-add-btn", function() {
12
		var section = $(this).data('section');
13
		$('#contactUsForm')[0].reset();
14
		$('#cuId').val(0);
15
		$('#cuSection').val(section);
16
		$('#contactUsModalTitle').text(section === 'ESCALATION' ? 'Add Escalation' : 'Add Contact');
17
		$('#cuBaseAreaLabel').text(section === 'ESCALATION' ? 'Region' : 'Base Area');
37019 aman 18
		buildPositionOptions(section, null, false);
36890 aman 19
		$('#contactUsModal').modal('show');
20
	});
21
 
22
	// Open the modal pre-filled in "edit" mode.
23
	$(document).on('click', ".contact-edit-btn", function() {
24
		var btn = $(this);
25
		var section = btn.data('section');
26
		var mobile = String(btn.data('mobile'));
27
		$('#contactUsForm')[0].reset();
28
		$('#cuId').val(btn.data('id'));
29
		$('#cuSection').val(section);
30
		$('#cuArea').val(btn.data('area'));
31
		$('#cuBaseArea').val(btn.data('basearea'));
32
		$('#cuName').val(btn.data('name'));
33
		$('#cuDesignation').val(btn.data('designation'));
34
		$('#cuMobile').val(mobile === '-' ? '' : mobile);
35
		$('#cuEmail').val(btn.data('email'));
36
		$('#contactUsModalTitle').text('Edit Contact');
37
		$('#cuBaseAreaLabel').text(section === 'ESCALATION' ? 'Region' : 'Base Area');
37019 aman 38
		buildPositionOptions(section, btn.data('id'), true);
36890 aman 39
		$('#contactUsModal').modal('show');
40
	});
41
 
42
	// Persist add / edit.
43
	$(document).on('click', "#contactUsSaveBtn", function() {
44
		var name = $.trim($('#cuName').val());
45
		if (name === '') {
46
			alert('Name is required.');
47
			return;
48
		}
37019 aman 49
		var isEdit = parseInt($('#cuId').val(), 10) > 0;
50
		var posVal = $('#cuPosition').val() || (isEdit ? 'KEEP|0' : 'BOTTOM|0');
51
		var posParts = posVal.split('|');
36890 aman 52
		var params = {
53
			id: $('#cuId').val(),
54
			section: $('#cuSection').val(),
55
			area: $('#cuArea').val(),
56
			baseArea: $('#cuBaseArea').val(),
57
			name: name,
58
			designation: $('#cuDesignation').val(),
59
			mobile: $('#cuMobile').val(),
37019 aman 60
			email: $('#cuEmail').val(),
61
			position: posParts[0],
62
			afterId: posParts[1] || 0
36890 aman 63
		};
64
		doPostAjaxRequestWithParamsHandler(context + "/contactUs/save", params, function(response) {
65
			if (response && response.success) {
66
				$('#contactUsModal').modal('hide');
67
				contactUs("main-content");
68
			} else {
69
				alert((response && response.message) ? response.message : 'Could not save contact.');
70
			}
71
		});
72
	});
73
 
36892 aman 74
	// Soft-delete an entry (with a styled confirmation).
36890 aman 75
	$(document).on('click', ".contact-delete-btn", function() {
76
		var name = $(this).data('name');
36892 aman 77
		var id = $(this).data('id');
78
		var doDelete = function() {
79
			doPostAjaxRequestWithParamsHandler(context + "/contactUs/delete", { id: id }, function(response) {
80
				if (response && response.success) {
81
					contactUs("main-content");
82
				} else {
83
					alert((response && response.message) ? response.message : 'Could not remove contact.');
84
				}
85
			});
86
		};
87
		if (typeof bootbox !== 'undefined' && bootbox.confirm) {
88
			bootbox.confirm({
89
				title: "Delete contact",
90
				message: "Are you sure you want to remove <b>" + name + "</b> from the contact list?",
91
				buttons: {
92
					confirm: { label: '<i class="fa fa-trash"></i> Yes, delete', className: 'btn-danger' },
93
					cancel: { label: 'Cancel', className: 'btn-default' }
94
				},
95
				callback: function(result) { if (result) { doDelete(); } }
96
			});
97
		} else if (confirm('Remove ' + name + ' from the contact list?')) {
98
			doDelete();
36890 aman 99
		}
100
	});
101
 
37019 aman 102
	// Move a contact one step up/down within its section (authorised users only).
103
	$(document).on('click', ".contact-up-btn, .contact-down-btn", function() {
104
		var id = $(this).data('id');
105
		var direction = $(this).hasClass('contact-up-btn') ? 'UP' : 'DOWN';
106
		doPostAjaxRequestWithParamsHandler(context + "/contactUs/reorder", { id: id, direction: direction }, function(response) {
107
			if (response && response.success) {
108
				contactUs("main-content");
109
			} else {
110
				alert((response && response.message) ? response.message : 'Could not reorder contact.');
111
			}
112
		});
113
	});
114
 
22661 amit.gupta 115
});
116
 
23343 ashik.ali 117
function contactUs(domId){
23500 ashik.ali 118
	doGetAjaxRequestHandler(context+"/contactUs", function(response){
119
		console.log("doGetAjaxRequest");
36890 aman 120
		// Drop any previously relocated modal to avoid duplicate #contactUsModal ids.
121
		$('#contactUsModal').remove();
23343 ashik.ali 122
		$('#' + domId).html(response);
36890 aman 123
		// The dashboard applies `zoom` to #main-content > .wrapper, which traps a
124
		// Bootstrap modal behind its body-level backdrop ("screen dims, no popup").
125
		// Move the modal out to <body> so it renders above the backdrop.
126
		$('#contactUsModal').appendTo('body');
22245 ashik.ali 127
	});
36890 aman 128
}
37019 aman 129
 
130
// Populate the "Position in list" dropdown for the given section by scanning the rendered rows.
131
// On edit, the contact being edited is excluded and "Keep current position" is offered as default.
132
function buildPositionOptions(section, excludeId, isEdit){
133
	var $sel = $('#cuPosition').empty();
134
	if (isEdit) {
135
		$sel.append($('<option>').val('KEEP|0').text('Keep current position'));
136
	}
137
	$sel.append($('<option>').val('TOP|0').text('Top of list'));
138
	$('#contact-us-table tr[data-cu-section="' + section + '"]').each(function(){
139
		var cid = String($(this).data('cuId'));
140
		if (excludeId != null && cid === String(excludeId)) {
141
			return; // skip the contact being edited - can't place it after itself
142
		}
143
		$sel.append($('<option>').val('AFTER|' + cid).text('After — ' + $(this).data('cuName')));
144
	});
145
	$sel.append($('<option>').val('BOTTOM|0').text('Bottom of list'));
146
	$sel.val(isEdit ? 'KEEP|0' : 'BOTTOM|0');
147
}