xref: /plugin/bez/scripts/task.js (revision 53df74e7ac5ae4234aac1fa716a33878a039026f)
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        $textarea.on('input', function() {
93            "use strict";
94            if (jQuery(this).val().length > 0) {
95                if ($do_button.length > 0) {
96                    $do_button.text(LANG.plugins.bez.comment_and_do_task);
97                } else {
98                    $reopen_button.text(LANG.plugins.bez.comment_and_reopen_task);
99                }
100            } else {
101                if ($do_button.length > 0) {
102                    $do_button.text(LANG.plugins.bez.do_task);
103                } else {
104                    $reopen_button.text(LANG.plugins.bez.reopen_task);
105                }
106            }
107        });
108    }
109
110    //tooltips
111    jQuery(document).tooltip({
112        items: '#issue_participants a[title]',
113        position: {
114            my: "left top+15",
115            at: "left bottom",
116            collision: "flipfit"
117        },
118        content: function() {
119            var $this = jQuery(this);
120            name = $this.find('.bez_name').text(),
121                content = '<div style="margin-bottom: 3px;">'+name+'</div>';
122            $this.find('.bez_awesome').each(function() {
123                var $this = jQuery(this);
124                content += '<div>'+$this.get(0).outerHTML+' '+$this.attr('title')+'</div>';
125            });
126
127            return content;
128        }
129    });
130
131    //INVITE USERS
132    jQuery.widget( "custom.combobox", {
133      _create: function() {
134        this.wrapper = jQuery( "<span>" )
135          .addClass( "custom-combobox" )
136          .insertAfter( this.element );
137
138        this.element.hide();
139        this._createAutocomplete();
140        this._createShowAllButton();
141      },
142
143      _createAutocomplete: function() {
144        var selected = this.element.children( ":selected" ),
145          value = selected.val() ? selected.text() : "";
146
147        this.input = jQuery( "<input>" )
148          .appendTo( this.wrapper )
149          .val( value )
150          .attr( "title", "" )
151          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
152          .autocomplete({
153            delay: 0,
154            minLength: 0,
155            source: jQuery.proxy( this, "_source" )
156          })
157          .tooltip({
158            classes: {
159              "ui-tooltip": "ui-state-highlight"
160            }
161          });
162
163        this._on( this.input, {
164          autocompleteselect: function( event, ui ) {
165            ui.item.option.selected = true;
166            this._trigger( "select", event, {
167              item: ui.item.option
168            });
169          },
170
171          autocompletechange: "_removeIfInvalid"
172        });
173      },
174
175      _createShowAllButton: function() {
176        var input = this.input,
177          wasOpen = false;
178
179        jQuery( "<a>" )
180          .attr( "tabIndex", -1 )
181          .attr( "title", LANG.plugins.bez.combobox_show_all_items )
182          .tooltip()
183          .appendTo( this.wrapper )
184          .button({
185            icons: {
186              primary: "ui-icon-triangle-1-s"
187            },
188            text: false
189          })
190          .removeClass( "ui-corner-all" )
191          .addClass( "custom-combobox-toggle ui-corner-right" )
192          .on( "mousedown", function() {
193            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
194          })
195          .on( "click", function() {
196            input.trigger( "focus" );
197
198            // Close if already visible
199            if ( wasOpen ) {
200              return;
201            }
202
203            // Pass empty string as value to search for, displaying all results
204            input.autocomplete( "search", "" );
205          });
206      },
207
208      _source: function( request, response ) {
209        var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
210        response( this.element.children( "option" ).map(function() {
211          var text = jQuery( this ).text();
212          if ( this.value && ( !request.term || matcher.test(text) ) ) {
213
214            return {
215              label: text,
216              value: text,
217              option: this
218            };
219          }
220        }) );
221      },
222
223      _removeIfInvalid: function( event, ui ) {
224
225        // Selected an item, nothing to do
226        if ( ui.item ) {
227          return;
228        }
229
230        // Search for a match (case-insensitive)
231        var value = this.input.val(),
232          valueLowerCase = value.toLowerCase(),
233          valid = false;
234        this.element.children( "option" ).each(function() {
235          if ( jQuery( this ).text().toLowerCase() === valueLowerCase ) {
236            this.selected = valid = true;
237            return false;
238          }
239        });
240
241        // Found a match, nothing to do
242        if ( valid ) {
243          return;
244        }
245
246        // Remove invalid value
247        this.input
248          .val( "" )
249          .attr( "title", value + " " + LANG.plugins.bez.combobox_did_not_match )
250          .tooltip( "open" );
251        this.element.val( "" );
252        this._delay(function() {
253          this.input.tooltip( "close" ).attr( "title", "" );
254        }, 2500 );
255        this.input.autocomplete( "instance" ).term = "";
256      },
257
258      _destroy: function() {
259        this.wrapper.remove();
260        this.element.show();
261      }
262    });
263
264    jQuery( "#bez_invite_users select" ).combobox();
265    //INVITE
266};
267