xref: /plugin/bez/script.js (revision e7b291ce15b04094b0c500d40606aa61cdb773cb)
1var bds = {};
2
3bds.gup = function (name) {
4    'use strict';
5    var regexS = "[\\?&]" + name + "=([^&#]*)",
6        regex = new RegExp(regexS),
7        results = regex.exec(window.location.href);
8    if (results === null) {
9        return "";
10    } else {
11        return results[1];
12    }
13};
14
15jQuery(document).ready(function () {
16    'use strict';
17	var ids = ['description', 'cause', 'task', 'reason', 'opinion'];
18
19	for (var i = 0; i < ids.length; i++) {
20		var textarea = jQuery("textarea#" + ids[i]);
21		if (textarea.length > 0) {
22			textarea.before('<div id="toolbar'+ids[i]+'"></div>');
23			if (textarea.parents("form").find("input[name=id]").length === 0) {
24				textarea.before('<input type="hidden" name="id" value="'+bds.gup('id')+'" />');
25			}
26			initToolbar('toolbar'+ids[i], ids[i], toolbar);
27		}
28	}
29
30	//comment
31	if (jQuery('#content').length > 0) {
32		var $textarea = jQuery('#content');
33		var $header = $textarea.parents(".bez_comment").find(".bez_toolbar");
34		$header.append('<div id="toolbarcontent"></div>');
35		initToolbar('toolbarcontent', 'content', toolbar);
36
37		jQuery(".bez_button_notify").on('click', function (e) {
38			e.preventDefault();
39
40			var selection = DWgetSelection($textarea[0]);
41			pasteText(selection, '@', {nosel: true});
42		});
43	}
44
45	var $conf = jQuery("#bez_removal_confirm");
46	$conf.find(".no").click(function(e) {
47		e.preventDefault();
48		$conf.hide();
49	});
50
51	//delete_button
52	var $delete_buts = jQuery("#bez_comments, #bez_causes").find(".bez_delete_button");
53	jQuery("body").bind("click", function (e) {
54		var $target = jQuery(e.target);
55		if (!$target.is($delete_buts)) {
56			$conf.hide();
57        }
58	});
59
60	jQuery('.bez_delete_prompt').click('on', function () {
61		return confirm('Are you sure you want to delete this item?');
62	});
63
64	//cause in task
65	//~ jQuery('#cause').on('change', function () {
66			//~ var val = jQuery(this).val(),
67				//~ $tasktype_row = jQuery('#tasktype').parents('.row');
68			//~ console.log(val);
69			//~ if (val === '') {
70				//~ $tasktype_row.hide();
71			//~ } else {
72				//~ $tasktype_row.show();
73			//~ }
74	//~ });
75
76	$delete_buts.each(function() {
77		jQuery(this).click(function(e) {
78			e.preventDefault();
79			var $click = jQuery(this);
80			var off = $click.offset();
81			$conf.appendTo("body");
82			$conf.css({
83					'position': 'absolute',
84					'left':		off.left-$conf.width()+$click.width(),
85					'top':	off.top+2,
86				});
87			$conf.find("input").unbind("click");
88			$conf.find("input").bind("click", function(e) {
89				e.preventDefault();
90				window.location = $click.attr("href");
91			});
92			$conf.show();
93		});
94	});
95
96	//entities sort
97	jQuery("#entities_form input[type=button]").click(function() {
98		var textarea = jQuery(this).parents("form").find("textarea");
99		var lines = jQuery.trim(textarea.val()).split("\n");
100		lines.sort();
101		textarea.val(lines.join("\n"));
102	});
103
104	jQuery("input[name=plan_date]").datepicker({
105		dateFormat: "yy-mm-dd"
106		});
107	if (jQuery("input[name=all_day_event]").is(":checked")) {
108		jQuery("input[name=start_time]").prop( "disabled", true );
109		jQuery("input[name=finish_time]").prop( "disabled", true );
110	}
111	jQuery("input[name=all_day_event]").on('change', function() {
112		if (jQuery(this).is(":checked")) {
113			jQuery("input[name=start_time]").prop( "disabled", true );
114			jQuery("input[name=finish_time]").prop( "disabled", true );
115		} else {
116			jQuery("input[name=start_time]").prop( "disabled", false );
117			jQuery("input[name=finish_time]").prop( "disabled", false );
118		}
119	});
120	//timepicker
121
122	var hours = ["00:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00",
123			"4:30", "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00", "9:30", "10:00", "10:30", "11:00", "11:30",
124			"12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30",
125			"16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30",
126			"20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30",
127			"24:00" ];
128
129	//ukrywanie niepotrzebnych godzin zależnie od godziny rozpoczęcia
130	var hide_unneeded_hours = function ($this) {
131		var hour = $this.val(),
132            index = hours.indexOf(hour),
133            $finish_time_li = jQuery("#bez_timepicker_finish_time li");
134
135		$finish_time_li.show();
136		$finish_time_li.eq(index).prevAll().hide();
137
138		if (jQuery("input[name=finish_time]").val() === '') {
139			jQuery("input[name=finish_time]").val(hour);
140        }
141	};
142	jQuery("input[name=start_time]").blur(function () {
143		hide_unneeded_hours(jQuery(this));
144	});
145
146
147	var autoFill = function (hour) {
148		if (hour.indexOf(":") === -1) {
149			hour += ":00";
150        } else if (hour.match(/^[0-9]{1,2}:$/g)) {
151			hour += "00";
152		} else if (hour.match(/^[0-9]{1,2}:(0|3)$/g)) {
153			hour += "0";
154		} else if (hour.match(/^[0-9]{1,2}:(1|2)$/g)) {
155			hour = hour.slice(0,-1)+"00";
156		} else if (hour.match(/^[0-9]{1,2}:[4-9]$/g)) {
157			hour = hour.slice(0,-1)+"30";
158		} else if (hour.match(/^[0-9]{1,2}:(0|3)[1-9]$/g)) {
159			hour = hour.slice(0,-1)+"0";
160        }
161		return hour;
162	};
163	var listScrool = function($list, hour) {
164		hour = autoFill(hour);
165		var index = hours.indexOf(hour);
166		if (index === -1) {
167            index = 0;
168        }
169		var $li = $list.find("li:first");
170		//hidden lis
171		var $hid_lis = $list.find("li:hidden");
172		$list.scrollTop((index - $hid_lis.length - 1) * $li.outerHeight());
173		$list.find("li").removeClass("selected");
174		$list.find("li").eq(index).addClass("selected");
175
176	};
177
178	jQuery(".bez_timepicker").each(function() {
179		var $this = jQuery(this);
180
181		var id = "bez_timepicker_"+$this.attr("name"),
182		    $wrapper = jQuery(document.createElement('div'))
183                .css('position', 'absolute').addClass('bez_timepicker_wrapper')
184		        .hide().attr("id", id).appendTo("body");
185
186		var offset = $this.offset();
187		offset.top += $this.outerHeight() + 1;
188		$wrapper.offset(offset);
189
190		var $ul = jQuery(document.createElement("ul")).appendTo($wrapper);
191
192
193		for (var h in hours) {
194			var hour = hours[h],
195			    $li = jQuery(document.createElement("li"));
196			$li.text(hour);
197			$ul.append($li);
198		}
199       $ul.on('mousedown', 'li', function(event) {
200				var id = jQuery(this).parents("div").attr("id"),
201				    name = id.replace("bez_timepicker_", '');
202				jQuery("input[name="+name+"]").val(jQuery(this).text());
203
204				jQuery(this).siblings().removeClass("selected");
205				jQuery(this).addClass("selected");
206			});
207
208		$this.focus(function() {
209			var $this = jQuery(this);
210			var id = "bez_timepicker_"+$this.attr("name");
211			$wrapper = jQuery("#"+id);
212			$wrapper.show();
213			listScrool($wrapper, $this.val());
214
215		});
216		$this.blur(function() {
217			var $this = jQuery(this);
218			var id = "bez_timepicker_"+$this.attr("name");
219			$wrapper = jQuery("#"+id);
220			$wrapper.hide();
221		});
222		$this.change(function() {
223			var $this = jQuery(this);
224			var id = "bez_timepicker_"+$this.attr("name");
225			$wrapper = jQuery("#"+id);
226			$this.val($wrapper.find("li.selected").text());
227			$wrapper.hide();
228		});
229		$this.on('keyup', function() {
230			var $this = jQuery(this);
231			var id = "bez_timepicker_"+$this.attr("name");
232			$wrapper = jQuery("#"+id);
233			listScrool($wrapper, $this.val());
234		});
235	});
236	hide_unneeded_hours(jQuery("input[name=start_time]"));
237
238	jQuery("#bez_task_context").hide();
239	jQuery("#bez_task_context_show_button").show().click(function () {
240		jQuery("#bez_task_context").show();
241		jQuery(this).hide();
242	});
243
244	//bez_show_desc
245	jQuery(".bez_desc_row").hide();
246	jQuery("#bez_show_desc").on('click', function(e) {
247		if (jQuery(this).find(".show").is(":visible")) {
248			jQuery(".bez_desc_row").show();
249			jQuery(this).find(".hide").show();
250			jQuery(this).find(".show").hide();
251		} else {
252			jQuery(".bez_desc_row").hide();
253			jQuery(this).find(".hide").hide();
254			jQuery(this).find(".show").show();
255		}
256		e.preventDefault();
257	});
258
259	jQuery(".bez_show_single_desc").on('click', function(e) {
260		var $row = jQuery(this).parents('tr'),
261			row_id = $row.data('bez-row-id'),
262			$desc_rows = $row.parents('table').find('.task'+row_id);
263
264		if ($desc_rows.is(":visible")) {
265			$desc_rows.hide();
266		} else {
267			$desc_rows.show();
268		}
269		e.preventDefault();
270	});
271
272	//highlight filters
273	var highlight = function() {
274		var $this = jQuery(this);
275		if ($this.find('option:selected').index() !== 0) {
276			$this.css('outline', '2px solid #FB5A5A');
277		} else {
278			$this.css('outline', 'none');
279		}
280	};
281	var highlight_input = function() {
282		var $this = jQuery(this);
283		if ($this.val().length > 0) {
284			$this.css('outline', '2px solid #FB5A5A');
285		} else {
286			$this.css('outline', 'none');
287		}
288	};
289	jQuery(".bez_filter_form select").change(highlight).each(highlight);
290	jQuery(".bez_filter_form input:text").blur(highlight_input).each(highlight_input);
291});
292