Elgg  Version 1.9
ui.js
Go to the documentation of this file.
1 elgg.provide('elgg.ui');
2 
3 elgg.ui.init = function () {
4  // @todo we need better documentation for this hack
5  // iOS Hover Event Class Fix
6  $('.elgg-page').attr("onclick", "return true");
7 
8  // add user hover menus
9  elgg.ui.initHoverMenu();
10 
11  //if the user clicks a system message, make it disappear
12  $('.elgg-system-messages li').live('click', function() {
13  $(this).stop().fadeOut('fast');
14  });
15 
16  $('.elgg-system-messages li').animate({opacity: 0.9}, 6000);
17  $('.elgg-system-messages li.elgg-state-success').fadeOut('slow');
18 
19  $('[rel=toggle]').live('click', elgg.ui.toggles);
20 
21  $('[rel=popup]').live('click', elgg.ui.popupOpen);
22 
23  $('.elgg-menu-page .elgg-menu-parent').live('click', elgg.ui.toggleMenu);
24 
25  $('.elgg-requires-confirmation').live('click', elgg.ui.requiresConfirmation);
26 
27  $('.elgg-autofocus').focus();
28  if ($('.elgg-autofocus').length > 0) {
29  elgg.deprecated_notice('Use of .elgg-autofocus is deprecated by html5 autofocus', 1.9);
30  }
31 
32  elgg.ui.initAccessInputs();
33 };
34 
44 elgg.ui.toggles = function(event) {
45  event.preventDefault();
46 
47  // @todo might want to switch this to elgg.getSelectorFromUrlFragment().
48  var target = $(this).toggleClass('elgg-state-active').attr('href');
49 
50  $(target).slideToggle('medium');
51 };
52 
71 elgg.ui.popupOpen = function(event) {
72  event.preventDefault();
73  event.stopPropagation();
74 
75  var target = elgg.getSelectorFromUrlFragment($(this).toggleClass('elgg-state-active').attr('href'));
76  var $target = $(target);
77 
78  // emit a hook to allow plugins to position and control popups
79  var params = {
80  targetSelector: target,
81  target: $target,
82  source: $(this)
83  };
84 
85  var options = {
86  my: 'center top',
87  at: 'center bottom',
88  of: $(this),
89  collision: 'fit fit'
90  };
91 
92  options = elgg.trigger_hook('getOptions', 'ui.popup', params, options);
93 
94  // allow plugins to cancel event
95  if (!options) {
96  return;
97  }
98 
99  // hide if already open
100  if ($target.is(':visible')) {
101  $target.fadeOut();
102  $('body').die('click', elgg.ui.popupClose);
103  return;
104  }
105 
106  $target.appendTo('body')
107  .fadeIn()
108  .position(options);
109 
110  $('body')
111  .die('click', elgg.ui.popupClose)
112  .live('click', elgg.ui.popupClose);
113 };
114 
118 elgg.ui.popupClose = function(event) {
119  $eventTarget = $(event.target);
120  var inTarget = false;
121  var $popups = $('[rel=popup]');
122 
123  // if the click event target isn't in a popup target, fade all of them out.
124  $popups.each(function(i, e) {
125  var target = elgg.getSelectorFromUrlFragment($(e).attr('href')) + ':visible';
126  var $target = $(target);
127 
128  if (!$target.is(':visible')) {
129  return;
130  }
131 
132  // didn't click inside the target
133  if ($eventTarget.closest(target).length > 0) {
134  inTarget = true;
135  return false;
136  }
137  });
138 
139  if (!inTarget) {
140  $popups.each(function(i, e) {
141  var $e = $(e);
142  var $target = $(elgg.getSelectorFromUrlFragment($e.attr('href')) + ':visible');
143  if ($target.length > 0) {
144  $target.fadeOut();
145  $e.removeClass('elgg-state-active');
146  }
147  });
148 
149  $('body').die('click', elgg.ui.popClose);
150  }
151 };
152 
159 elgg.ui.toggleMenu = function(event) {
160  $(this).siblings().slideToggle('medium');
161  $(this).toggleClass('elgg-menu-closed elgg-menu-opened');
162  event.preventDefault();
163 };
164 
171 elgg.ui.initHoverMenu = function(parent) {
172  if (!parent) {
173  parent = document;
174  }
175 
176  // avatar image menu link
177  $(parent).find(".elgg-avatar").live('mouseover', function() {
178  $(this).children(".elgg-icon-hover-menu").show();
179  })
180  .live('mouseout', function() {
181  $(this).children(".elgg-icon-hover-menu").hide();
182  });
183 
184 
185  // avatar contextual menu
186  $(".elgg-avatar > .elgg-icon-hover-menu").live('click', function(e) {
187  // check if we've attached the menu to this element already
188  var $hovermenu = $(this).data('hovermenu') || null;
189 
190  if (!$hovermenu) {
191  $hovermenu = $(this).parent().find(".elgg-menu-hover");
192  $(this).data('hovermenu', $hovermenu);
193  }
194 
195  // close hovermenu if arrow is clicked & menu already open
196  if ($hovermenu.css('display') == "block") {
197  $hovermenu.fadeOut();
198  } else {
199  $avatar = $(this).closest(".elgg-avatar");
200 
201  // @todo Use jQuery-ui position library instead -- much simpler
202  var offset = $avatar.offset();
203  var top = $avatar.height() + offset.top + 'px';
204  var left = $avatar.width() - 15 + offset.left + 'px';
205 
206  $hovermenu.appendTo('body')
207  .css('position', 'absolute')
208  .css("top", top)
209  .css("left", left)
210  .fadeIn('normal');
211  }
212 
213  // hide any other open hover menus
214  $(".elgg-menu-hover:visible").not($hovermenu).fadeOut();
215  });
216 
217  // hide avatar menu when user clicks elsewhere
218  $(document).click(function(event) {
219  if ($(event.target).parents(".elgg-avatar").length === 0) {
220  $(".elgg-menu-hover").fadeOut();
221  }
222  });
223 };
224 
231 elgg.ui.requiresConfirmation = function(e) {
232  var confirmText = $(this).data('confirm') || elgg.echo('question:areyousure');
233  if (!confirm(confirmText)) {
234  e.preventDefault();
235  }
236 };
237 
248 elgg.ui.loginHandler = function(hook, type, params, options) {
249  if (params.target.attr('id') == 'login-dropdown-box') {
250  options.my = 'right top';
251  options.at = 'right bottom';
252  return options;
253  }
254  return null;
255 };
256 
269 elgg.ui.initDatePicker = function() {
270  var loadDatePicker = function() {
271  $('.elgg-input-date').datepicker({
272  // ISO-8601
273  dateFormat: 'yy-mm-dd',
274  onSelect: function(dateText) {
275  if ($(this).is('.elgg-input-timestamp')) {
276  // convert to unix timestamp
277  var dateParts = dateText.split("-");
278  var timestamp = Date.UTC(dateParts[0], dateParts[1] - 1, dateParts[2]);
279  timestamp = timestamp / 1000;
280 
281  var id = $(this).attr('id');
282  $('input[name="' + id + '"]').val(timestamp);
283  }
284  }
285  });
286  };
287 
288  if ($('.elgg-input-date').length && elgg.get_language() == 'en') {
289  loadDatePicker();
290  } else if ($('.elgg-input-date').length) {
291  elgg.get({
292  url: elgg.config.wwwroot + 'vendors/jquery/i18n/jquery.ui.datepicker-'+ elgg.get_language() +'.js',
293  dataType: "script",
294  cache: true,
295  success: loadDatePicker,
296  error: loadDatePicker // english language is already loaded.
297  });
298  }
299 };
300 
308 elgg.ui.registerTogglableMenuItems = function(menuItemNameA, menuItemNameB) {
309  // Handles clicking the first button.
310  $('.elgg-menu-item-' + menuItemNameA + ' a').live('click', function() {
311  var $menu = $(this).closest('.elgg-menu');
312 
313  // Be optimistic about success
314  elgg.ui.toggleMenuItems($menu, menuItemNameB, menuItemNameA);
315 
316  // Send the ajax request
317  elgg.action($(this).attr('href'), {
318  success: function(json) {
319  if (json.system_messages.error.length) {
320  // Something went wrong, so undo the optimistic changes
321  elgg.ui.toggleMenuItems($menu, menuItemNameA, menuItemNameB);
322  }
323  },
324  error: function() {
325  // Something went wrong, so undo the optimistic changes
326  elgg.ui.toggleMenuItems($menu, menuItemNameA, menuItemNameB);
327  }
328  });
329 
330  // Don't want to actually click the link
331  return false;
332  });
333 
334  // Handles clicking the second button
335  $('.elgg-menu-item-' + menuItemNameB + ' a').live('click', function() {
336  var $menu = $(this).closest('.elgg-menu');
337 
338  // Be optimistic about success
339  elgg.ui.toggleMenuItems($menu, menuItemNameA, menuItemNameB);
340 
341  // Send the ajax request
342  elgg.action($(this).attr('href'), {
343  success: function(json) {
344  if (json.system_messages.error.length) {
345  // Something went wrong, so undo the optimistic changes
346  elgg.ui.toggleMenuItems($menu, menuItemNameB, menuItemNameA);
347  }
348  },
349  error: function() {
350  // Something went wrong, so undo the optimistic changes
351  elgg.ui.toggleMenuItems($menu, menuItemNameB, menuItemNameA);
352  }
353  });
354 
355  // Don't want to actually click the link
356  return false;
357  });
358 };
359 
360 elgg.ui.toggleMenuItems = function($menu, nameOfItemToShow, nameOfItemToHide) {
361  $menu.find('.elgg-menu-item-' + nameOfItemToShow).removeClass('hidden').find('a').focus();
362  $menu.find('.elgg-menu-item-' + nameOfItemToHide).addClass('hidden');
363 };
364 
374 elgg.ui.initAccessInputs = function () {
375  $('.elgg-input-access').each(function () {
376  function updateMembersonlyNote() {
377  var val = $select.val();
378  if (val != acl && val !== 0) {
379  // .show() failed in Chrome. Maybe a float/jQuery bug
380  $note.css('visibility', 'visible');
381  } else {
382  $note.css('visibility', 'hidden');
383  }
384  }
385  var $select = $(this),
386  acl = $select.data('group-acl'),
387  $note = $('.elgg-input-access-membersonly', this.parentNode);
388  if ($note) {
389  updateMembersonlyNote();
390  $select.change(updateMembersonlyNote);
391  }
392  });
393 };
394 
395 elgg.register_hook_handler('init', 'system', elgg.ui.init);
396 elgg.register_hook_handler('init', 'system', elgg.ui.initDatePicker);
397 elgg.register_hook_handler('getOptions', 'ui.popup', elgg.ui.loginHandler);
398 elgg.ui.registerTogglableMenuItems('add-friend', 'remove-friend');
elgg message elgg state success
Definition: admin.php:243
elgg
Definition: install.js:23
i
Definition: admin.php:47
$e
Definition: metadata.php:12
margin top
Definition: admin.php:192
elgg message elgg state error
Definition: admin.php:237
GNU GENERAL PUBLIC LICENSE June Free Software Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license document
Definition: LICENSE.txt:4
function elgg combo checkbox click(function(){if($(this).is(':checked')){$(this).prev().attr('disabled', true);$(this).prev().val('');}else{$(this).prev().attr('disabled', false);}})
$CONFIG url
The full URL where Elgg is installed.
Definition: config.php:94
$menu
Definition: default.php:19
$target
Definition: responses.php:18
list style type
Definition: admin.php:724
ui datepicker
Definition: admin.php:569
margin left
Definition: admin.php:210