xref: /plugin/bez/scripts/thread.js (revision 6f38077355681f273cc9db672efab6fff436baea)
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
175    //INVITE USERS
176    jQuery.widget( "custom.combobox", {
177      _create: function() {
178        this.wrapper = jQuery( "<span>" )
179          .addClass( "custom-combobox" )
180          .insertAfter( this.element );
181
182        this.element.hide();
183        this._createAutocomplete();
184        this._createShowAllButton();
185      },
186
187      _createAutocomplete: function() {
188        var selected = this.element.children( ":selected" ),
189          value = selected.val() ? selected.text() : "";
190
191        this.input = jQuery( "<input>" )
192          .appendTo( this.wrapper )
193          .val( value )
194          .attr( "title", "" )
195          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
196          .autocomplete({
197            delay: 0,
198            minLength: 0,
199            source: jQuery.proxy( this, "_source" )
200          })
201          .tooltip({
202            classes: {
203              "ui-tooltip": "ui-state-highlight"
204            }
205          });
206
207        this._on( this.input, {
208          autocompleteselect: function( event, ui ) {
209            ui.item.option.selected = true;
210            this._trigger( "select", event, {
211              item: ui.item.option
212            });
213          },
214
215          autocompletechange: "_removeIfInvalid"
216        });
217      },
218
219      _createShowAllButton: function() {
220        var input = this.input,
221          wasOpen = false;
222
223        jQuery( "<a>" )
224          .attr( "tabIndex", -1 )
225          .attr( "title", LANG.plugins.bez.combobox_show_all_items )
226          .tooltip()
227          .appendTo( this.wrapper )
228          .button({
229            icons: {
230              primary: "ui-icon-triangle-1-s"
231            },
232            text: false
233          })
234          .removeClass( "ui-corner-all" )
235          .addClass( "custom-combobox-toggle ui-corner-right" )
236          .on( "mousedown", function() {
237            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
238          })
239          .on( "click", function() {
240            input.trigger( "focus" );
241
242            // Close if already visible
243            if ( wasOpen ) {
244              return;
245            }
246
247            // Pass empty string as value to search for, displaying all results
248            input.autocomplete( "search", "" );
249          });
250      },
251
252      _source: function( request, response ) {
253        var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
254        response( this.element.children( "option" ).map(function() {
255          var text = jQuery( this ).text();
256          if ( this.value && ( !request.term || matcher.test(text) ) ) {
257
258            return {
259              label: text,
260              value: text,
261              option: this
262            };
263          }
264        }) );
265      },
266
267      _removeIfInvalid: function( event, ui ) {
268
269        // Selected an item, nothing to do
270        if ( ui.item ) {
271          return;
272        }
273
274        // Search for a match (case-insensitive)
275        var value = this.input.val(),
276          valueLowerCase = value.toLowerCase(),
277          valid = false;
278        this.element.children( "option" ).each(function() {
279          if ( jQuery( this ).text().toLowerCase() === valueLowerCase ) {
280            this.selected = valid = true;
281            return false;
282          }
283        });
284
285        // Found a match, nothing to do
286        if ( valid ) {
287          return;
288        }
289
290        // Remove invalid value
291        this.input
292          .val( "" )
293          .attr( "title", value + " " + LANG.plugins.bez.combobox_did_not_match )
294          .tooltip( "open" );
295        this.element.val( "" );
296        this._delay(function() {
297          this.input.tooltip( "close" ).attr( "title", "" );
298        }, 2500 );
299        this.input.autocomplete( "instance" ).term = "";
300      },
301
302      _destroy: function() {
303        this.wrapper.remove();
304        this.element.show();
305      }
306    });
307
308    jQuery( "#bez_invite_users select" ).combobox();
309    //INVITE
310};
311