xref: /plugin/bez/scripts/thread.js (revision 53df74e7ac5ae4234aac1fa716a33878a039026f)
1
2bez.ctl.thread = function() {
3
4	jQuery('.bez_commcause_delete_prompt').click('on', function (event) {
5		if (!window.confirm(LANG.plugins.bez.remove_confirm)) {
6            event.preventDefault();
7		}
8	});
9
10	var $bez_comment_form = jQuery('.bez_comment_form'),
11		$tabs = $bez_comment_form.find('.bez_tabs');
12	if ($tabs.length > 0) {
13		var $links = $tabs.find('a'),
14			$active = $tabs.find('li.active a'),
15			active	= $active[0],
16			$comment_type_input = jQuery(".bez_comment_type"),
17			$cause_type_div = jQuery(".bez_cause_type");
18
19		var setActive = function($a) {
20			$tabs.find('.active').removeClass('active');
21			$a.parent().addClass('active');
22		};
23
24        var $action_buttons = $bez_comment_form.find("button[value=thread_close], button[value=thread_reject]");
25
26		var activateComment = function () {
27			$comment_type_input.removeAttr("disabled");
28			$cause_type_div.hide();
29			$cause_type_div.find("input").attr("disabled", "disabled");
30
31			$bez_comment_form.removeClass('bez_cause');
32            $action_buttons.show();
33		};
34
35		var activateCause = function() {
36			$comment_type_input.attr("disabled", "disabled");
37			$cause_type_div.show();
38			$cause_type_div.find("input").removeAttr("disabled");
39
40			$bez_comment_form.addClass('bez_cause');
41            $action_buttons.hide();
42		};
43
44		if (active.hash === '#comment') {
45			setActive($active);
46			activateComment();
47		} else if (active.hash === '#cause') {
48			setActive($active);
49			activateCause();
50		}
51
52
53		$tabs.on('click', 'a', function (e) {
54			e.preventDefault();
55			setActive(jQuery(this));
56		});
57
58		$tabs.on('click', 'a[href="#comment"]', activateComment);
59		$tabs.on('click', 'a[href="#cause"]', activateCause);
60	}
61    if ($bez_comment_form.length > 0) {
62		//textareas
63		var $textarea = $bez_comment_form.find("textarea");
64
65        var $close_button = $bez_comment_form.find("button[value=thread_close]");
66        var $reopen_button = $bez_comment_form.find("button[value=thread_reopen]");
67        var $reject_button = $bez_comment_form.find("button[value=thread_reject]");
68
69		var $header = $bez_comment_form.find(".bez_toolbar");
70		bez.rich_text_editor($textarea, $header);
71
72        $textarea.on('input', function() {
73            "use strict";
74            if (jQuery(this).val().length > 0) {
75                if ($close_button.length > 0) {
76                    $close_button.text(LANG.plugins.bez.comment_and_close_issue);
77                } else if($reopen_button.length > 0) {
78                    $reopen_button.text(LANG.plugins.bez.comment_and_reopen_issue);
79                } else {
80                    $reject_button.text(LANG.plugins.bez.comment_and_reject_issue);
81                }
82            } else {
83                if ($close_button.length > 0) {
84                    $close_button.text(LANG.plugins.bez.close_issue);
85                } else if($reopen_button.length > 0) {
86                    $reopen_button.text(LANG.plugins.bez.reopen_issue);
87                } else {
88                    $reject_button.text(LANG.plugins.bez.reject_issue);
89                }
90            }
91        });
92    }
93
94	//show/hide comments
95	var $show_comments = jQuery(".bez_show_comments"),
96		$hide_comments = jQuery(".bez_hide_comments");
97
98	var bez_show_comments = function() {
99		$show_comments.hide();
100		$hide_comments.show();
101		localStorage.setItem('comments_are_hidden', '0');
102		jQuery('.bez_type_0').show();
103	};
104
105	var bez_hide_comments = function() {
106		$hide_comments.hide();
107		$show_comments.show();
108		localStorage.setItem('comments_are_hidden', '1');
109		jQuery('.bez_type_0').hide();
110	};
111
112	var comments_are_hidden = localStorage.getItem('comments_are_hidden');
113	if (comments_are_hidden === null || comments_are_hidden === '0') {
114		bez_show_comments();
115	} else {
116		bez_hide_comments();
117	}
118
119	$show_comments.on('click', function(e) { e.preventDefault(); bez_show_comments() });
120	$hide_comments.on('click', function(e) { e.preventDefault(); bez_hide_comments() });
121
122	if (jQuery('.bez_task_form').length > 0) {
123		var $task_form = jQuery('.bez_task_form');
124		//date picker
125		jQuery("input[name=plan_date]").datepicker({
126			dateFormat: "yy-mm-dd"
127			});
128		if (jQuery("input[name=all_day_event]").is(":checked")) {
129			jQuery('#task_datapair').hide();
130		}
131		jQuery("input[name=all_day_event]").on('change', function() {
132			if (jQuery(this).is(":checked")) {
133				jQuery('#task_datapair').hide();
134			} else {
135				jQuery('#task_datapair').show();
136			}
137		});
138
139		//time picker
140		jQuery('#task_datapair .time').timepicker({
141				'showDuration': true,
142				'timeFormat': 'H:i'
143			});
144		var timeDatepair = new Datepair(jQuery('#task_datapair').get(0));
145
146		//cost
147		//~ jQuery('input[name=cost]').spinner({
148			//~ min: 0,
149			//~ max: 100000,
150			//~ step: 50
151		//~ });
152
153		bez.rich_text_editor($task_form.find('textarea'), $task_form.find('.bez_toolbar'));
154
155		//~ $task_form.validetta({
156
157		//~ });
158	}
159
160    if (jQuery('.bez_metaform').length > 0) {
161        var tooltips = jQuery('.bez_metaform').find("input, select").tooltip({
162                position: {
163                    my: "left top",
164                    at: "right+5 top-5",
165                    collision: "none"
166                }
167            });
168        jQuery.validate({
169            form: '.bez_metaform',
170            inlineErrorMessageCallback:  function($input, errorMessage, config) {
171                if ($input.tooltip("instance") === undefined) {
172                    return false;
173                }
174
175                if (errorMessage) {
176                    //customDisplayInlineErrorMessage($input, errorMessage);
177                    $input.attr('title', errorMessage);
178                    $input.tooltip("open");
179                } else {
180                    //customRemoveInlineError($input);
181                    $input.tooltip("disable");
182                }
183                return false; // prevent default behaviour
184            }
185        });
186
187        //~ if (jQuery('input[name="close_date"]').length > 0) {
188            //~ console.log(jQuery('.bez_timebox'));
189            //~ //https://github.com/jonthornton/Datepair.js#jquery-plugin
190            //~ jQuery('.bez_timebox').datepair({
191                //~ parseDate: function (el) {
192                    //~ var val = jQuery(el).datepicker('getDate');
193                    //~ if (!val) {
194                        //~ return null;
195                    //~ }
196                    //~ var utc = new Date(val);
197                    //~ return utc && new Date(utc.getTime() + (utc.getTimezoneOffset() * 60000));
198                //~ },
199                //~ updateDate: function (el, v) {
200                    //~ jQuery(el).datepicker('setDate', new Date(v.getTime() - (v.getTimezoneOffset() * 60000)));
201                //~ }
202            //~ });
203        //~ }
204
205        jQuery("input[name=date], input[name=close_date], input[name=last_mod], input[name=last_activity_date]").datepicker({
206			dateFormat: "yy-mm-dd"
207        });
208    }
209
210	if (jQuery('#opinion').length > 0) {
211		bez.rich_text_editor(jQuery('#opinion'), jQuery('.bez_opinion_toolbar'));
212	}
213
214	if (jQuery('#reason').length > 0) {
215        jQuery('#no_evaluation').on('change', function() {
216            if (jQuery(this).prop('checked') === true) {
217                jQuery('#reason').prop('disabled', true).hide();
218                jQuery('.bez_reason_toolbar').hide();
219            } else {
220                jQuery('#reason').prop('disabled', false).show();
221                jQuery('.bez_reason_toolbar').show();
222            }
223
224        });
225		bez.rich_text_editor(jQuery('#reason'), jQuery('.bez_reason_toolbar'));
226	}
227
228	//tooltips
229	jQuery(document).tooltip({
230		items: '#issue_participants a[title]',
231		position: {
232			my: "left top+15",
233			at: "left bottom",
234			collision: "flipfit"
235		},
236		content: function() {
237			var $this = jQuery(this);
238				name = $this.find('.bez_name').text(),
239				content = '<div style="margin-bottom: 3px;">'+name+'</div>';
240			$this.find('.bez_awesome').each(function() {
241				var $this = jQuery(this);
242				content += '<div>'+$this.get(0).outerHTML+' '+$this.attr('title')+'</div>';
243			});
244
245			return content;
246		}
247	});
248
249
250    //INVITE USERS
251    jQuery.widget( "custom.combobox", {
252      _create: function() {
253        this.wrapper = jQuery( "<span>" )
254          .addClass( "custom-combobox" )
255          .insertAfter( this.element );
256
257        this.element.hide();
258        this._createAutocomplete();
259        this._createShowAllButton();
260      },
261
262      _createAutocomplete: function() {
263        var selected = this.element.children( ":selected" ),
264          value = selected.val() ? selected.text() : "";
265
266        this.input = jQuery( "<input>" )
267          .appendTo( this.wrapper )
268          .val( value )
269          .attr( "title", "" )
270          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
271          .autocomplete({
272            delay: 0,
273            minLength: 0,
274            source: jQuery.proxy( this, "_source" )
275          })
276          .tooltip({
277            classes: {
278              "ui-tooltip": "ui-state-highlight"
279            }
280          });
281
282        this._on( this.input, {
283          autocompleteselect: function( event, ui ) {
284            ui.item.option.selected = true;
285            this._trigger( "select", event, {
286              item: ui.item.option
287            });
288          },
289
290          autocompletechange: "_removeIfInvalid"
291        });
292      },
293
294      _createShowAllButton: function() {
295        var input = this.input,
296          wasOpen = false;
297
298        jQuery( "<a>" )
299          .attr( "tabIndex", -1 )
300          .attr( "title", LANG.plugins.bez.combobox_show_all_items )
301          .tooltip()
302          .appendTo( this.wrapper )
303          .button({
304            icons: {
305              primary: "ui-icon-triangle-1-s"
306            },
307            text: false
308          })
309          .removeClass( "ui-corner-all" )
310          .addClass( "custom-combobox-toggle ui-corner-right" )
311          .on( "mousedown", function() {
312            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
313          })
314          .on( "click", function() {
315            input.trigger( "focus" );
316
317            // Close if already visible
318            if ( wasOpen ) {
319              return;
320            }
321
322            // Pass empty string as value to search for, displaying all results
323            input.autocomplete( "search", "" );
324          });
325      },
326
327      _source: function( request, response ) {
328        var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
329        response( this.element.children( "option" ).map(function() {
330          var text = jQuery( this ).text();
331          if ( this.value && ( !request.term || matcher.test(text) ) ) {
332
333            return {
334              label: text,
335              value: text,
336              option: this
337            };
338          }
339        }) );
340      },
341
342      _removeIfInvalid: function( event, ui ) {
343
344        // Selected an item, nothing to do
345        if ( ui.item ) {
346          return;
347        }
348
349        // Search for a match (case-insensitive)
350        var value = this.input.val(),
351          valueLowerCase = value.toLowerCase(),
352          valid = false;
353        this.element.children( "option" ).each(function() {
354          if ( jQuery( this ).text().toLowerCase() === valueLowerCase ) {
355            this.selected = valid = true;
356            return false;
357          }
358        });
359
360        // Found a match, nothing to do
361        if ( valid ) {
362          return;
363        }
364
365        // Remove invalid value
366        this.input
367          .val( "" )
368          .attr( "title", value + " " + LANG.plugins.bez.combobox_did_not_match )
369          .tooltip( "open" );
370        this.element.val( "" );
371        this._delay(function() {
372          this.input.tooltip( "close" ).attr( "title", "" );
373        }, 2500 );
374        this.input.autocomplete( "instance" ).term = "";
375      },
376
377      _destroy: function() {
378        this.wrapper.remove();
379        this.element.show();
380      }
381    });
382
383    jQuery( "#bez_invite_users select" ).combobox();
384    //INVITE
385};
386