noteBar.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * [ONEMAP.M.noteBar]
  3. * @return {[object]}
  4. */
  5. define(['html!templates/noteBar',
  6. 'css!styles/noteBar'
  7. ], function (tplLayout) {
  8. var count = 0;
  9. var timer = {};
  10. /**
  11. * 初始化并订阅事件
  12. * @return {[type]} [description]
  13. */
  14. function init() {
  15. $('body').append(tplLayout);
  16. subscribe();
  17. }
  18. /**
  19. * 构建外壳
  20. * @return {[type]} [description]
  21. */
  22. function buildNoteWrap() {
  23. return $('<div class="note-bar cover-panel"><iframe frameborder="0" class="cover-iframe"></iframe><div class="cover-content"></div></div>');
  24. }
  25. /**
  26. * 构建关闭按钮
  27. * @return {[type]} [description]
  28. */
  29. function buildNoteBtnClose() {
  30. return $('<span class="icon icon-remove ibtn-note-bar-close"></span>');
  31. }
  32. /**
  33. * 添加提示
  34. * @param {[type]} options [description]
  35. * options.type
  36. * options.message
  37. * options.keep
  38. */
  39. function add(options) {
  40. var noteId = ++count;
  41. var wrap = buildNoteWrap();
  42. wrap.attr('id', 'noteBar-' + noteId);
  43. var btnClose = buildNoteBtnClose();
  44. var content = $('<div class="note-content"></div>');
  45. if (!options.hasOwnProperty('delayTime')) {
  46. options.delayTime = 3000;
  47. }
  48. switch (options.type) {
  49. case 'note':
  50. content.addClass('background-note');
  51. $('<span class="icon icon-cogs note-icon"></span>').appendTo(wrap.find('.cover-content'));
  52. break;
  53. case 'success':
  54. content.addClass('background-success');
  55. $('<span class="icon icon-ok note-icon"></span>').appendTo(wrap.find('.cover-content'));
  56. break;
  57. case 'warning':
  58. content.addClass('background-warning');
  59. $('<span class="icon icon-warning-sign note-icon"></span>').appendTo(wrap.find('.cover-content'));
  60. break;
  61. case 'error':
  62. content.addClass('background-error');
  63. $('<span class="icon icon-bell note-icon"></span>').appendTo(wrap.find('.cover-content'));
  64. break;
  65. }
  66. content.append(options.message);
  67. btnClose.appendTo(wrap.find('.cover-content'));
  68. content.appendTo(wrap.find('.cover-content'));
  69. wrap.appendTo($('#noteBar'));
  70. wrap.fadeIn('fast');
  71. btnClose.bind('click', {
  72. id: noteId
  73. }, function (e) {
  74. del({
  75. 'id': e.data.id
  76. });
  77. });
  78. if (!options.keep) {
  79. timer[noteId] = setTimeout(function () {
  80. del({
  81. 'id': noteId
  82. });
  83. }, options.delayTime);
  84. }
  85. return noteId;
  86. }
  87. /**
  88. * 删除提示
  89. * @param {[type]} options [description]
  90. * @return {[type]} [description]
  91. */
  92. function del(options) {
  93. $('#noteBar-' + options.id).fadeOut(100, function () {
  94. $('#noteBar-' + options.id).remove();
  95. if (timer.hasOwnProperty(options.id)) {
  96. clearTimeout(timer[options.id]);
  97. }
  98. });
  99. }
  100. /**
  101. * 清除所有提示
  102. * @param {[type]} options [description]
  103. * @return {[type]} [description]
  104. */
  105. function clean() {
  106. timer = {};
  107. count = 0;
  108. $('#noteBar').empty();
  109. }
  110. /**
  111. * 注册监听
  112. * @type {Function}
  113. * ONEMAP.C.publisher.publish(options,'noteBar::type');
  114. */
  115. function subscribe() {
  116. ONEMAP.C.publisher.subscribe(add, 'noteBar::add');
  117. ONEMAP.C.publisher.subscribe(del, 'noteBar::del');
  118. ONEMAP.C.publisher.subscribe(clean, 'noteBar::clean');
  119. }
  120. init();
  121. return ONEMAP.M.noteBar = {
  122. add: add,
  123. del: del,
  124. clean: clean
  125. }
  126. });