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

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

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

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

Qiang Xue committed
83 84 85 86 87 88 89
        $('#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');
90 91
            } else if (e.keyCode === 32) {
                $('.modal-checkbox').trigger('click');
Qiang Xue committed
92 93
            }
        });
94 95 96 97 98 99 100 101 102 103 104

        $('.modal-checkbox').on('click', checkFileToggle);
    };

    var checkFileToggle = function () {
        var $modal = $('#preview-modal');
        var $checkbox = $modal.data('current').closest('tr').find('input');
        var checked = !$checkbox.prop('checked');
        $checkbox.prop('checked', checked);
        $modal.find('.modal-checkbox span').toggleClass('glyphicon-check', checked).toggleClass('glyphicon-unchecked', !checked);
        return false;
Qiang Xue committed
105
    };
Qiang Xue committed
106

107 108 109 110
    var checkAllToggle = function () {
        $('#check-all').prop('checked', !$('.default-view-files table .check input:enabled:not(:checked)').length);
    };

Qiang Xue committed
111 112 113
    var initConfirmationCheckboxes = function () {
        var $checkAll = $('#check-all');
        $checkAll.click(function () {
114
            $('.default-view-files table .check input:enabled').prop('checked', this.checked);
Qiang Xue committed
115 116
        });
        $('.default-view-files table .check input').click(function () {
117 118 119 120 121 122 123
            checkAllToggle();
        });
        checkAllToggle();
    };

    var initToggleActions = function () {
        $('#action-toggle :input').change(function () {
Thiago Talma committed
124
            $(this).parent('label').toggleClass('active', this.checked);
125 126
            $('.' + this.value, '.default-view-files table').toggle(this.checked).find('.check input').attr('disabled', !this.checked);
            checkAllToggle();
Qiang Xue committed
127 128
        });
    };
Qiang Xue committed
129

Qiang Xue committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    return {
        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()});
        },
        init: function () {
            initHintBlocks();
            initStickyInputs();
            initPreviewDiffLinks();
            initConfirmationCheckboxes();
147
            initToggleActions();
Qiang Xue committed
148

Qiang Xue committed
149
            // model generator: hide class name input when table name input contains *
Luciano Baraglia committed
150
            $('#model-generator #generator-tablename').change(function () {
Qiang Xue committed
151 152
                $('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1);
            }).change();
153

154 155 156
            // hide message category when I18N is disabled
            $('form #generator-enablei18n').change(function () {
                $('form .field-generator-messagecategory').toggle($(this).is(':checked'));
157 158
            }).change();

Qiang Xue committed
159 160 161 162 163
            // hide Generate button if any input is changed
            $('.default-view .form-group input,select,textarea').change(function () {
                $('.default-view-results,.default-view-files').hide();
                $('.default-view button[name="generate"]').hide();
            });
164

Qiang Xue committed
165 166 167 168 169 170 171 172 173
            $('.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
174
})(jQuery);