1bez.ctl.task = function() {
2
3    jQuery('.bez_commcause_delete_prompt').click('on', function (event) {
4        if (!window.confirm(LANG.plugins.bez.remove_confirm)) {
5            event.preventDefault();
6        }
7    });
8
9    var $task_form = jQuery('.bez_task_form');
10
11    if ($task_form.length > 0) {
12        //date picker
13        jQuery("input[name=plan_date]").datepicker({
14            dateFormat: "yy-mm-dd"
15            });
16        if (jQuery("input[name=all_day_event]").is(":checked")) {
17            jQuery('#task_datapair').hide();
18        }
19        jQuery("input[name=all_day_event]").on('change', function() {
20            if (jQuery(this).is(":checked")) {
21                jQuery('#task_datapair').hide();
22            } else {
23                jQuery('#task_datapair').show();
24            }
25        });
26
27        //time picker
28        jQuery('#task_datapair .time').timepicker({
29                'showDuration': true,
30                'timeFormat': 'H:i'
31            });
32        var timeDatepair = new Datepair(jQuery('#task_datapair').get(0));
33        bez.rich_text_editor($task_form.find('textarea'), $task_form.find('.bez_toolbar'));
34    }
35
36    if (jQuery('#reason').length > 0) {
37        jQuery('#no_evaluation').on('change', function() {
38            if (jQuery(this).prop('checked') === true) {
39                jQuery('#reason').prop('disabled', true).hide();
40                jQuery('.bez_reason_toolbar').hide();
41            } else {
42                jQuery('#reason').prop('disabled', false).show();
43                jQuery('.bez_reason_toolbar').show();
44            }
45
46        });
47		bez.rich_text_editor(jQuery('#reason'), jQuery('.bez_reason_toolbar'));
48	}
49
50    if (jQuery('.bez_metaform').length > 0) {
51        var tooltips = jQuery('.bez_metaform').find("input, select").tooltip({
52                position: {
53                    my: "left top",
54                    at: "right+5 top-5",
55                    collision: "none"
56                }
57            });
58        jQuery.validate({
59            form: '.bez_metaform',
60            inlineErrorMessageCallback:  function($input, errorMessage, config) {
61                if ($input.tooltip("instance") === undefined) {
62                    return false;
63                }
64
65                if (errorMessage) {
66                    //customDisplayInlineErrorMessage($input, errorMessage);
67                    $input.attr('title', errorMessage);
68                    $input.tooltip("open");
69                } else {
70                    //customRemoveInlineError($input);
71                    $input.tooltip("disable");
72                }
73                return false; // prevent default behaviour
74            }
75        });
76
77        jQuery("input[name=date], input[name=close_date]").datepicker({
78			dateFormat: "yy-mm-dd"
79        });
80    }
81
82    var $bez_comment_form = jQuery('.bez_comment_form');
83    if ($bez_comment_form.length > 0) {
84        //textareas
85        var $textarea = $bez_comment_form.find("textarea");
86        var $do_button = $bez_comment_form.find("button[value=task_do]");
87        var $reopen_button = $bez_comment_form.find("button[value=task_reopen]");
88
89        var $header = $bez_comment_form.find(".bez_toolbar");
90        bez.rich_text_editor($textarea, $header);
91    }
92
93    //tooltips
94    jQuery(document).tooltip({
95        items: '#issue_participants a[title]',
96        position: {
97            my: "left top+15",
98            at: "left bottom",
99            collision: "flipfit"
100        },
101        content: function() {
102            var $this = jQuery(this),
103                name = $this.find('.bez_name').text(),
104                content = '<div style="margin-bottom: 3px;">'+name+'</div>';
105            $this.find('.bez_awesome').each(function() {
106                var $this = jQuery(this);
107                content += '<div>'+$this.get(0).outerHTML+' '+$this.attr('title')+'</div>';
108            });
109
110            return content;
111        }
112    });
113
114    jQuery("#plugin__bez_do_task_button").click(function() {
115        "use strict";
116        jQuery("button[value=task_do], button[value=task_reopen]").effect("highlight");
117    });
118
119    jQuery('#issue_participants .participant_remove').hide().click('on', function (event) {
120        if (!window.confirm(LANG.plugins.bez.remove_confirm)) {
121            event.preventDefault();
122        }
123    });
124    jQuery('#issue_participants li').hover(
125        function() {
126            "use strict";
127            jQuery(this).find('.participant_remove').show();
128        },
129        function() {
130            "use strict";
131            jQuery(this).find('.participant_remove').hide();
132        }
133    );
134
135    //INVITE USERS
136    jQuery.widget( "custom.inviteUsersCombobox", {
137      _create: function() {
138        this.wrapper = jQuery( "<span>" )
139          .addClass( "custom-inviteUsersCombobox" )
140          .insertAfter( this.element );
141
142        this.element.hide();
143        this._createAutocomplete();
144        this._createShowAllButton();
145      },
146
147      _createAutocomplete: function() {
148        var selected = this.element.children( ":selected" ),
149          value = selected.val() ? selected.text() : "";
150
151        this.input = jQuery( "<input>" )
152          .appendTo( this.wrapper )
153          .val( value )
154          .attr( "title", "" )
155          .addClass( "custom-inviteUsersCombobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
156          .autocomplete({
157            delay: 0,
158            minLength: 0,
159            source: jQuery.proxy( this, "_source" )
160          })
161          .tooltip({
162            classes: {
163              "ui-tooltip": "ui-state-highlight"
164            }
165          });
166
167        this._on( this.input, {
168          autocompleteselect: function( event, ui ) {
169            ui.item.option.selected = true;
170            this._trigger( "select", event, {
171              item: ui.item.option
172            });
173          },
174
175          autocompletechange: "_removeIfInvalid"
176        });
177      },
178
179      _createShowAllButton: function() {
180        var input = this.input,
181          wasOpen = false;
182
183        jQuery( "<a>" )
184          .attr( "tabIndex", -1 )
185          .attr( "title", LANG.plugins.bez.combobox_show_all_items )
186          .tooltip()
187          .appendTo( this.wrapper )
188          .button({
189            icons: {
190              primary: "ui-icon-triangle-1-s"
191            },
192            text: false
193          })
194          .removeClass( "ui-corner-all" )
195          .addClass( "custom-inviteUsersCombobox-toggle ui-corner-right" )
196          .on( "mousedown", function() {
197            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
198          })
199          .on( "click", function() {
200            input.trigger( "focus" );
201
202            // Close if already visible
203            if ( wasOpen ) {
204              return;
205            }
206
207            // Pass empty string as value to search for, displaying all results
208            input.autocomplete( "search", "" );
209          });
210      },
211
212      _source: function( request, response ) {
213        var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
214        response( this.element.children( "option" ).map(function() {
215          var text = jQuery( this ).text();
216          if ( this.value && ( !request.term || matcher.test(text) ) ) {
217
218            return {
219              label: text,
220              value: text,
221              option: this
222            };
223          }
224        }) );
225      },
226
227      _removeIfInvalid: function( event, ui ) {
228
229        // Selected an item, nothing to do
230        if ( ui.item ) {
231          return;
232        }
233
234        // Search for a match (case-insensitive)
235        var value = this.input.val(),
236          valueLowerCase = value.toLowerCase(),
237          valid = false;
238        this.element.children( "option" ).each(function() {
239          if ( jQuery( this ).text().toLowerCase() === valueLowerCase ) {
240            this.selected = valid = true;
241            return false;
242          }
243        });
244
245        // Found a match, nothing to do
246        if ( valid ) {
247          return;
248        }
249
250        // Remove invalid value
251        this.input
252          .val( "" )
253          .attr( "title", value + " " + LANG.plugins.bez.combobox_did_not_match )
254          .tooltip( "open" );
255        this.element.val( "" );
256        this._delay(function() {
257          this.input.tooltip( "close" ).attr( "title", "" );
258        }, 2500 );
259        this.input.autocomplete( "instance" ).term = "";
260      },
261
262      _destroy: function() {
263        this.wrapper.remove();
264        this.element.show();
265      }
266    });
267
268    jQuery( "#bez_invite_users select" ).inviteUsersCombobox();
269    //INVITE
270
271    if (jQuery('#plugin__bez_task_pin_form').length > 0) {
272        var $form = jQuery('#plugin__bez_task_pin_form'),
273            $label = $form.find('label'),
274            $org_button = $form.find('#plugin__bez_pin_to_the_issue');
275
276        //this button is hidden by default
277        $label.find('button').show();
278
279        //hide entire form
280        $label.hide();
281        $org_button.click(function (e) {
282            e.preventDefault();
283            jQuery(this).hide();
284            $label.show();
285        })
286    }
287
288    var $contentTextarea = jQuery('.bez_comment textarea[name=content]');
289    if ($contentTextarea.length > 0) {
290        var $doTaskButton = jQuery('button[name=fn][value=task_do]'),
291            $addCommentButton = jQuery('button[name=fn][value=comment_add]');
292        $contentTextarea.on('keyup', function () {
293            if ($contentTextarea.val() === '') {
294                $doTaskButton.html(LANG.plugins.bez.close_without_comment);
295                $addCommentButton.hide();
296            } else {
297                $doTaskButton.html(LANG.plugins.bez.close_with_comment);
298                $addCommentButton.show();
299            }
300
301        });
302    }
303
304};
305