xref: /plugin/bez/scripts/task.js (revision de02284c1e90f3c0d8df29c1c019b3ef912eafd9)
1bez.ctl.task = function() {
2    var $task_form = jQuery('.bez_task_form');
3
4    if ($task_form.length > 0) {
5        //date picker
6        jQuery("input[name=plan_date]").datepicker({
7            dateFormat: "yy-mm-dd"
8            });
9        if (jQuery("input[name=all_day_event]").is(":checked")) {
10            jQuery('#task_datapair').hide();
11        }
12        jQuery("input[name=all_day_event]").on('change', function() {
13            if (jQuery(this).is(":checked")) {
14                jQuery('#task_datapair').hide();
15            } else {
16                jQuery('#task_datapair').show();
17            }
18        });
19
20        //time picker
21        jQuery('#task_datapair .time').timepicker({
22                'showDuration': true,
23                'timeFormat': 'H:i'
24            });
25        var timeDatepair = new Datepair(jQuery('#task_datapair').get(0));
26        bez.rich_text_editor($task_form.find('textarea'), $task_form.find('.bez_toolbar'));
27    }
28
29    if (jQuery('#reason').length > 0) {
30        jQuery('#no_evaluation').on('change', function() {
31            if (jQuery(this).prop('checked') === true) {
32                jQuery('#reason').prop('disabled', true).hide();
33                jQuery('.bez_reason_toolbar').hide();
34            } else {
35                jQuery('#reason').prop('disabled', false).show();
36                jQuery('.bez_reason_toolbar').show();
37            }
38
39        });
40		bez.rich_text_editor(jQuery('#reason'), jQuery('.bez_reason_toolbar'));
41	}
42
43    jQuery('#bez_hidden_issue').hide();
44    jQuery('#bez_show_issue').on('click', function (e) {
45        e.preventDefault();
46        jQuery('#bez_hidden_issue').slideDown();
47        jQuery(this).hide();
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    //INVITE USERS
83    jQuery.widget( "custom.combobox", {
84      _create: function() {
85        this.wrapper = jQuery( "<span>" )
86          .addClass( "custom-combobox" )
87          .insertAfter( this.element );
88
89        this.element.hide();
90        this._createAutocomplete();
91        this._createShowAllButton();
92      },
93
94      _createAutocomplete: function() {
95        var selected = this.element.children( ":selected" ),
96          value = selected.val() ? selected.text() : "";
97
98        this.input = jQuery( "<input>" )
99          .appendTo( this.wrapper )
100          .val( value )
101          .attr( "title", "" )
102          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
103          .autocomplete({
104            delay: 0,
105            minLength: 0,
106            source: jQuery.proxy( this, "_source" )
107          })
108          .tooltip({
109            classes: {
110              "ui-tooltip": "ui-state-highlight"
111            }
112          });
113
114        this._on( this.input, {
115          autocompleteselect: function( event, ui ) {
116            ui.item.option.selected = true;
117            this._trigger( "select", event, {
118              item: ui.item.option
119            });
120          },
121
122          autocompletechange: "_removeIfInvalid"
123        });
124      },
125
126      _createShowAllButton: function() {
127        var input = this.input,
128          wasOpen = false;
129
130        jQuery( "<a>" )
131          .attr( "tabIndex", -1 )
132          .attr( "title", LANG.plugins.bez.combobox_show_all_items )
133          .tooltip()
134          .appendTo( this.wrapper )
135          .button({
136            icons: {
137              primary: "ui-icon-triangle-1-s"
138            },
139            text: false
140          })
141          .removeClass( "ui-corner-all" )
142          .addClass( "custom-combobox-toggle ui-corner-right" )
143          .on( "mousedown", function() {
144            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
145          })
146          .on( "click", function() {
147            input.trigger( "focus" );
148
149            // Close if already visible
150            if ( wasOpen ) {
151              return;
152            }
153
154            // Pass empty string as value to search for, displaying all results
155            input.autocomplete( "search", "" );
156          });
157      },
158
159      _source: function( request, response ) {
160        var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
161        response( this.element.children( "option" ).map(function() {
162          var text = jQuery( this ).text();
163          if ( this.value && ( !request.term || matcher.test(text) ) ) {
164
165            return {
166              label: text,
167              value: text,
168              option: this
169            };
170          }
171        }) );
172      },
173
174      _removeIfInvalid: function( event, ui ) {
175
176        // Selected an item, nothing to do
177        if ( ui.item ) {
178          return;
179        }
180
181        // Search for a match (case-insensitive)
182        var value = this.input.val(),
183          valueLowerCase = value.toLowerCase(),
184          valid = false;
185        this.element.children( "option" ).each(function() {
186          if ( jQuery( this ).text().toLowerCase() === valueLowerCase ) {
187            this.selected = valid = true;
188            return false;
189          }
190        });
191
192        // Found a match, nothing to do
193        if ( valid ) {
194          return;
195        }
196
197        // Remove invalid value
198        this.input
199          .val( "" )
200          .attr( "title", value + " " + LANG.plugins.bez.combobox_did_not_match )
201          .tooltip( "open" );
202        this.element.val( "" );
203        this._delay(function() {
204          this.input.tooltip( "close" ).attr( "title", "" );
205        }, 2500 );
206        this.input.autocomplete( "instance" ).term = "";
207      },
208
209      _destroy: function() {
210        this.wrapper.remove();
211        this.element.show();
212      }
213    });
214
215    jQuery( "#bez_invite_users select" ).combobox();
216    //INVITE
217};
218