Guest User

Untitled

a guest
Jan 27th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. /**********************************************************************************\
  2.  
  3. Copyright (c) 2014-2015 Dzianis Rusak
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22.  
  23. \**********************************************************************************/
  24.  
  25. if(typeof SelectLikeABoss !== 'undefined')
  26. SelectLikeABoss.del();
  27. SelectLikeABoss = (function(){
  28. function stopEvent(e){
  29. return e.preventDefault(), e.stopPropagation(), !1;
  30. }
  31. function _bind(evt, bind){
  32. if(bind === undefined)
  33. bind = true;
  34. if(evt.constructor !== Array)
  35. evt = [evt];
  36. for(var i = 0, l = evt.length; i < l; i+=1)
  37. document[bind ? 'addEventListener' : 'removeEventListener'](evt[i], handlers[evt[i]], true);
  38. }
  39. function _unbind(evt){
  40. _bind(evt, false);
  41. }
  42. var browser = (function(){
  43. var w = 'WebkitAppearance' in document.documentElement.style;
  44. var f = typeof InstallTrigger !== 'undefined';
  45. return {
  46. getRangeFromPoint: function(x,y){
  47. if(document.caretRangeFromPoint)
  48. return document.caretRangeFromPoint(x,y);
  49. if(document.caretPositionFromPoint){
  50. var p = document.caretPositionFromPoint(x,y);
  51. if(p){
  52. var range = document.createRange();
  53. range.setStart(p.offsetNode, p.offset);
  54. }
  55. }
  56. return range;
  57. }
  58. , isWK : w
  59. , isFF : f
  60. };
  61. })();
  62. var styleElm,
  63. _letUserSelect = (function(){
  64. var DATA = 'data-Select-like-a-Boss';
  65. var prefix = '',
  66. n;
  67. document.head.appendChild(styleElm = document.createElement('style'));
  68. if(browser.isWK) prefix = '-webkit-';
  69. else if(browser.isFF) prefix = '-moz-';
  70. styleElm.sheet.insertRule('['+DATA+'],['+DATA+'] *{'+prefix+'user-select:text!important;outline:none!important}',0);
  71. return function(node){
  72. if(node){
  73. (n = node).setAttribute(DATA,1);
  74. }else if(n){
  75. n.removeAttribute(DATA);
  76. n = null;
  77. }
  78. };
  79. })();
  80. var findFulcrum = (function(){
  81. var NODE_EXCLUDED = null,
  82. NODE_UNDF;
  83. var gc_top = [
  84. HTMLBodyElement,
  85. HTMLHtmlElement,
  86. HTMLTableCellElement,
  87. HTMLLIElement,
  88. HTMLUListElement,
  89. HTMLOListElement,
  90. HTMLFormElement,
  91. HTMLPreElement
  92. ],
  93. gc_exclude = [
  94. HTMLCanvasElement,
  95. HTMLTextAreaElement,
  96. HTMLInputElement,
  97. HTMLImageElement
  98. ],
  99. gc_elements = [
  100. HTMLAnchorElement,
  101. HTMLButtonElement,
  102. ],
  103. gc_roles = ['button', 'menu','menuitem','menuitemcheckbox','menuitemradio', 'option', 'tab'];
  104. function getCurrent_(node){
  105. var n = node,
  106. gc_ie, gc_ir,
  107. c, r,
  108. ret = {
  109. n: NODE_EXCLUDED,
  110. likeUI: !1,
  111. isAnchor:!1
  112. };
  113. do{
  114. c = n.constructor;
  115. if(gc_top.indexOf(c) > -1)
  116. break;
  117. if(gc_exclude.indexOf(c) > -1)
  118. return ret;
  119. gc_ie = gc_elements.indexOf(c);
  120. gc_ir = (r = n.getAttribute('role')) ? gc_roles.indexOf(r) : -1;
  121. if(gc_ie > -1 || gc_ir > -1){
  122. ret.isAnchor= gc_ie === 0;
  123. ret.likeUI = gc_ir >= 0 || gc_ie > 0;
  124. if(n.textContent !== '')
  125. ret.n = n;
  126. return ret;
  127. }
  128. }while(n = n.parentNode);
  129. if(node.textContent !== '')
  130. ret.n = NODE_UNDF;
  131. return ret;
  132. }
  133. function _fHasParentAnchor(n){
  134. var c;
  135. while((n = n.parentNode) && (c = n.constructor) !== HTMLBodyElement)
  136. if(c === HTMLAnchorElement)
  137. return true;
  138. }
  139. return function(target){
  140. if(target.nodeType === 3)
  141. target = target.parentNode;
  142. var c = getCurrent_(target);
  143. if(c.n === NODE_EXCLUDED)
  144. return;
  145. var n = c.n ? c.n : target;
  146. if(isNotLinkAndDraggable(n))
  147. return;
  148. return {
  149. forced: !! c.n,
  150. node: n,
  151. likeUI: c.likeUI,
  152. isAnchor:c.isAnchor,
  153. hasParentAnchor: ! c.isAnchor && c.n && _fHasParentAnchor(c.n)
  154. };
  155. };
  156. })();
  157. function getCurrentAnchor(node){
  158. var c;
  159. do{
  160. if(node.nodeType === 3)
  161. continue;
  162. if((c = node.constructor) === HTMLAnchorElement)
  163. return node;
  164. if(c === HTMLBodyElement || c === HTMLHtmlElement)
  165. break;
  166. }while(node = node.parentNode);
  167. }
  168. var cursor = {},
  169. f_moveFired,
  170. needDetermineUserSelection,
  171. userSelecting,
  172. needCreateStartSelection,
  173. needStopClick,
  174. timeDown,
  175. s,
  176. _range,
  177. o;
  178. function resetVars(){
  179. needDetermineUserSelection = needCreateStartSelection = true;
  180. f_moveFired = userSelecting = needStopClick = false;
  181. }
  182. var events_mMove_mUp_dStart_dEnd_Click = ['mousemove','mouseup','dragstart','dragend','click'],
  183. events_mMove_mUp_dStart_dEnd = ['mousemove','mouseup','dragstart','dragend'];
  184. function unbindAndDeselect(events){
  185. _unbind(events !== undefined ? events : events_mMove_mUp_dStart_dEnd_Click);
  186. _letUserSelect();
  187. s.removeAllRanges();
  188. }
  189. function isNotLinkAndDraggable(n){
  190. return n.constructor !== HTMLAnchorElement && n.draggable;
  191. }
  192. var hack_lazy_draggable = (function(){
  193. function hack_binder(act){document[act+'EventListener']('mousemove', hack_handler, true);}
  194. function hack_handler(){
  195. hack_binder('remove');
  196. if( ! o || isNotLinkAndDraggable(o.node))
  197. unbindAndDeselect('click');
  198. }
  199. return function(){
  200. hack_binder('remove');
  201. hack_binder('add');
  202. };
  203. })();
  204. function mainMouseDownHandler(e){
  205. if(e.which < 2){
  206. resetVars();
  207. timeDown = e.timeStamp;
  208. cursor.x = e.clientX;
  209. cursor.y = e.clientY;
  210. s = window.getSelection();
  211. if( ! s.isCollapsed && (_range = browser.getRangeFromPoint(cursor.x, cursor.y))){
  212. for(var i = 0, l = s.rangeCount; i < l; i+=1)
  213. if(s.getRangeAt(i).isPointInRange(_range.startContainer, _range.startOffset))
  214. return;
  215. }
  216. _letUserSelect();
  217. if( ! (o = findFulcrum(e.target)))
  218. return;
  219. if(o.forced){
  220. _bind(events_mMove_mUp_dStart_dEnd);
  221. }else{
  222. _bind('click');
  223. hack_lazy_draggable();
  224. }
  225. _letUserSelect(o.node);
  226. }
  227. }
  228. var D = 3,
  229. K = 1.4,
  230. T = 151;
  231. function isCasual(e){
  232. return e.timeStamp - timeDown < T && Math.abs(cursor.x - e.clientX) < 41;
  233. }
  234. function startNewSelection(e, x,y){
  235. if( ! e.ctrlKey || ! browser.isFF)
  236. s.removeAllRanges();
  237. var correct = x > cursor.x ? -2 : 2;
  238. if(_range = browser.getRangeFromPoint(x + correct, y)){
  239. s.addRange(_range);
  240. needCreateStartSelection = false;
  241. }
  242. }
  243. function stopDetermining(isDragging){
  244. needDetermineUserSelection = false;
  245. if( ! isDragging){
  246. needStopClick = true;
  247. _bind('click');
  248. }
  249. }
  250. var handlers = {
  251. mousemove: function(e){
  252. f_moveFired = true;
  253. if(e.which < 1)
  254. return unbindAndDeselect();
  255. var x = e.clientX,
  256. y = e.clientY;
  257. if(needCreateStartSelection)
  258. startNewSelection(e, x,y);
  259. if(needDetermineUserSelection){
  260. var vx = Math.abs(cursor.x - x),
  261. vy = Math.abs(cursor.y - y);
  262. var _uSel = vy === 0 || vx / vy > K;
  263. if(browser.isFF)
  264. userSelecting = _uSel;
  265. if(vx > D || vy > D){
  266. userSelecting = _uSel;
  267. stopDetermining( ! userSelecting);
  268. }
  269. }
  270. if(userSelecting){
  271. if(_range = browser.getRangeFromPoint(x, y - 3)){
  272. s.extend(_range.startContainer, _range.startOffset);
  273. if( ! s.isCollapsed)
  274. stopDetermining();
  275. }
  276. }else{
  277. if( ! needDetermineUserSelection)
  278. unbindAndDeselect();
  279. }
  280. }
  281. , dragstart: function(e){
  282. _unbind('dragstart');
  283. if( ! f_moveFired){
  284. userSelecting = true;
  285. startNewSelection(e, e.clientX, e.clientY);
  286. }
  287. if(userSelecting)
  288. return stopEvent(e);
  289. }
  290. , mouseup: function(e){
  291. _unbind(events_mMove_mUp_dStart_dEnd);
  292. var anchorEnv = o.isAnchor || o.hasParentAnchor;
  293. if((anchorEnv || o.likeUI) && isCasual(e)){
  294. if(userSelecting && ! s.isCollapsed){
  295. unbindAndDeselect('click');
  296. if(browser.isWK && anchorEnv && getCurrentAnchor(e.target) === getCurrentAnchor(o.node))
  297. o.node.dispatchEvent(new MouseEvent('click', e));
  298. }
  299. return;
  300. }
  301. if( ! userSelecting && needStopClick)
  302. needStopClick = false;
  303. setTimeout(function(){_unbind('click')}, 111);
  304. if(s.isCollapsed)
  305. _letUserSelect();
  306. if(o.likeUI && ! s.isCollapsed && ! isCasual(e)){
  307. needStopClick = true;
  308. return stopEvent(e);
  309. }
  310. }
  311. , click: function(e){
  312. _unbind('click');
  313. if(s.isCollapsed)
  314. _letUserSelect();
  315. if(needStopClick || ( ! s.isCollapsed && ! isCasual(e) && ! o.forced))
  316. return stopEvent(e);
  317. }
  318. , dragend: function(e){_unbind(events_mMove_mUp_dStart_dEnd);}
  319. };
  320. var enabled;
  321. return {
  322. toggle: function(is){
  323. enabled = ! (is !== undefined ? ! is : enabled);
  324. document[(enabled ? 'add' : 'remove') + 'EventListener']('mousedown', mainMouseDownHandler, true);
  325. if( ! enabled)
  326. _letUserSelect();
  327. }
  328. , on : function(){this.toggle(!0)}
  329. , off: function(){this.toggle(!1)}
  330. , del: function(){
  331. if(styleElm)
  332. styleElm.parentNode.removeChild(styleElm);
  333. this.off();
  334. delete SelectLikeABoss;
  335. }
  336. };
  337. })();
  338. SelectLikeABoss.on();
Advertisement
Add Comment
Please, Sign In to add comment