Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

full-screen-helper.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * full-screen-helper.js 1.0.5
  3. *
  4. * Copyright (c) 2020 Guilherme Nascimento (brcontainer@yahoo.com.br)
  5. *
  6. * Released under the MIT license
  7. */
  8. (function (u) {
  9. "use strict";
  10. var html, body, timer,
  11. w = typeof window !== 'undefined' ? window : {},
  12. d = w.document || {},
  13. $ = w.$ || {},
  14. current = null,
  15. wsso, wssoc = false, escEvt = false,
  16. useviewport = false, allowviewport = true, state = false,
  17. sc = !!d.exitFullscreen,
  18. mozc = !!d.mozCancelFullScreen,
  19. wkc = !!(d.webkitExitFullscreen || d.webkitCancelFullScreen),
  20. wkco = !!d.webkitCancelFullScreen,
  21. wkcn = !!d.webkitExitFullscreen,
  22. msc = !!d.msExitFullscreen,
  23. reRoot = /(^|\s+)fsh-infullscreen($|\s+)/i,
  24. reElement = /(^|\s+)full-screen-helper($|\s+)/i,
  25. changeEvents = [],
  26. events = [
  27. "webkitfullscreenchange", "mozfullscreenchange",
  28. "fullscreenchange", "MSFullscreenChange"
  29. ];
  30. var realsupport = sc || mozc || wkc || msc;
  31. function change(callback, remove) {
  32. if (typeof callback !== "function") return;
  33. if (!remove) {
  34. changeEvents.push(callback);
  35. return;
  36. }
  37. if (!remove) {
  38. changeEvents.push(callback);
  39. return;
  40. }
  41. var relist = [];
  42. for (var i = 0, j = changeEvents.length; i < j; i++) {
  43. if (changeEvents[i] !== callback) relist.push(callback);
  44. }
  45. changeEvents = relist;
  46. relist = null;
  47. }
  48. function isValid(obj) {
  49. if (current) return false;
  50. if (obj === u || obj === d) return d.body;
  51. if (typeof obj === "string") return d.querySelector(obj);
  52. if (w.HTMLElement) {
  53. if (!obj || !(obj instanceof w.HTMLElement && obj.ownerDocument === d)) return false;
  54. }
  55. if (!obj || obj.nodeType !== 1 || obj.ownerDocument !== d) return false;
  56. return obj;
  57. }
  58. function addEvt(obj, type, callback) {
  59. if (obj.addEventListener) {
  60. obj.addEventListener(type, callback);
  61. } else {
  62. obj.attachEvent("on" + type, callback);
  63. }
  64. }
  65. function isFS1() {
  66. return getElements() &&
  67. (w.outerWidth || w.innerWidth || html.clientWidth) == w.screen.width;
  68. }
  69. function isFS2() {
  70. var element = d.fullscreenElement || d.mozFullScreenElement ||
  71. d.webkitFullscreenElement || d.msFullscreenElement;
  72. if (!element) return false;
  73. if (element !== current) current = element;
  74. return true;
  75. }
  76. function getWSSO() {
  77. if (wssoc) return wssoc;
  78. if (wsso === false || w.ActiveXObject === u) {
  79. wsso = false;
  80. } else if (wsso === u) {
  81. try {
  82. wsso = new w.ActiveXObject("WScript.Shell");
  83. wssoc = true;
  84. addEvt(w, "resize", resizeObserver);
  85. } catch (ee) {
  86. wsso = false;
  87. }
  88. }
  89. return wssoc;
  90. }
  91. function escObserver(e) {
  92. e = e || w.event;
  93. if ((e.wich || e.keyCode) == 27) exit();
  94. }
  95. function toggleClass() {
  96. active(wssoc ? isFS1() : isFS2());
  97. }
  98. function resizeObserver(e) {
  99. clearTimeout(timer);
  100. timer = setTimeout(toggleClass, wssoc ? 100 : 10);
  101. }
  102. function getElements() {
  103. if (html) return true;
  104. body = d.body;
  105. html = d.documentElement || (body && body.parentNode);
  106. return !!html;
  107. }
  108. function fallbackRequest(element) {
  109. if (!escEvt) {
  110. escEvt = true;
  111. addEvt(d, "keydown", escObserver);
  112. }
  113. if (getWSSO()) {
  114. if (!isFS1()) {
  115. current = element;
  116. active(true);
  117. wsso.SendKeys("{F11}");
  118. }
  119. return;
  120. }
  121. useviewport = allowviewport;
  122. if (useviewport) request(element);
  123. }
  124. function active(enable) {
  125. if (state === enable || !current) return;
  126. if (!getElements()) {
  127. current = null;
  128. return;
  129. }
  130. if (enable) {
  131. if (!reRoot.test(html.className)) html.className += " fsh-infullscreen";
  132. if (!reElement.test(current.className)) current.className += " full-screen-helper";
  133. } else {
  134. html.className = html.className.replace(reRoot, " ");
  135. current.className = current.className.replace(reElement, " ");
  136. current = null;
  137. }
  138. state = enable;
  139. setTimeout(triggerEvents, 1);
  140. }
  141. function triggerEvents() {
  142. for (var i = 0; i < changeEvents.length; i++) changeEvents[i]();
  143. }
  144. function supported() {
  145. return realsupport || getWSSO();
  146. }
  147. function request(element) {
  148. element = isValid(element);
  149. if (!element) return;
  150. if (sc) {
  151. element.requestFullscreen();
  152. } else if (mozc) {
  153. element.mozRequestFullScreen();
  154. } else if (wkc) {
  155. element.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  156. } else if (msc) {
  157. element.msRequestFullscreen();
  158. } else if (!useviewport) {
  159. fallbackRequest(element);
  160. return;
  161. }
  162. current = element;
  163. active(true);
  164. }
  165. function exit() {
  166. if (!current) return;
  167. if (sc) {
  168. d.exitFullscreen();
  169. } else if (mozc) {
  170. d.mozCancelFullScreen();
  171. } else if (wkcn) {
  172. d.webkitExitFullscreen();
  173. } else if (wkco) {
  174. d.webkitCancelFullScreen();
  175. } else if (msc) {
  176. d.msExitFullscreen();
  177. } else if (!useviewport) {
  178. if (isFS1() && wssoc) {
  179. active(false);
  180. wsso.SendKeys("{F11}");
  181. }
  182. return;
  183. }
  184. active(false);
  185. }
  186. function toggle(element) {
  187. if (current === (element || d.body)) {
  188. exit();
  189. } else {
  190. request(element);
  191. }
  192. }
  193. if (realsupport) {
  194. for (var i = events.length - 1; i >= 0; i--) addEvt(d, events[i], resizeObserver);
  195. addEvt(w, "resize", resizeObserver);
  196. }
  197. var main = {
  198. "supported": supported,
  199. "request": request,
  200. "toggle": toggle,
  201. "exit": exit,
  202. "current": function () {
  203. return current;
  204. },
  205. "state": function () {
  206. return state;
  207. },
  208. "viewport": function (enable) {
  209. allowviewport = !!enable;
  210. },
  211. "on": function (callback) {
  212. change(callback);
  213. },
  214. "off": function (callback) {
  215. change(callback, true);
  216. }
  217. };
  218. if ($ && $.extend && $.expr) {
  219. $.fn.fullScreenHelper = function (action) {
  220. var element = this[0];
  221. if (!element) return;
  222. switch (action) {
  223. case "toggle":
  224. toggle(element);
  225. break;
  226. case "request":
  227. case u:
  228. request(element);
  229. break;
  230. }
  231. };
  232. $.fullScreenHelper = function (action) {
  233. switch (action) {
  234. case "exit":
  235. exit();
  236. break;
  237. case "supported":
  238. return supported();
  239. break;
  240. case "state":
  241. return state;
  242. }
  243. };
  244. $.expr[":"].fullscreen = function (element) {
  245. return reElement.test(element.className);
  246. };
  247. if (!("onfullscreenchange" in d)) {
  248. var $d = $(d);
  249. change(function () {
  250. $d.trigger("fullscreenchange");
  251. });
  252. }
  253. }
  254. w.FullScreenHelper = main;
  255. // CommonJS
  256. if (typeof module !== "undefined" && module.exports) module.exports = main;
  257. // RequireJS
  258. if (typeof define !== "undefined") define(function () { return main; });
  259. })();