cccc-core.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. (function () {
  2. let obj = {}
  3. obj.isArray = function (argu) {
  4. let result = typeof argu != 'undefined';
  5. result = result && argu;
  6. result = result && argu instanceof Array;
  7. return result === true;
  8. }
  9. obj.isUndefined = function (argu) {
  10. return (typeof argu == 'undefined') === true;
  11. }
  12. obj.isNull = function (argu) {
  13. return obj.isUndefined(argu) || argu == null;
  14. }
  15. obj.isNotNull = function (argu) {
  16. return !obj.isNull(argu);
  17. }
  18. /*
  19. * 判断为空字符串
  20. */
  21. obj.isEmpty = function (str) {
  22. return obj.isNull(str) || str == "";
  23. }
  24. obj.isFunction = function (argu) {
  25. return obj.isNotNull(argu) && argu instanceof Function
  26. }
  27. /**
  28. * 判断是否为IE浏览器
  29. * @return {boolean}
  30. */
  31. obj.isIE = function () {
  32. if (!!window.ActiveXObject || "ActiveXObject" in window) {
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. /**
  39. * 获取http url后面的查询参数
  40. */
  41. obj.getReqParameter = function (key) {
  42. var re = new RegExp(key + '=([^&]*)(?:&)?');
  43. var url = window.location.href;
  44. return url.match(re) && url.match(re)[1];
  45. }
  46. /**
  47. * 格式化字符串
  48. * @param str: 包含占位符{n}的字符串
  49. * @param str之后的参数列表中为替换占位符的值
  50. * 举例 : cccc.format('{1} hellow {0} ', 'world' , 'xiaoming'), 格式化结果: xiaoming hello world
  51. */
  52. obj.format = function (str) {
  53. let result = str
  54. for (let index = 1; index < arguments.length; index++) {
  55. let regex = new RegExp('\\{' + (index - 1) + '\\}', 'g')
  56. result = result.replace(regex, arguments[index])
  57. }
  58. return result
  59. }
  60. obj.formatNumber = function (num) {
  61. return (100 + num + '').substring(1)
  62. }
  63. obj.formatDate = function (date) {
  64. if (date instanceof Date) {
  65. let now = date
  66. let y = now.getFullYear()
  67. let m = obj.formatNumber(now.getMonth() + 1)
  68. let d = obj.formatNumber(now.getDate())
  69. let h = obj.formatNumber(now.getHours())
  70. let minute = obj.formatNumber(now.getMinutes())
  71. let sec = obj.formatNumber(now.getSeconds())
  72. let datestr = cccc.format('{0}-{1}-{2}', y, m, d)
  73. return datestr
  74. }
  75. }
  76. obj.formatDateTime = function (date) {
  77. if (date instanceof Date) {
  78. let now = date
  79. let h = obj.formatNumber(now.getHours())
  80. let minute = obj.formatNumber(now.getMinutes())
  81. let sec = obj.formatNumber(now.getSeconds())
  82. let timeStr = cccc.format('{0}:{1}:{2}', h, minute, sec)
  83. let dataStr = obj.formatDate(now)
  84. return dataStr + ' ' + timeStr
  85. } else
  86. return ''
  87. }
  88. //如果是IE浏览器
  89. if (obj.isIE()) {
  90. Object.assign = function (target, source) {
  91. for (var item in source) {
  92. target[item] = source[item]
  93. }
  94. }
  95. }
  96. function resizeWidth(e) {
  97. pauseEvent(e);
  98. if (e.buttons == 1) {
  99. let offset = e.clientX - document.active_resizing_target.last_pos_x
  100. document.active_resizing_target.style.width = document.active_resizing_target.clientWidth + offset + "px"
  101. document.active_resizing_target.last_pos_x = e.clientX
  102. }
  103. }
  104. function resizeHeight(e) {
  105. pauseEvent(e);
  106. if (e.buttons == 1) {
  107. let offset = e.clientY - document.active_resizing_target.last_pos_y
  108. document.active_resizing_target.style.height = document.active_resizing_target.clientHeight + offset + "px"
  109. document.active_resizing_target.last_pos_y = e.clientY
  110. }
  111. }
  112. let movelistener = {
  113. handleEvent: function (e) {
  114. pauseEvent(e);
  115. }
  116. }
  117. /**
  118. * 阻止事件冒泡
  119. * 不仅仅要stopPropagation,还要preventDefault
  120. * @param e
  121. * @return {boolean}
  122. */
  123. function pauseEvent(e) {
  124. //在事件中
  125. e = e || window.event;
  126. if (e.stopPropagation) e.stopPropagation();
  127. if (e.preventDefault) e.preventDefault();
  128. e.cancelBubble = true;
  129. e.returnValue = false;
  130. return e;
  131. }
  132. obj.enableResize = function (targetElement) {
  133. if (obj.isNull(targetElement))
  134. return
  135. targetElement.className = typeof targetElement.className == 'undefined' || !targetElement.className ? 'enable-resizable' : targetElement.className + ' ' + 'enable-resizable'
  136. let rightSide = document.createElement('div')
  137. rightSide.className = 'right-side'
  138. targetElement.appendChild(rightSide)
  139. document.addEventListener('mousemove', movelistener)
  140. rightSide.addEventListener('mousedown', function (e) {
  141. pauseEvent(e);
  142. document.active_resizing_target = targetElement
  143. document.active_resizing_target.last_pos_x = e.clientX
  144. movelistener.handleEvent = resizeWidth
  145. })
  146. let bottomSide = document.createElement('div')
  147. bottomSide.className = 'bottom-side'
  148. targetElement.appendChild(bottomSide)
  149. bottomSide.addEventListener('mousedown', function (e) {
  150. pauseEvent(e);
  151. document.active_resizing_target = targetElement
  152. document.active_resizing_target.last_pos_y = e.clientY
  153. movelistener.handleEvent = resizeHeight
  154. })
  155. let thumb = document.createElement('div')
  156. thumb.className = 'thumb'
  157. targetElement.appendChild(thumb)
  158. thumb.addEventListener('mousedown', function (e) {
  159. pauseEvent(e);
  160. document.active_resizing_target = targetElement
  161. document.active_resizing_target.last_pos_y = e.clientY
  162. document.active_resizing_target.last_pos_x = e.clientX
  163. movelistener.handleEvent = function (e) {
  164. pauseEvent(e);
  165. resizeHeight(e)
  166. resizeWidth(e)
  167. }
  168. })
  169. document.addEventListener('mouseup', function (e) {
  170. pauseEvent(e);
  171. movelistener.handleEvent = function (e) {
  172. pauseEvent(e);
  173. }
  174. })
  175. }
  176. obj.dialog = function (targetElement, title, dialogWrapId) {
  177. let dialogWrap = document.createElement('div')
  178. dialogWrap.id = dialogWrapId
  179. dialogWrap.className = 'cccc-dialog'
  180. let titlebar = document.createElement('div')
  181. titlebar.className = 'title-bar'
  182. let caption = document.createElement('div')
  183. caption.innerHTML = title
  184. caption.className = 'caption'
  185. let closeBtn = document.createElement('span')
  186. closeBtn.className = 'close'
  187. titlebar.appendChild(caption)
  188. titlebar.appendChild(closeBtn)
  189. dialogWrap.appendChild(titlebar)
  190. dialogWrap.appendChild(targetElement)
  191. document.body.appendChild(dialogWrap)
  192. function mousemoveListener(e) {
  193. e = pauseEvent(e)
  194. if (e.buttons == 1) {
  195. let current_pos = {
  196. x: e.clientX,
  197. y: e.clientY
  198. }
  199. // console.log('this.parentNode.clientLeft = ' + this.parentNode.clientLeft)
  200. titlebar.parentNode.style.left =
  201. titlebar.parentNode.offsetLeft + (current_pos.x - titlebar.last_pos.x) + 'px'
  202. titlebar.parentNode.style.top =
  203. titlebar.parentNode.offsetTop + (current_pos.y - titlebar.last_pos.y) + 'px'
  204. titlebar.last_pos = current_pos
  205. }
  206. }
  207. titlebar.addEventListener('mousedown', function (e) {
  208. e = pauseEvent(e)
  209. this.last_pos = {
  210. x: e.clientX,
  211. y: e.clientY
  212. }
  213. document.addEventListener('mousemove', mousemoveListener)
  214. })
  215. document.addEventListener('mouseup', function (e) {
  216. e = pauseEvent(e)
  217. document.removeEventListener('mousemove', mousemoveListener)
  218. })
  219. closeBtn.addEventListener('click', function (e) {
  220. dialogWrap.style.display = 'none'
  221. })
  222. return {}
  223. }
  224. /**
  225. * 计算html元素在页面中的绝对边界(相对根元素)
  226. * @param htmlElement
  227. */
  228. obj.getBound = function (htmlElement) {
  229. let rect = {
  230. left: $(htmlElement).offset().left, top: $(htmlElement).offset().top,
  231. width: htmlElement.offsetWidth, height: htmlElement.offsetHeight
  232. }
  233. /* if(htmlElement.tagName.toLowerCase() !== 'html')
  234. {
  235. let outter = obj.getBound($(htmlElement).offsetParent().get(0))
  236. rect.left += outter.left
  237. rect.top += outter.top
  238. }*/
  239. return rect
  240. }
  241. if (typeof window.cccc == 'undefined' || obj.isNull(window.cccc))
  242. window.cccc = {}
  243. Object.assign(window.cccc, obj)
  244. }());