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