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