xref: /plugin/bez/script.js (revision b3485d9277c69bf541e996553726f9636a464cd4)
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	$delete_buts.each(function() {
65		jQuery(this).click(function(e) {
66			e.preventDefault();
67			var $click = jQuery(this);
68			var off = $click.offset();
69			$conf.appendTo("body");
70			$conf.css({
71					'position': 'absolute',
72					'left':		off.left-$conf.width()+$click.width(),
73					'top':	off.top+2,
74				});
75			$conf.find("input").unbind("click");
76			$conf.find("input").bind("click", function(e) {
77				e.preventDefault();
78				window.location = $click.attr("href");
79			});
80			$conf.show();
81		});
82	});
83
84	//entities sort
85	jQuery("#entities_form input[type=button]").click(function() {
86		var textarea = jQuery(this).parents("form").find("textarea");
87		var lines = jQuery.trim(textarea.val()).split("\n");
88		lines.sort();
89		textarea.val(lines.join("\n"));
90	});
91
92	jQuery("input[name=plan_date]").datepicker({
93		dateFormat: "yy-mm-dd"
94		});
95	if (jQuery("input[name=all_day_event]").is(":checked")) {
96		jQuery("input[name=start_time]").prop( "disabled", true );
97		jQuery("input[name=finish_time]").prop( "disabled", true );
98	}
99	jQuery("input[name=all_day_event]").on('change', function() {
100		if (jQuery(this).is(":checked")) {
101			jQuery("input[name=start_time]").prop( "disabled", true );
102			jQuery("input[name=finish_time]").prop( "disabled", true );
103		} else {
104			jQuery("input[name=start_time]").prop( "disabled", false );
105			jQuery("input[name=finish_time]").prop( "disabled", false );
106		}
107	});
108	//timepicker
109
110	var hours = ["00:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00",
111			"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",
112			"12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30",
113			"16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30",
114			"20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30",
115			"24:00" ];
116
117	//ukrywanie niepotrzebnych godzin zależnie od godziny rozpoczęcia
118	var hide_unneeded_hours = function ($this) {
119		var hour = $this.val(),
120            index = hours.indexOf(hour),
121            $finish_time_li = jQuery("#bez_timepicker_finish_time li");
122
123		$finish_time_li.show();
124		$finish_time_li.eq(index).prevAll().hide();
125
126		if (jQuery("input[name=finish_time]").val() === '') {
127			jQuery("input[name=finish_time]").val(hour);
128        }
129	};
130	jQuery("input[name=start_time]").blur(function () {
131		hide_unneeded_hours(jQuery(this));
132	});
133
134
135	var autoFill = function (hour) {
136		if (hour.indexOf(":") === -1) {
137			hour += ":00";
138        } else if (hour.match(/^[0-9]{1,2}:$/g)) {
139			hour += "00";
140		} else if (hour.match(/^[0-9]{1,2}:(0|3)$/g)) {
141			hour += "0";
142		} else if (hour.match(/^[0-9]{1,2}:(1|2)$/g)) {
143			hour = hour.slice(0,-1)+"00";
144		} else if (hour.match(/^[0-9]{1,2}:[4-9]$/g)) {
145			hour = hour.slice(0,-1)+"30";
146		} else if (hour.match(/^[0-9]{1,2}:(0|3)[1-9]$/g)) {
147			hour = hour.slice(0,-1)+"0";
148        }
149		return hour;
150	};
151	var listScrool = function($list, hour) {
152		hour = autoFill(hour);
153		var index = hours.indexOf(hour);
154		if (index === -1) {
155            index = 0;
156        }
157		var $li = $list.find("li:first");
158		//hidden lis
159		var $hid_lis = $list.find("li:hidden");
160		$list.scrollTop((index - $hid_lis.length - 1) * $li.outerHeight());
161		$list.find("li").removeClass("selected");
162		$list.find("li").eq(index).addClass("selected");
163
164	};
165
166	jQuery(".bez_timepicker").each(function() {
167		var $this = jQuery(this);
168
169		var id = "bez_timepicker_"+$this.attr("name"),
170		    $wrapper = jQuery(document.createElement('div'))
171                .css('position', 'absolute').addClass('bez_timepicker_wrapper')
172		        .hide().attr("id", id).appendTo("body");
173
174		var offset = $this.offset();
175		offset.top += $this.outerHeight() + 1;
176		$wrapper.offset(offset);
177
178		var $ul = jQuery(document.createElement("ul")).appendTo($wrapper);
179
180
181		for (var h in hours) {
182			var hour = hours[h],
183			    $li = jQuery(document.createElement("li"));
184			$li.text(hour);
185			$ul.append($li);
186		}
187       $ul.on('mousedown', 'li', function(event) {
188				var id = jQuery(this).parents("div").attr("id"),
189				    name = id.replace("bez_timepicker_", '');
190				jQuery("input[name="+name+"]").val(jQuery(this).text());
191
192				jQuery(this).siblings().removeClass("selected");
193				jQuery(this).addClass("selected");
194			});
195
196		$this.focus(function() {
197			var $this = jQuery(this);
198			var id = "bez_timepicker_"+$this.attr("name");
199			$wrapper = jQuery("#"+id);
200			$wrapper.show();
201			listScrool($wrapper, $this.val());
202
203		});
204		$this.blur(function() {
205			var $this = jQuery(this);
206			var id = "bez_timepicker_"+$this.attr("name");
207			$wrapper = jQuery("#"+id);
208			$wrapper.hide();
209		});
210		$this.change(function() {
211			var $this = jQuery(this);
212			var id = "bez_timepicker_"+$this.attr("name");
213			$wrapper = jQuery("#"+id);
214			$this.val($wrapper.find("li.selected").text());
215			$wrapper.hide();
216		});
217		$this.on('keyup', function() {
218			var $this = jQuery(this);
219			var id = "bez_timepicker_"+$this.attr("name");
220			$wrapper = jQuery("#"+id);
221			listScrool($wrapper, $this.val());
222		});
223	});
224	hide_unneeded_hours(jQuery("input[name=start_time]"));
225
226	jQuery("#bez_task_context").hide();
227	jQuery("#bez_task_context_show_button").show().click(function () {
228		jQuery("#bez_task_context").show();
229		jQuery(this).hide();
230	});
231
232	//bez_show_desc
233	jQuery(".bez_desc_row").hide();
234	jQuery("#bez_show_desc").on('click', function(e) {
235		if (jQuery(this).find(".show").is(":visible")) {
236			jQuery(".bez_desc_row").show();
237			jQuery(this).find(".hide").show();
238			jQuery(this).find(".show").hide();
239		} else {
240			jQuery(".bez_desc_row").hide();
241			jQuery(this).find(".hide").hide();
242			jQuery(this).find(".show").show();
243		}
244		e.preventDefault();
245	});
246
247	jQuery(".bez_show_single_desc").on('click', function(e) {
248		var $row = jQuery(this).parents('tr'),
249			row_id = $row.data('bez-row-id'),
250			$desc_rows = $row.parents('table').find('.task'+row_id);
251
252		if ($desc_rows.is(":visible")) {
253			$desc_rows.hide();
254		} else {
255			$desc_rows.show();
256		}
257		e.preventDefault();
258	});
259});
260