pm_editor.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var userAgent = navigator.userAgent.toLowerCase();
  2. var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
  3. var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
  4. var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
  5. function isUndefined(variable) {
  6. return typeof variable == 'undefined' ? true : false;
  7. }
  8. function $(id) {
  9. return document.getElementById(id);
  10. }
  11. function fetchOffset(obj) {
  12. var left_offset = obj.offsetLeft;
  13. var top_offset = obj.offsetTop;
  14. while((obj = obj.offsetParent) != null) {
  15. left_offset += obj.offsetLeft;
  16. top_offset += obj.offsetTop;
  17. }
  18. return { 'left' : left_offset, 'top' : top_offset };
  19. }
  20. function _attachEvent(obj, evt, func) {
  21. if(obj.addEventListener) {
  22. obj.addEventListener(evt, func, false);
  23. } else if(obj.attachEvent) {
  24. obj.attachEvent("on" + evt, func);
  25. }
  26. }
  27. function strlen(str) {
  28. return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
  29. }
  30. var menus = new menu_handler();
  31. function menu_handler() {
  32. this.menu = Array();
  33. }
  34. function menuitems() {
  35. this.ctrlobj = null,
  36. this.menuobj = null;
  37. this.parentids = Array();
  38. this.allowhide = 1;
  39. this.hidelock = 0;
  40. this.clickstatus = 0;
  41. }
  42. function menuobjpos(id, offset) {
  43. if(!menus.menu[id]) {
  44. return;
  45. }
  46. if(!offset) {
  47. offset = 0;
  48. }
  49. var showobj = menus.menu[id].ctrlobj;
  50. var menuobj = menus.menu[id].menuobj;
  51. showobj.pos = fetchOffset(showobj);
  52. showobj.X = showobj.pos['left'];
  53. showobj.Y = showobj.pos['top'];
  54. showobj.w = showobj.offsetWidth;
  55. showobj.h = showobj.offsetHeight;
  56. menuobj.w = menuobj.offsetWidth;
  57. menuobj.h = menuobj.offsetHeight;
  58. if(offset < 3) {
  59. menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
  60. menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
  61. } else if(offset == 3) {
  62. menuobj.style.left = (document.body.clientWidth - menuobj.clientWidth) / 2 + document.body.scrollLeft + 'px';
  63. menuobj.style.top = (document.body.clientHeight - menuobj.clientHeight) / 2 + document.body.scrollTop + 'px';
  64. } else if(offset == 4) {
  65. menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + showobj.w + 'px';
  66. menuobj.style.top = showobj.Y + 'px';
  67. }
  68. if(menuobj.style.clip && !is_opera) {
  69. menuobj.style.clip = 'rect(auto, auto, auto, auto)';
  70. }
  71. }
  72. function showmenu(event, id, click, position) {
  73. if(isUndefined(click)) click = false;
  74. if(!menus.menu[id]) {
  75. menus.menu[id] = new menuitems();
  76. menus.menu[id].ctrlobj = $(id);
  77. if(!menus.menu[id].ctrlobj.getAttribute('parentmenu')) {
  78. menus.menu[id].parentids = Array();
  79. } else {
  80. menus.menu[id].parentids = menus.menu[id].ctrlobj.getAttribute('parentmenu').split(',');
  81. }
  82. menus.menu[id].menuobj = $(id + '_menu');
  83. menus.menu[id].menuobj.style.position = 'absolute';
  84. if(event.type == 'mouseover') {
  85. _attachEvent(menus.menu[id].ctrlobj, 'mouseout', function() { setTimeout(function() {hidemenu(id)}, 100); });
  86. _attachEvent(menus.menu[id].menuobj, 'mouseover', function() { lockmenu(id, 0); });
  87. _attachEvent(menus.menu[id].menuobj, 'mouseout', function() { lockmenu(id, 1);setTimeout(function() {hidemenu(id)}, 100); });
  88. } else if(click || event.type == 'click') {
  89. menus.menu[id].clickstatus = 1;
  90. lockmenu(id, 0);
  91. }
  92. } else if(menus.menu[id].clickstatus == 1) {
  93. lockmenu(id, 1);
  94. hidemenu(id);
  95. menus.menu[id].clickstatus = 0;
  96. return;
  97. }
  98. menuobjpos(id, position);
  99. menus.menu[id].menuobj.style.display = '';
  100. }
  101. function hidemenu(id) {
  102. if(!menus.menu[id] || !menus.menu[id].allowhide || menus.menu[id].hidelock) {
  103. return;
  104. }
  105. menus.menu[id].menuobj.style.display = 'none';
  106. }
  107. function lockmenu(id, value) {
  108. if(!menus.menu[id]) {
  109. return;
  110. }
  111. for(i = 0;i < menus.menu[id].parentids.length;i++) {
  112. menus.menu[menus.menu[id].parentids[i]].hidelock = value == 0 ? 1 : 0;
  113. }
  114. menus.menu[id].allowhide = value;
  115. }
  116. var lang = new Array();
  117. function insertunit(text, textend, moveend) {
  118. $('pm_textarea').focus();
  119. textend = isUndefined(textend) ? '' : textend;
  120. moveend = isUndefined(textend) ? 0 : moveend;
  121. startlen = strlen(text);
  122. endlen = strlen(textend);
  123. if(!isUndefined($('pm_textarea').selectionStart)) {
  124. var opn = $('pm_textarea').selectionStart + 0;
  125. if(textend != '') {
  126. text = text + $('pm_textarea').value.substring($('pm_textarea').selectionStart, $('pm_textarea').selectionEnd) + textend;
  127. }
  128. $('pm_textarea').value = $('pm_textarea').value.substr(0, $('pm_textarea').selectionStart) + text + $('pm_textarea').value.substr($('pm_textarea').selectionEnd);
  129. if(!moveend) {
  130. $('pm_textarea').selectionStart = opn + strlen(text) - endlen;
  131. $('pm_textarea').selectionEnd = opn + strlen(text) - endlen;
  132. }
  133. } else if(document.selection && document.selection.createRange) {
  134. var sel = document.selection.createRange();
  135. if(textend != '') {
  136. text = text + sel.text + textend;
  137. }
  138. sel.text = text.replace(/\r?\n/g, '\r\n');
  139. if(!moveend) {
  140. sel.moveStart('character', -endlen);
  141. sel.moveEnd('character', -endlen);
  142. }
  143. sel.select();
  144. } else {
  145. $('pm_textarea').value += text;
  146. }
  147. }
  148. function getSel() {
  149. if(!isUndefined($('pm_textarea').selectionStart)) {
  150. return $('pm_textarea').value.substr($('pm_textarea').selectionStart, $('pm_textarea').selectionEnd - $('pm_textarea').selectionStart);
  151. } else if(document.selection && document.selection.createRange) {
  152. return document.selection.createRange().text;
  153. } else if(window.getSelection) {
  154. return window.getSelection() + '';
  155. } else {
  156. return false;
  157. }
  158. }
  159. function insertlist(type) {
  160. txt = getSel();
  161. type = isUndefined(type) ? '' : '=' + type;
  162. if(txt) {
  163. var regex = new RegExp('([\r\n]+|^[\r\n]*)(?!\\[\\*\\]|\\[\\/?list)(?=[^\r\n])', 'gi');
  164. txt = '[list' + type + ']\n' + txt.replace(regex, '$1[*]') + '\n' + '[/list]';
  165. insertunit(txt);
  166. } else {
  167. insertunit('[list' + type + ']\n', '[/list]');
  168. while(listvalue = prompt(lang['pm_prompt_list'], '')) {
  169. if(is_opera > 8) {
  170. listvalue = '\n' + '[*]' + listvalue;
  171. insertunit(listvalue);
  172. } else {
  173. listvalue = '[*]' + listvalue + '\n';
  174. insertunit(listvalue);
  175. }
  176. }
  177. }
  178. }
  179. function inserttag(tag, type) {
  180. txt = getSel();
  181. type = isUndefined(type) ? 0 : type;
  182. if(!type) {
  183. if(!txt) {
  184. txt = prompt(lang['pm_prompt_' + tag], '')
  185. }
  186. if(txt) {
  187. insertunit('[' + tag + ']' + txt + '[/' + tag + ']');
  188. }
  189. } else {
  190. txt1 = prompt(lang['pm_prompt_' + tag], '');
  191. if(!txt) {
  192. txt = txt1;
  193. }
  194. if(txt1) {
  195. insertunit('[' + tag + '=' + txt1 + ']' + txt + '[/' + tag + ']');
  196. }
  197. }
  198. }