query.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Tencent is pleased to support the open source community by making vConsole available.
  3. Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. http://opensource.org/licenses/MIT
  6. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. /**
  9. * DOM related Functions
  10. */
  11. import {isArray} from '../lib/tool.js';
  12. import render from '../lib/mito.js';
  13. const $ = {};
  14. /**
  15. * get single element
  16. * @public
  17. */
  18. $.one = function(selector, contextElement) {
  19. try {
  20. return (contextElement || document).querySelector(selector) || undefined;
  21. } catch (e) {
  22. return undefined;
  23. }
  24. }
  25. /**
  26. * get multiple elements
  27. * @public
  28. */
  29. $.all = function(selector, contextElement) {
  30. try {
  31. const nodeList = (contextElement || document).querySelectorAll(selector);
  32. return Array.from(nodeList);
  33. } catch (e) {
  34. return [];
  35. }
  36. }
  37. /**
  38. * add className(s) to an or multiple element(s)
  39. * @public
  40. */
  41. $.addClass = function($el, className) {
  42. if (!$el) {
  43. return;
  44. }
  45. if (!isArray($el)) {
  46. $el = [$el];
  47. }
  48. for (let i=0; i<$el.length; i++) {
  49. let name = $el[i].className || '',
  50. arr = name.split(' ');
  51. if (arr.indexOf(className) > -1) {
  52. continue;
  53. }
  54. arr.push(className);
  55. $el[i].className = arr.join(' ');
  56. }
  57. }
  58. /**
  59. * remove className(s) from an or multiple element(s)
  60. * @public
  61. */
  62. $.removeClass = function($el, className) {
  63. if (!$el) {
  64. return;
  65. }
  66. if (!isArray($el)) {
  67. $el = [$el];
  68. }
  69. for (let i=0; i<$el.length; i++) {
  70. let arr = $el[i].className.split(' ');
  71. for (let j=0; j<arr.length; j++) {
  72. if (arr[j] == className) {
  73. arr[j] = '';
  74. }
  75. }
  76. $el[i].className = arr.join(' ').trim();
  77. }
  78. }
  79. /**
  80. * see whether an element contains a className
  81. * @public
  82. */
  83. $.hasClass = function($el, className) {
  84. if (!$el || !$el.classList) {
  85. return false;
  86. }
  87. return $el.classList.contains(className);
  88. }
  89. /**
  90. * bind an event to element(s)
  91. * @public
  92. * @param array $el element object or array
  93. * @param string eventType name of the event
  94. * @param function fn
  95. * @param boolean useCapture
  96. */
  97. $.bind = function($el, eventType, fn, useCapture) {
  98. if (!$el) {
  99. return;
  100. }
  101. if (!isArray($el)) {
  102. $el = [$el];
  103. }
  104. $el.forEach((el) => {
  105. el.addEventListener(eventType, fn, !!useCapture);
  106. })
  107. }
  108. /**
  109. * delegate an event to a parent element
  110. * @public
  111. * @param array $el parent element
  112. * @param string eventType name of the event
  113. * @param string selector target's selector
  114. * @param function fn
  115. */
  116. $.delegate = function($el, eventType, selector, fn) {
  117. if (!$el) { return; }
  118. $el.addEventListener(eventType, function(e) {
  119. let targets = $.all(selector, $el);
  120. if (!targets) {
  121. return;
  122. }
  123. findTarget:
  124. for (let i=0; i<targets.length; i++) {
  125. let $node = e.target;
  126. while ($node) {
  127. if ($node == targets[i]) {
  128. fn.call($node, e);
  129. break findTarget;
  130. }
  131. $node = $node.parentNode;
  132. if ($node == $el) {
  133. break;
  134. }
  135. }
  136. }
  137. }, false);
  138. };
  139. /**
  140. * simply render a HTML template
  141. * @param string tpl
  142. * @param object key-value data
  143. * @param boolean whether to conver to HTML string
  144. * @return object|string
  145. */
  146. $.render = render;
  147. /**
  148. * export
  149. */
  150. export default $;