summaryrefslogtreecommitdiff
path: root/lib/scripts/editor.js
blob: fbdb1d79a0c76e99c70b09af0592257b3830686b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * The DokuWiki editor features
 *
 * These are the advanced features of the editor. It does NOT contain any
 * code for the toolbar buttons and it functions. See toolbar.js for that.
 */

var dw_editor = {

    /**
     * initialize the default editor functionality
     *
     * All other functions can also be called separately for non-default
     * textareas
     */
    init: function(){
        var editor = jQuery('#wiki__text');
        if(!editor.length) return;

        dw_editor.initSizeCtl('#size__ctl',editor);
    },

    /**
     * Add the edit window size and wrap controls
     *
     * Initial values are read from cookie if it exists
     *
     * @param selector ctlarea the div to place the controls
     * @param selector editor  the textarea to control
     */
    initSizeCtl: function(ctlarea,editor){
        var ctl      = jQuery(ctlarea);
        var textarea = jQuery(editor);
        if(!ctl.length || !textarea.length) return;

        var hgt = DokuCookie.getValue('sizeCtl');
        if(hgt){
            textarea.css('height', hgt);
        }else{
            textarea.css('height', '300px');
        }

        var wrp = DokuCookie.getValue('wrapCtl');
        if(wrp){
            dw_editor.setWrap(textarea[0], wrp);
        } // else use default value

        var l = document.createElement('img');
        var s = document.createElement('img');
        var w = document.createElement('img');
        l.src = DOKU_BASE+'lib/images/larger.gif';
        s.src = DOKU_BASE+'lib/images/smaller.gif';
        w.src = DOKU_BASE+'lib/images/wrap.gif';
        jQuery(l).click(function(){dw_editor.sizeCtl(editor,100);});
        jQuery(s).click(function(){dw_editor.sizeCtl(editor,-100);});
        jQuery(w).click(function(){dw_editor.toggleWrap(editor);});
        ctl.append(l);
        ctl.append(s);
        ctl.append(w);
    },

    /**
     * This sets the vertical size of the editbox and adjusts the cookie
     *
     * @param selector editor  the textarea to control
     * @param int val          the relative value to resize in pixel
     */
    sizeCtl: function(editor,val){
        var textarea = jQuery(editor);
        var height = parseInt(textarea.css('height'));
        height += val;
        textarea.css('height', height+'px');
        DokuCookie.setValue('sizeCtl',textarea.css('height'));
    },

    /**
     * Toggle the wrapping mode of the editor textarea and adjusts the
     * cookie
     *
     * @param selector editor  the textarea to control
     */
    toggleWrap: function(editor){
        var textarea = jQuery(editor);
        var wrap = textarea.attr('wrap');
        if(wrap && wrap.toLowerCase() == 'off'){
            dw_editor.setWrap(textarea[0], 'soft');
        }else{
            dw_editor.setWrap(textarea[0], 'off');
        }
        DokuCookie.setValue('wrapCtl',textarea.attr('wrap'));
    },

    /**
     * Set the wrapping mode of a textarea
     *
     * @author Fluffy Convict <fluffyconvict@hotmail.com>
     * @author <shutdown@flashmail.com>
     * @link   http://news.hping.org/comp.lang.javascript.archive/12265.html
     * @link   https://bugzilla.mozilla.org/show_bug.cgi?id=41464
     * @param  DomObject textarea
     * @param  string wrapAttrValue
     */
    setWrap: function(textarea, wrapAttrValue){
        textarea.setAttribute('wrap', wrapAttrValue);

        // Fix display for mozilla
        var parNod = textarea.parentNode;
        var nxtSib = textarea.nextSibling;
        parNod.removeChild(textarea);
        parNod.insertBefore(textarea, nxtSib);
    }


};

jQuery(dw_editor.init);