gii.js 4.49 KB
Newer Older
Qiang Xue committed
1
yii.gii = (function ($) {
Qiang Xue committed
2 3 4 5 6
	var isActive = $('.default-view').length > 0;

	var initHintBlocks = function () {
		$('.hint-block').each(function () {
			var $hint = $(this);
Qiang Xue committed
7
			$hint.parent().find('label').addClass('help').popover({
Qiang Xue committed
8
				html: true,
Qiang Xue committed
9
				trigger: 'hover',
Qiang Xue committed
10 11
				placement: 'right',
				content: $hint.html()
Qiang Xue committed
12
			});
Qiang Xue committed
13 14
		});
	};
Qiang Xue committed
15

Qiang Xue committed
16
	var initStickyInputs = function () {
Qiang Xue committed
17
		$('.sticky:not(.error)').find('input[type="text"],select,textarea').each(function () {
Qiang Xue committed
18
			var value;
Alexander Makarov committed
19
			if (this.tagName === 'SELECT') {
Qiang Xue committed
20
				value = this.options[this.selectedIndex].text;
Alexander Makarov committed
21
			} else if (this.tagName === 'TEXTAREA') {
Qiang Xue committed
22 23 24 25
				value = $(this).html();
			} else {
				value = $(this).val();
			}
Alexander Makarov committed
26
			if (value === '') {
Qiang Xue committed
27 28 29 30
				value = '[empty]';
			}
			$(this).before('<div class="sticky-value">' + value + '</div>').hide();
		});
31
		$('.sticky-value').on('click', function () {
Qiang Xue committed
32 33 34 35 36 37
			$(this).hide();
			$(this).next().show().get(0).focus();
		});
	};

	var initPreviewDiffLinks = function () {
38
		$('.preview-code, .diff-code, .modal-refresh, .modal-previous, .modal-next').on('click', function () {
Qiang Xue committed
39 40
			var $modal = $('#preview-modal');
			var $link = $(this);
41 42 43 44
			$modal.find('.modal-refresh').attr('href', $link.attr('href'));
			if ($link.hasClass('preview-code') || $link.hasClass('diff-code')) {
				$modal.data('action', ($link.hasClass('preview-code') ? 'preview-code' : 'diff-code'))
			}
Qiang Xue committed
45 46 47 48 49 50 51 52 53
			$modal.find('.modal-title').text($link.data('title'));
			$modal.find('.modal-body').html('Loading ...');
			$modal.modal('show');
			$.ajax({
				type: 'POST',
				cache: false,
				url: $link.prop('href'),
				data: $('.default-view form').serializeArray(),
				success: function (data) {
54 55 56 57 58 59 60 61 62
					if (!$link.hasClass('modal-refresh')) {
						var filesSelector = 'a.' + $modal.data('action');
						var $files = $(filesSelector);
						var index = $files.filter('[href="' + $link.attr('href') + '"]').index(filesSelector);
						var $prev = $files.eq(index-1);
						var $next = $files.eq((index+1 == $files.length ? 0 : index+1));
						$modal.find('.modal-previous').attr('href', $prev.attr('href')).data('title', $prev.data('title'));
						$modal.find('.modal-next').attr('href', $next.attr('href')).data('title', $next.data('title'));
					}
Qiang Xue committed
63 64 65 66
					$modal.find('.modal-body').html(data);
					$modal.find('.content').css('max-height', ($(window).height() - 200) + 'px');
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
Alexander Makarov committed
67
					$modal.find('.modal-body').html('<div class="error">' + XMLHttpRequest.responseText + '</div>');
Qiang Xue committed
68
				}
Qiang Xue committed
69
			});
Qiang Xue committed
70 71
			return false;
		});
72 73 74 75 76 77 78 79 80 81

		$('#preview-modal').on('keydown', function(e) {
			if (e.keyCode === 37) {
				$('.modal-previous').trigger('click');
			} else if(e.keyCode === 39) {
				$('.modal-next').trigger('click');
			} else if(e.keyCode === 82) {
				$('.modal-refresh').trigger('click');
			}
		});
Qiang Xue committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
	};

	var initConfirmationCheckboxes = function () {
		var $checkAll = $('#check-all');
		$checkAll.click(function () {
			$('.default-view-files table .check input').prop('checked', this.checked);
		});
		$('.default-view-files table .check input').click(function () {
			$checkAll.prop('checked', !$('.default-view-files table .check input:not(:checked)').length);
		});
		$checkAll.prop('checked', !$('.default-view-files table .check input:not(:checked)').length);
	};

	return {
Antonio Ramirez committed
96 97 98 99 100 101 102 103 104
		autocomplete: function (counter, data) {
			var datum = new Bloodhound({
				datumTokenizer: function(d){return Bloodhound.tokenizers.whitespace(d.word);},
				queryTokenizer: Bloodhound.tokenizers.whitespace,
				local: data
			});
			datum.initialize();
			jQuery('.typeahead-'+counter).typeahead(null,{displayKey: 'word', source: datum.ttAdapter()});
		},
Qiang Xue committed
105 106 107 108 109 110
		init: function () {
			initHintBlocks();
			initStickyInputs();
			initPreviewDiffLinks();
			initConfirmationCheckboxes();

111 112 113 114 115
			// model generator: hide class name input when table name input contains *
			$('#model-generator #generator-tablename').on('change', function () {
				$('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1);
			}).change();

116 117
			// hide Generate button if any input is changed
			$('.default-view .form-group input,select,textarea').change(function () {
Qiang Xue committed
118 119
				$('.default-view-results,.default-view-files').hide();
				$('.default-view button[name="generate"]').hide();
Qiang Xue committed
120
			});
121 122 123 124 125 126 127 128

			$('.module-form #generator-moduleclass').change(function () {
				var value = $(this).val().match(/(\w+)\\\w+$/);
				var $idInput = $('#generator-moduleid');
				if (value && value[1] && $idInput.val() == '') {
					$idInput.val(value[1]);
				}
			});
Qiang Xue committed
129 130 131
		}
	};
})(jQuery);