1/**
2 * Required toolbar functions for the TFSLink plugin
3 *
4 * @author   Thorsten Klingert <Thorsten.Klingert@gmail.com>
5 * based on Link Wizard
6 *      @author Andreas Gohr <gohr@cosmocode.de>
7 *      @author Pierre Spring <pierre.spring@caillou.ch>
8 * based on IndexMenu wizard
9 *      @author Gerrit Uitslag
10 */
11var addworkitemlink_wiz = {
12    $wiz: null,
13    textArea: null,
14    justSyncingSelection: false,
15
16    init: function($editor) {
17        // position relative to the text area
18        var pos = $editor.position();
19        // create HTML Structure
20        if (addworkitemlink_wiz.$wiz)
21            return;
22        addworkitemlink_wiz.$wiz = jQuery(document.createElement('div'))
23            .dialog({
24                autoOpen: false,
25                draggable: true,
26                title: LANG.plugins.tfslink.addlinktitle,
27                resizable: false
28            })
29            .html(
30                '<fieldset class="attributes"><legend>'+LANG.plugins.tfslink.attributes+'</legend>' +
31                '<div><label class="number">'+LANG.plugins.tfslink.workitemid+' : # <input id="workitemid" type="text"></label>' +
32                '<label>'+LANG.plugins.tfslink.title+' : <input id="title" type="text"></label></div>' +
33                '</fieldset>' +
34                '<fieldset class="projectcollection"><legend>'+LANG.plugins.tfslink.projectcollection+'</legend>' +
35                '<div><label>'+LANG.plugins.tfslink.usedefaultprojectcollection+'</label><input type="checkbox" id="usedefaultprojectcollection" name="default" value="1" checked>' +
36                '<div><select id="projectcollection" name="projectcollection" size="3"></select></div>' +
37                '</fieldset>' +
38                '<input type="submit" value="'+LANG.plugins.tfslink.insertlink+'" class="button" id="addworkitemlink__insert">'
39                )
40            .parent()
41            .attr('id','addworkitemlink__wiz')
42            .css({
43                'position':    'absolute',
44                'top':         (pos.top+20)+'px',
45                'left':        (pos.left+80)+'px'
46            })
47            .hide()
48            .appendTo('.dokuwiki:first');
49
50        addworkitemlink_wiz.textArea = $editor[0];
51
52        addworkitemlink_wiz.populateProjectCollections();
53
54        // attach event handlers
55        jQuery('#workitemid').bind('keydown keyup', function(){
56            // restrict input to numbers only
57            addworkitemlink_wiz.filterNumberInput(this);
58        });
59        jQuery("#usedefaultprojectcollection").bind('change', function() {
60            addworkitemlink_wiz.syncProjectCollectionSelections(true);
61        });
62        jQuery('#projectcollection').bind('change', function () {
63            addworkitemlink_wiz.syncProjectCollectionSelections(false);
64        });
65        jQuery('#addworkitemlink__insert').click(addworkitemlink_wiz.insertLink);
66
67        jQuery('#addworkitemlink__wiz').find('.ui-dialog-titlebar-close').click(addworkitemlink_wiz.hide);
68    },
69    /**
70     * Allow only number, by direct removing other characters from input
71     */
72    filterNumberInput: function(elem){
73        if(elem.value.match(/\D/)) {
74            elem.value=this.value.replace(/\D/g,'');
75        }
76    },
77    /**
78     * Populates the project collections
79     */
80    populateProjectCollections: function() {
81        var addOptions = function(data) {
82            if (jQuery.isEmptyObject(data)) {
83                // hide project collection options and check 'use default'
84                jQuery('fieldset.projectcollection').hide();
85                jQuery('#usedefaultprojectcollection').prop('checked', true);
86                return;
87            }
88
89            jQuery.each(data, function(i, pc){
90                if (!pc) return;
91                jQuery('#projectcollection').append( jQuery('<option>', { value: pc.id, text: pc.title }));
92            });
93        };
94
95        jQuery.post(
96            DOKU_BASE + 'lib/exe/ajax.php',
97            {call: 'tfslink', req: 'projectcollections'},
98            addOptions,
99            'json'
100        );
101    },
102    syncProjectCollectionSelections: function(fromCheckbox){
103        if (addworkitemlink_wiz.justSyncingSelection) return;
104        addworkitemlink_wiz.justSyncingSelection = true;
105        try {
106            var $checkbox = jQuery('#usedefaultprojectcollection');
107            if (fromCheckbox) {
108                if($checkbox.prop('checked')) {
109                    var $selected = jQuery('#projectcollection').find("option:selected");
110                    if ($selected) $selected.prop('selected', false);
111                    return;
112                }
113                var firstOption = jQuery('#projectcollection').find("option:first");
114                if (!firstOption) {
115                    $checkbox.prop('checked', true);
116                    return;
117                }
118                firstOption.prop('selected', true);
119            } else {
120                var $selected = jQuery('#projectcollection').find("option:selected");
121                $checkbox.prop('checked', !$selected);
122            }
123        } finally
124        {
125            addworkitemlink_wiz.justSyncingSelection = false;
126        }
127    },
128
129    /**
130     * Inserts the syntax for the currently configured link
131     */
132    insertLink: function(){
133        var sel, pc, id, title, syntax;
134
135        // XXX: Compatibility Fix for 2014-05-05 "Ponder Stibbons", splitbrain/dokuwiki#505
136        if(DWgetSelection) {
137            sel = DWgetSelection(addworkitemlink_wiz.textArea);
138        } else {
139            sel = getSelection(addworkitemlink_wiz.textArea);
140        }
141
142        pc = '';
143        if( !jQuery('#usedefaultprojectcollection').prop('checked')) {
144            pc = jQuery('#projectcollection').find("option:selected").val();
145            if (!pc) pc = '';
146        }
147        id = parseInt(jQuery('#workitemid').val());
148        title = jQuery('#title').val();
149
150        syntax = '[[wi>'+ pc + '#' + (id ? id : '-1');
151        if (title && title.length > 0)
152            syntax += '|' + title;
153        syntax+=']]';
154
155        pasteText(sel, syntax,{startofs: 5, endofs: 2});
156        addworkitemlink_wiz.hide();
157    },
158    /**
159     * Shows the wizard
160     */
161    show: function(){
162        // XXX: Compatibility Fix for 2014-05-05 "Ponder Stibbons", splitbrain/dokuwiki#505
163        if(DWgetSelection) {
164            addworkitemlink_wiz.selection = DWgetSelection(addworkitemlink_wiz.textArea);
165        } else {
166            addworkitemlink_wiz.selection = getSelection(addworkitemlink_wiz.textArea);
167        }
168
169        addworkitemlink_wiz.$wiz.show();
170        jQuery('#workitemid').focus();
171    },
172    /**
173     * Hides the wizard
174     */
175    hide: function(){
176        addworkitemlink_wiz.$wiz.hide();
177        addworkitemlink_wiz.textArea.focus();
178    },
179    /**
180     * Toggles the visibility of the wizard
181     */
182    toggle: function(){
183        if(addworkitemlink_wiz.$wiz.css('display') == 'none'){
184            addworkitemlink_wiz.show();
185        }else{
186            addworkitemlink_wiz.hide();
187        }
188    }
189};
190