resource.service.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. angular.module('kityminderEditor')
  2. .service('resourceService', ['$document', function($document) {
  3. var openScope = null;
  4. this.open = function( dropdownScope ) {
  5. if ( !openScope ) {
  6. $document.bind('click', closeDropdown);
  7. $document.bind('keydown', escapeKeyBind);
  8. }
  9. if ( openScope && openScope !== dropdownScope ) {
  10. openScope.resourceListOpen = false;
  11. }
  12. openScope = dropdownScope;
  13. };
  14. this.close = function( dropdownScope ) {
  15. if ( openScope === dropdownScope ) {
  16. openScope = null;
  17. $document.unbind('click', closeDropdown);
  18. $document.unbind('keydown', escapeKeyBind);
  19. }
  20. };
  21. var closeDropdown = function( evt ) {
  22. // This method may still be called during the same mouse event that
  23. // unbound this event handler. So check openScope before proceeding.
  24. //console.log(evt, openScope);
  25. if (!openScope) { return; }
  26. var toggleElement = openScope.getToggleElement();
  27. if ( evt && toggleElement && toggleElement[0].contains(evt.target) ) {
  28. return;
  29. }
  30. openScope.$apply(function() {
  31. console.log('to close the resourcelist');
  32. openScope.resourceListOpen = false;
  33. });
  34. };
  35. var escapeKeyBind = function( evt ) {
  36. if ( evt.which === 27 ) {
  37. openScope.focusToggleElement();
  38. closeDropdown();
  39. }
  40. };
  41. }])