gii.js 3.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 () {
Thiago Talma committed
38
		$('.preview-code, .diff-code, .modal-refresh').on('click', function () {
Qiang Xue committed
39 40
			var $modal = $('#preview-modal');
			var $link = $(this);
41
			$modal.find('.modal-refresh').attr('href', $link.prop('href'));
Qiang Xue committed
42 43 44 45 46 47 48 49 50 51 52 53 54
			$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) {
					$modal.find('.modal-body').html(data);
					$modal.find('.content').css('max-height', ($(window).height() - 200) + 'px');
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
Alexander Makarov committed
55
					$modal.find('.modal-body').html('<div class="error">' + XMLHttpRequest.responseText + '</div>');
Qiang Xue committed
56
				}
Qiang Xue committed
57
			});
Qiang Xue committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
			return false;
		});
	};

	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
74 75 76 77 78 79 80 81 82
		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
83 84 85 86 87 88
		init: function () {
			initHintBlocks();
			initStickyInputs();
			initPreviewDiffLinks();
			initConfirmationCheckboxes();

89 90 91 92 93
			// 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();

94 95
			// hide Generate button if any input is changed
			$('.default-view .form-group input,select,textarea').change(function () {
Qiang Xue committed
96 97
				$('.default-view-results,.default-view-files').hide();
				$('.default-view button[name="generate"]').hide();
Qiang Xue committed
98
			});
99 100 101 102 103 104 105 106

			$('.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
107 108 109
		}
	};
})(jQuery);