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