summaryrefslogtreecommitdiff
path: root/script/postman.js
blob: f019dbb2e5df758061241f895d1922620494af40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var redirectUrlWarning = false;
if (!console)
	console = {
		log : function() {
		}
	}
function disable(identifier) {
	var el = jQuery(identifier);
	console.debug('disabling ' + identifier);
	el.attr('disabled', 'disabled');
}
function enable(identifier) {
	var el = jQuery(identifier);
	console.debug('enabling ' + identifier);
	el.removeAttr('disabled');
}
function hide(identifier) {
	var el = jQuery(identifier);
	console.debug('hiding ' + identifier);
	el.hide("fast");
}
function show(identifier) {
	var el = jQuery(identifier);
	console.debug('showing ' + identifier);
	el.show("fast");
}
function writeable(identifier) {
	var el = jQuery(identifier);
	el.prop("readonly", false);
}
function readonly(identifier) {
	var el = jQuery(identifier);
	el.prop("readonly", true);
}
function hideLoaderIcon() {
	hide('.ajax-loader');
}
function showLoaderIcon() {
	show('.ajax-loader');
}
function handleConfigurationResponse(response) {
	response = response.data;
	if (response.display_auth == 'oauth2') {
		show('p#wizard_oauth2_help');
		jQuery('p#wizard_oauth2_help').html(response.help_text);
		jQuery(postman_redirect_url_el).val(response.redirect_url);
		jQuery('#input_oauth_callback_domain').val(response.callback_domain);
		jQuery('#client_id').html(response.client_id_label);
		jQuery('#client_secret').html(response.client_secret_label);
		jQuery('#redirect_url').html(response.redirect_url_label);
		jQuery('#callback_domain').html(response.callback_domain_label);
	}
}
// add an event on the authentication input field
// on user changing the auth type, determine whether to show
// password or oauth section
jQuery(document).ready(function() {
	jQuery('a#show-diagnostics').click(function() {
		show('#diagnostic-text');
	});
});

// http://www.electrictoolbox.com/toggle-password-field-text-password/
function setupPasswordToggle(passwordFieldId, togglePasswordFieldId) {
	try {
		/**
		 * switch the password field to text, then back to password to see if it
		 * supports changing the field type (IE9+, and all other browsers do).
		 * then switch it back.
		 */
		passwordField = document.getElementById(passwordFieldId);
		for (var i = 0, len = passwordField.value.length; i < len; i++) {
			if (passwordField.value[i] == '*')
				return false;
		}
		passwordField.type = 'text';
		passwordField.type = 'password';

		/**
		 * if it does support changing the field type then add the event handler
		 * and make the button visible. if the browser doesn't support it, then
		 * this is bypassed and code execution continues in the catch() section
		 * below
		 */
		togglePasswordField = document.getElementById(togglePasswordFieldId);
		togglePasswordField.addEventListener('click',
				togglePasswordFieldClicked, false);
		togglePasswordField.style.visibility = 'visible';

		return true;
	}

	catch (err) {
		return true;
	}

}

function togglePasswordFieldClicked() {

	// var passwordField = document.getElementById('passwordField');
	var value = passwordField.value;

	if (passwordField.type == 'password') {
		passwordField.type = 'text';
		togglePasswordField.disabled = true;
	} else {
		// nah, let's not toggle it back
		// passwordField.type = 'password';
	}
	passwordField.value = value;

}

// password toggle
showPassword = false;

function enablePasswordDisplayOnEntry($el1, $el2) {
	// http://stackoverflow.com/questions/1948332/detect-all-changes-to-a-input-type-text-immediately-using-jquery
	console.debug('in enablePasswordDisplayEntryOn');
	jQuery('#' + $el1).each(
			function() {
				var elem = jQuery(this);

				// Save current value of element
				elem.data('oldVal', elem.val());

				// Look for changes in the value
				elem.bind("propertychange change click keyup input paste",
						function(event) {

							// If value has changed...
							if (elem.data('oldVal') != elem.val()) {
								// Updated stored value
								elem.data('oldVal', elem.val());

								if (!showPassword)
									showPassword = setupPasswordToggle($el1,
											$el2);
							}
						});
			});

}

jQuery('body').ajaxStart(function() {
	jQuery(this).css({
		'cursor' : 'wait'
	});
}).ajaxStop(function() {
	jQuery(this).css({
		'cursor' : 'default'
	});
});

function ajaxFailed(response) {
	if (response.responseText) {
		alert(postman_ajax_msg.bad_response + " "
				+ JSON.stringify(response, null, 4));
	}
}

// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {

	Array.prototype.forEach = function(callback, thisArg) {

		var T, k;

		if (this == null) {
			throw new TypeError(' this is null or not defined');
		}

		// 1. Let O be the result of calling ToObject passing the |this| value
		// as the argument.
		var O = Object(this);

		// 2. Let lenValue be the result of calling the Get internal method of O
		// with the argument "length".
		// 3. Let len be ToUint32(lenValue).
		var len = O.length >>> 0;

		// 4. If IsCallable(callback) is false, throw a TypeError exception.
		// See: http://es5.github.com/#x9.11
		if (typeof callback !== "function") {
			throw new TypeError(callback + ' is not a function');
		}

		// 5. If thisArg was supplied, let T be thisArg; else let T be
		// undefined.
		if (arguments.length > 1) {
			T = thisArg;
		}

		// 6. Let k be 0
		k = 0;

		// 7. Repeat, while k < len
		while (k < len) {

			var kValue;

			// a. Let Pk be ToString(k).
			// This is implicit for LHS operands of the in operator
			// b. Let kPresent be the result of calling the HasProperty internal
			// method of O with argument Pk.
			// This step can be combined with c
			// c. If kPresent is true, then
			if (k in O) {

				// i. Let kValue be the result of calling the Get internal
				// method of O with argument Pk.
				kValue = O[k];

				// ii. Call the Call internal method of callback with T as the
				// this value and
				// argument list containing kValue, k, and O.
				callback.call(T, kValue, k, O);
			}
			// d. Increase k by 1.
			k++;
		}
		// 8. return undefined
	};
}
function postmanValidateAjaxResponse(response) {
	if (response.data == undefined) {
		// handle corrupt response
		jQuery('#postman_test_message_status').html(
				postman_email_test.ajax_error);
		jQuery('#postman_test_message_status').css('color', 'red');
		jQuery('#postman_test_message_error_message').val(
				postman_ajax_msg.corrupt_response + ":\n\n" + response);
		jQuery('li + li').removeClass('disabled');
		return false;
	}
	return true;
}
function postmanValidateAjaxResponseWithPopup(response) {
	if (response.data == undefined) {
		alert(postman_ajax_msg.corrupt_response + ":\n\n"
				+ JSON.stringify(response, null, 4));
		return false;
	}
	return true;
}