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