authchoice.js 1.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/**
 * Yii auth choice widget.
 *
 * This is the JavaScript widget used by the yii\authclient\widgets\Choice widget.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
jQuery(function($) {
	$.fn.authchoice = function(options) {
		options = $.extend({
			popup: {
				resizable: 'yes',
				scrollbars: 'no',
				toolbar: 'no',
				menubar: 'no',
				location: 'no',
				directories: 'no',
				status: 'yes',
				width: 450,
				height: 380
			}
		}, options);

		return this.each(function() {
29
			var $container = $(this);
30

31
			$container.find('a').on('click', function(e) {
32
				e.preventDefault();
33 34 35 36

				var authChoicePopup = null;

				if (authChoicePopup = $container.data('authChoicePopup')) {
37 38
					authChoicePopup.close();
				}
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
				var url = this.href;
				var popupOptions = options.popup;

				var localPopupWidth = this.getAttribute('data-popup-width');
				if (localPopupWidth) {
					popupOptions.width = localPopupWidth;
				}
				var localPopupHeight = this.getAttribute('data-popup-height');
				if (localPopupWidth) {
					popupOptions.height = localPopupHeight;
				}

				popupOptions.left = (window.screen.width - options.popup.width) / 2;
				popupOptions.top = (window.screen.height - options.popup.height) / 2;

				var popupFeatureParts = [];
				for (var propName in popupOptions) {
					popupFeatureParts.push(propName + '=' + popupOptions[propName]);
				}
				var popupFeature = popupFeatureParts.join(',');

				authChoicePopup = window.open(url, 'yii_auth_choice', popupFeature);
				authChoicePopup.focus();
63 64

				$container.data('authChoicePopup', authChoicePopup);
65 66 67 68
			});
		});
	};
});