Pecoes Wiki
Advertisement

//

var FindReplaceDialog = function () {
    
    function escapeForRegex (s) {
        return s.replace(/[-\/[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
    }
    
    function parseRegex (s) {
        var m = s.match(/^\/(.*)\/([gim]*)$/);
        if (!m) throw new Error('invalid regular expression');;
        var r = escapeForRegex(m[1]);
        try {
            return 3 == m.length ? new RegExp(r, m[2]) : new RegExp(r);
        } catch (e) {
            throw new Error('invalid regular expression');
        }
    }
    
    function smartCase (s) {
        return s.replace(/[a-z]/g, function (m) {
            return '[' + m[0].toUpperCase() + m[0] + ']'; 
        });
    }

    function evaluate (s) {
        if (isRegex) return parseRegex(s);
        var s = escapeForRegex(s);
        switch (caseType) {
            case SMARTCASE:
                return new RegExp(smartCase(s));
            case NOCASE:
                return new RegExp(s, 'i');
            case MATCHCASE:
                return new RegExp(s);
        }
    }
    
    function lint (s) {
        if (!/^\/.*\/[gim]*$/.test(s)) return false;
        try {
            var r = new RegExp(s);
        } catch (e) {
            return false;
        }
        return true;
    }
    
    var mode = 'find';
    
    var SMARTCASE = 1, NOCASE = 2, MATCHCASE = 3;
    
    var caseType = 1;
    var isRegex = false;

    var my = {
        
        dialog: null,
        
        load: function (callback) {
            
            if ($('#fr-ui').length) return;

            $.get('/index.php?title=MediaWiki:FindReplaceDialog&action=raw&ctype=text/html&usemsgcache=yes&maxage=86400', function (module, callback) {
                return function (data) {
                    $('body').append(data);
                    module.dialog = $('#fr-ui');
                    module.dialog.css({
                        left: 0, top: 0,
                        display: 'none'
                    });
                    
                    $('#fr-close').click(function (module) {
                        return function () {
                            module.close();
                        };
                    }(module));
                    
                    $('input[name="fr-case"]').click(function () {
                        caseType = parseInt($(this).val());
                    });
                    
                    $('input#fr-regex').click(function () {
                        isRegex = $(this).is(':checked');
                        if (isRegex) {
                            var r = $('input#fr-find-text').val();
                            if (!r.length) return;
                            $('input#fr-find-text').css('background-color', lint(r) ? 'green' : 'red');
                        } else {
                            $('input#fr-find-text').css('background-color', 'white');
                        }
                    });
                    
                    $('input#fr-toggle').click(function (module) {
                        return function () {
                            module.setMode($(this).is(':checked') ? 'replace' : 'find');
                        };
                    }(module));
                    
                    $('input#fr-replace-text').keypress(function (module) {
                        return function () {
                            module.setMode('replace');
                        }
                    }(module));
                    
                    $('input#fr-find-text').keyup(function () {
                        if (isRegex) {
                            $(this).css('background-color', lint($(this).val()) ? 'green' : 'red');
                        }
                    });
                    
                    callback(module.dialog);
                    
                    function waitForJQueryUI (dialog) {
                        if ('undefined' == typeof dialog.draggable) {
                            window.setTimeout(function () { waitForJQueryUI(dialog); }, 200);
                            return;
                        }
                        dialog.draggable();
                    }
                    waitForJQueryUI(module.dialog);
                };
            }(this, callback));
        },
        
        open: function (mode) {
            this.dialog.show('clip', 200);
            $('#fr-find-text').focus();
            this.setMode(mode);
        },
        
        close: function () {
            this.dialog.hide('clip', 200);
        },
        
        getMode: function () {
            return mode;
        },
        
        setMode: function (newMode) {
            switch (newMode) {
                case 'find':
                    mode = 'find';
                    $('#fr-title').html('Find');
                    $('#fr-toggle').attr('checked', false);
                    $('#fr-replace-text').val('').css('background-color', '#DDDDDD');
                    $('#fr-replace').attr('disabled', true);
                    $('#fr-replace-all').attr('disabled', true);
                    break;
                case 'replace':
                    mode = 'replace';
                    $('#fr-title').html('Replace');
                    $('#fr-toggle').attr('checked', true);
                    $('#fr-replace').attr('disabled', false);
                    $('#fr-replace-all').attr('disabled', false);
                    $('#fr-replace-text').css('background-color', '#FFFFFF');
                    break;
            }
        },
        
        log: function () {
            console.clear();
            var s = $('#fr-find-text').val();
            console.log('mode: %s', mode);
            console.log('search string: %s', s);
            console.log('replace string: %s', $('#fr-replace-text').val());
            switch(caseType) {
                case SMARTCASE: console.log('case type: smart case'); break;
                case NOCASE:    console.log('case type: no case'   ); break;
                case MATCHCASE: console.log('case type: match case'); break;
            }
            console.log('regex: %s', isRegex.toString());
            if (!s.length) return;
            var valid = true;
            if (isRegex) {
                valid = lint(s);
                console.log('regex is valid: %s', valid.toString());
            }
            if (valid) {
                console.log('search: %o', evaluate(s));
            }
        }
    };

    return my;
}();

$(function () {
    if ('edit' != $.getUrlVar('action')) return;

    var textarea = $('textarea#wpTextbox1');
    textarea.after('<div id="highlighter"></div>');
    var highlighter = $('#highlighter');

    importScriptURI('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js');
    
    FindReplaceDialog.load(function(dialog) {
        $('div#cke_toolbar_source_1 > *:last').before(
            '<img id="fr-icon-find" title="Find" src="http://images3.wikia.nocookie.net/__cb20120415071217/central/images/9/95/Search.png">'
        );
        $('#fr-icon-find').click(function () {
            FindReplaceDialog.open('find');
        });
        
        $('div#cke_toolbar_source_1 > *:last').before(
            '<img id="fr-icon-replace" title="Replace" src="http://images2.wikia.nocookie.net/__cb20120415071129/central/images/7/71/Replace.png">'
        );
        $('#fr-icon-replace').click(function () {
            FindReplaceDialog.open('replace');
        });
        
        if ($('.editpage-sourcewidemode').length) {
            $('#cke_toolbar_source_1').css('width', '340px');
            $('textarea#wpSummary').css('left', '48px');
            $('label.wpMinoredit').css('left', '330px');
        }
        
        dialog.css({
            left: textarea.width() + textarea.offset().left - dialog.width() + 100 + 'px',
            top: textarea.offset().top + 50 + 'px'
        });
    });

     var editFields = {
        width: '100%', left: 0, top: 0, border: '0 none', display: 'block',
        height: $(window).height() - textarea.offset().top - $('.toolbar').height() + 'px',
        fontFamily: 'Consolas,"Eupheima UCAS",Ayuthaya,Menlo,monospace',
        fontSize: '13.5px', outline: 'medium none', margin: 0, padding: 0, resize: 'none'
    };
    
    textarea.css(editFields).css({
        position: 'relative', zIndex: '1',
        color: 'transparent', backgroundColor: 'transparent'
    });
    
    highlighter.css(editFields).css({
        position: 'absolute', zIndex: '0'
    })
    
    function synchHighlighter () {
        highlighter.html(textarea.val().replace(/test/gi,
            '<span style="color: black; background-color: white">$&</span>'
        ));
    };
    
    textarea.keyup (synchHighlighter);
    textarea.change(synchHighlighter);
    synchHighlighter();
});
//
Advertisement