xref: /dokuwiki/lib/scripts/editor.js (revision 99e7bfd4c733d81cd01dd2d97e10bb4dda62df66)
1/**
2 * The DokuWiki editor features
3 *
4 * These are the advanced features of the editor. It does NOT contain any
5 * code for the toolbar buttons and it functions. See toolbar.js for that.
6 */
7
8var dw_editor = {
9
10    /**
11     * initialize the default editor functionality
12     *
13     * All other functions can also be called separately for non-default
14     * textareas
15     */
16    init: function(){
17        var editor = jQuery('#wiki__text');
18        if(!editor.length) return;
19
20        dw_editor.initSizeCtl('#size__ctl',editor);
21    },
22
23    /**
24     * Add the edit window size and wrap controls
25     *
26     * Initial values are read from cookie if it exists
27     *
28     * @param selector ctlarea the div to place the controls
29     * @param selector editor  the textarea to control
30     */
31    initSizeCtl: function(ctlarea,editor){
32        var ctl      = jQuery(ctlarea);
33        var textarea = jQuery(editor);
34        if(!ctl.length || !textarea.length) return;
35
36        var hgt = DokuCookie.getValue('sizeCtl');
37        if(hgt){
38            textarea.css('height', hgt);
39        }else{
40            textarea.css('height', '300px');
41        }
42
43        var wrp = DokuCookie.getValue('wrapCtl');
44        if(wrp){
45            dw_editor.setWrap(textarea[0], wrp);
46        } // else use default value
47
48        var l = document.createElement('img');
49        var s = document.createElement('img');
50        var w = document.createElement('img');
51        l.src = DOKU_BASE+'lib/images/larger.gif';
52        s.src = DOKU_BASE+'lib/images/smaller.gif';
53        w.src = DOKU_BASE+'lib/images/wrap.gif';
54        jQuery(l).click(function(){dw_editor.sizeCtl(editor,100);});
55        jQuery(s).click(function(){dw_editor.sizeCtl(editor,-100);});
56        jQuery(w).click(function(){dw_editor.toggleWrap(editor);});
57        ctl.append(l);
58        ctl.append(s);
59        ctl.append(w);
60    },
61
62    /**
63     * This sets the vertical size of the editbox and adjusts the cookie
64     *
65     * @param selector editor  the textarea to control
66     * @param int val          the relative value to resize in pixel
67     */
68    sizeCtl: function(editor,val){
69        var textarea = jQuery(editor);
70        var height = parseInt(textarea.css('height'));
71        height += val;
72        textarea.css('height', height+'px');
73        DokuCookie.setValue('sizeCtl',textarea.css('height'));
74    },
75
76    /**
77     * Toggle the wrapping mode of the editor textarea and adjusts the
78     * cookie
79     *
80     * @param selector editor  the textarea to control
81     */
82    toggleWrap: function(editor){
83        var textarea = jQuery(editor);
84        var wrap = textarea.attr('wrap');
85        if(wrap && wrap.toLowerCase() == 'off'){
86            dw_editor.setWrap(textarea[0], 'soft');
87        }else{
88            dw_editor.setWrap(textarea[0], 'off');
89        }
90        DokuCookie.setValue('wrapCtl',textarea.attr('wrap'));
91    },
92
93    /**
94     * Set the wrapping mode of a textarea
95     *
96     * @author Fluffy Convict <fluffyconvict@hotmail.com>
97     * @author <shutdown@flashmail.com>
98     * @link   http://news.hping.org/comp.lang.javascript.archive/12265.html
99     * @link   https://bugzilla.mozilla.org/show_bug.cgi?id=41464
100     * @param  DomObject textarea
101     * @param  string wrapAttrValue
102     */
103    setWrap: function(textarea, wrapAttrValue){
104        textarea.setAttribute('wrap', wrapAttrValue);
105
106        // Fix display for mozilla
107        var parNod = textarea.parentNode;
108        var nxtSib = textarea.nextSibling;
109        parNod.removeChild(textarea);
110        parNod.insertBefore(textarea, nxtSib);
111    }
112
113
114};
115
116jQuery(dw_editor.init);
117