xref: /plugin/bez/script.js (revision 23f0eea4779cf2d9b8379aa8f41de3d41d8107f8)
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	/*Zmiana kolorów podczas zmiany priorytetów*/
74	var $issue_rep = jQuery("#bez_issue_report");
75	if ($issue_rep.length > 0) {
76		var colors = {'0': '#B0D2B6', '1':  '#dd9', '2': '#F0AFAD'};
77		var bgcolors = {'0': '#eeF6F0', '1':  '#ffd', '2': '#F8e8E8'};
78		var $form = $issue_rep.find(".bds_form");
79		var $prior = $issue_rep.find(".priorities");
80
81		$prior.find("input").hide();
82		$prior.find("label").css({
83				'margin-right': '5px',
84				'padding' : '5px',
85				'background-color' : '#F7F7F0',
86				'border' : '2px solid',
87				'border-bottom' : '0',
88				'border-top-left-radius' : '5px',
89				'border-top-right-radius' : '5px',
90				'position' : 'relative',
91				'top' : '-1px'
92		});
93
94		$form.css({
95			'border': '2px solid',
96			'border-top-left-radius': '0',
97			'position' : 'relative',
98			'z-index': '100'
99		});
100
101
102		$prior.find("label").each(function() {
103			var $this = jQuery(this);
104			var ind = $this.index();
105			$this.css('border-color', colors[ind]);
106			$this.css('background-color', bgcolors[ind]);
107		});
108
109		var change_color = function() {
110			var color = $prior.find("input:checked").val();
111			$form.css('border-color', colors[color]);
112			$form.css('background-color', bgcolors[color]);
113			/*ustaw*/
114			$prior.find("label").css('z-index', '0');
115			$prior.find("input:checked").parents("label").css('z-index', '1000');
116		};
117		change_color();
118
119		$prior.change(function() {
120			change_color();
121		});
122	}
123
124	jQuery("input[name=plan_date]").datepicker({
125		dateFormat: "yy-mm-dd"
126		});
127	if (jQuery("input[name=all_day_event]").is(":checked")) {
128		jQuery("input[name=start_time]").prop( "disabled", true );
129		jQuery("input[name=finish_time]").prop( "disabled", true );
130	}
131	jQuery("input[name=all_day_event]").on('change', function() {
132		if (jQuery(this).is(":checked")) {
133			jQuery("input[name=start_time]").prop( "disabled", true );
134			jQuery("input[name=finish_time]").prop( "disabled", true );
135		} else {
136			jQuery("input[name=start_time]").prop( "disabled", false );
137			jQuery("input[name=finish_time]").prop( "disabled", false );
138		}
139	});
140	//timepicker
141
142	var hours = ["00:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00",
143			"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",
144			"12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30",
145			"16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30",
146			"20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30",
147			"24:00" ];
148
149	//ukrywanie niepotrzebnych godzin zależnie od godziny rozpoczęcia
150	var hide_unneeded_hours = function ($this) {
151		var hour = $this.val(),
152            index = hours.indexOf(hour),
153            $finish_time_li = jQuery("#bez_timepicker_finish_time li");
154
155		$finish_time_li.show();
156		$finish_time_li.eq(index).prevAll().hide();
157
158		if (jQuery("input[name=finish_time]").val() === '') {
159			jQuery("input[name=finish_time]").val(hour);
160        }
161	};
162	jQuery("input[name=start_time]").blur(function () {
163		hide_unneeded_hours(jQuery(this));
164	});
165
166
167	var autoFill = function (hour) {
168		if (hour.indexOf(":") === -1) {
169			hour += ":00";
170        } else if (hour.match(/^[0-9]{1,2}:$/g)) {
171			hour += "00";
172		} else if (hour.match(/^[0-9]{1,2}:(0|3)$/g)) {
173			hour += "0";
174		} else if (hour.match(/^[0-9]{1,2}:(1|2)$/g)) {
175			hour = hour.slice(0,-1)+"00";
176		} else if (hour.match(/^[0-9]{1,2}:[4-9]$/g)) {
177			hour = hour.slice(0,-1)+"30";
178		} else if (hour.match(/^[0-9]{1,2}:(0|3)[1-9]$/g)) {
179			hour = hour.slice(0,-1)+"0";
180        }
181		return hour;
182	};
183	var listScrool = function($list, hour) {
184		hour = autoFill(hour);
185		var index = hours.indexOf(hour);
186		if (index === -1) {
187            index = 0;
188        }
189		var $li = $list.find("li:first");
190		//hidden lis
191		var $hid_lis = $list.find("li:hidden");
192		$list.scrollTop((index - $hid_lis.length - 1) * $li.outerHeight());
193		$list.find("li").removeClass("selected");
194		$list.find("li").eq(index).addClass("selected");
195
196	};
197
198	jQuery(".bez_timepicker").each(function() {
199		var $this = jQuery(this);
200
201		var id = "bez_timepicker_"+$this.attr("name"),
202		    $wrapper = jQuery(document.createElement('div'))
203                .css('position', 'absolute').addClass('bez_timepicker_wrapper')
204		        .hide().attr("id", id).appendTo("body");
205
206		var offset = $this.offset();
207		offset.top += $this.outerHeight() + 1;
208		$wrapper.offset(offset);
209
210		var $ul = jQuery(document.createElement("ul")).appendTo($wrapper);
211
212
213		for (var h in hours) {
214			var hour = hours[h],
215			    $li = jQuery(document.createElement("li"));
216			$li.text(hour);
217			$ul.append($li);
218		}
219       $ul.on('mousedown', 'li', function(event) {
220				var id = jQuery(this).parents("div").attr("id"),
221				    name = id.replace("bez_timepicker_", '');
222				jQuery("input[name="+name+"]").val(jQuery(this).text());
223
224				jQuery(this).siblings().removeClass("selected");
225				jQuery(this).addClass("selected");
226			});
227
228		$this.focus(function() {
229			var $this = jQuery(this);
230			var id = "bez_timepicker_"+$this.attr("name");
231			$wrapper = jQuery("#"+id);
232			$wrapper.show();
233			listScrool($wrapper, $this.val());
234
235		});
236		$this.blur(function() {
237			var $this = jQuery(this);
238			var id = "bez_timepicker_"+$this.attr("name");
239			$wrapper = jQuery("#"+id);
240			$wrapper.hide();
241		});
242		$this.change(function() {
243			var $this = jQuery(this);
244			var id = "bez_timepicker_"+$this.attr("name");
245			$wrapper = jQuery("#"+id);
246			$this.val($wrapper.find("li.selected").text());
247			$wrapper.hide();
248		});
249		$this.on('keyup', function() {
250			var $this = jQuery(this);
251			var id = "bez_timepicker_"+$this.attr("name");
252			$wrapper = jQuery("#"+id);
253			listScrool($wrapper, $this.val());
254		});
255	});
256	hide_unneeded_hours(jQuery("input[name=start_time]"));
257
258
259	//bez_show_desc
260	jQuery(".bez_desc_row").hide();
261	jQuery("#bez_show_desc").on('click', function(e) {
262		if (jQuery(this).find(".show").is(":visible")) {
263			jQuery(".bez_desc_row").show();
264			jQuery(this).find(".hide").show();
265			jQuery(this).find(".show").hide();
266		} else {
267			jQuery(".bez_desc_row").hide();
268			jQuery(this).find(".hide").hide();
269			jQuery(this).find(".show").show();
270		}
271		e.preventDefault();
272	});
273});
274