030.jquery.helper.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc Helper functions integrated in jQuery
  4. * @package KCFinder
  5. * @version 3.12
  6. * @author Pavel Tzonkov <sunhater@sunhater.com>
  7. * @copyright 2010-2014 KCFinder Project
  8. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  9. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  10. * @link http://kcfinder.sunhater.com
  11. */
  12. (function($) {
  13. $.fn.selection = function(start, end) {
  14. var field = this.get(0);
  15. if (field.createTextRange) {
  16. var selRange = field.createTextRange();
  17. selRange.collapse(true);
  18. selRange.moveStart('character', start);
  19. selRange.moveEnd('character', end-start);
  20. selRange.select();
  21. } else if (field.setSelectionRange) {
  22. field.setSelectionRange(start, end);
  23. } else if (field.selectionStart) {
  24. field.selectionStart = start;
  25. field.selectionEnd = end;
  26. }
  27. field.focus();
  28. };
  29. $.fn.disableTextSelect = function() {
  30. return this.each(function() {
  31. if ($.agent.firefox) { // Firefox
  32. $(this).css('MozUserSelect', "none");
  33. } else if ($.agent.msie) { // IE
  34. $(this).bind('selectstart', function() {
  35. return false;
  36. });
  37. } else { //Opera, etc.
  38. $(this).mousedown(function() {
  39. return false;
  40. });
  41. }
  42. });
  43. };
  44. $.fn.outerSpace = function(type, mbp) {
  45. var selector = this.get(0),
  46. r = 0, x;
  47. if (!mbp) mbp = "mbp";
  48. if (/m/i.test(mbp)) {
  49. x = parseInt($(selector).css('margin-' + type));
  50. if (x) r += x;
  51. }
  52. if (/b/i.test(mbp)) {
  53. x = parseInt($(selector).css('border-' + type + '-width'));
  54. if (x) r += x;
  55. }
  56. if (/p/i.test(mbp)) {
  57. x = parseInt($(selector).css('padding-' + type));
  58. if (x) r += x;
  59. }
  60. return r;
  61. };
  62. $.fn.outerLeftSpace = function(mbp) {
  63. return this.outerSpace('left', mbp);
  64. };
  65. $.fn.outerTopSpace = function(mbp) {
  66. return this.outerSpace('top', mbp);
  67. };
  68. $.fn.outerRightSpace = function(mbp) {
  69. return this.outerSpace('right', mbp);
  70. };
  71. $.fn.outerBottomSpace = function(mbp) {
  72. return this.outerSpace('bottom', mbp);
  73. };
  74. $.fn.outerHSpace = function(mbp) {
  75. return (this.outerLeftSpace(mbp) + this.outerRightSpace(mbp));
  76. };
  77. $.fn.outerVSpace = function(mbp) {
  78. return (this.outerTopSpace(mbp) + this.outerBottomSpace(mbp));
  79. };
  80. $.fn.fullscreen = function() {
  81. if (!$(this).get(0))
  82. return
  83. var t = $(this).get(0),
  84. requestMethod =
  85. t.requestFullScreen ||
  86. t.requestFullscreen ||
  87. t.webkitRequestFullScreen ||
  88. t.mozRequestFullScreen ||
  89. t.msRequestFullscreen;
  90. if (requestMethod)
  91. requestMethod.call(t);
  92. else if (typeof window.ActiveXObject !== "undefined") {
  93. var wscript = new ActiveXObject("WScript.Shell");
  94. if (wscript !== null)
  95. wscript.SendKeys("{F11}");
  96. }
  97. };
  98. $.fn.toggleFullscreen = function(doc) {
  99. if ($.isFullscreen(doc))
  100. $.exitFullscreen(doc);
  101. else
  102. $(this).fullscreen();
  103. };
  104. $.exitFullscreen = function(doc) {
  105. var d = doc ? doc : document,
  106. requestMethod =
  107. d.cancelFullScreen ||
  108. d.cancelFullscreen ||
  109. d.webkitCancelFullScreen ||
  110. d.mozCancelFullScreen ||
  111. d.msExitFullscreen ||
  112. d.exitFullscreen;
  113. if (requestMethod)
  114. requestMethod.call(d);
  115. else if (typeof window.ActiveXObject !== "undefined") {
  116. var wscript = new ActiveXObject("WScript.Shell");
  117. if (wscript !== null)
  118. wscript.SendKeys("{F11}");
  119. }
  120. };
  121. $.isFullscreen = function(doc) {
  122. var d = doc ? doc : document;
  123. return (d.fullScreenElement && (d.fullScreenElement !== null)) ||
  124. (d.fullscreenElement && (d.fullscreenElement !== null)) ||
  125. (d.msFullscreenElement && (d.msFullscreenElement !== null)) ||
  126. d.mozFullScreen || d.webkitIsFullScreen;
  127. };
  128. $.clearSelection = function() {
  129. if (document.selection)
  130. document.selection.empty();
  131. else if (window.getSelection)
  132. window.getSelection().removeAllRanges();
  133. };
  134. $.$ = {
  135. htmlValue: function(value) {
  136. return value
  137. .replace(/\&/g, "&amp;")
  138. .replace(/\"/g, "&quot;")
  139. .replace(/\'/g, "&#39;");
  140. },
  141. htmlData: function(value) {
  142. return value.toString()
  143. .replace(/\&/g, "&amp;")
  144. .replace(/\</g, "&lt;")
  145. .replace(/\>/g, "&gt;")
  146. .replace(/\ /g, "&nbsp;")
  147. .replace(/\"/g, "&quot;")
  148. .replace(/\'/g, "&#39;");
  149. },
  150. jsValue: function(value) {
  151. return value
  152. .replace(/\\/g, "\\\\")
  153. .replace(/\r?\n/, "\\\n")
  154. .replace(/\"/g, "\\\"")
  155. .replace(/\'/g, "\\'");
  156. },
  157. basename: function(path) {
  158. var expr = /^.*\/([^\/]+)\/?$/g;
  159. return expr.test(path)
  160. ? path.replace(expr, "$1")
  161. : path;
  162. },
  163. dirname: function(path) {
  164. var expr = /^(.*)\/[^\/]+\/?$/g;
  165. return expr.test(path)
  166. ? path.replace(expr, "$1")
  167. : '';
  168. },
  169. inArray: function(needle, arr) {
  170. if (!$.isArray(arr))
  171. return false;
  172. for (var i = 0; i < arr.length; i++)
  173. if (arr[i] == needle)
  174. return true;
  175. return false;
  176. },
  177. getFileExtension: function(filename, toLower) {
  178. if (typeof toLower == 'undefined') toLower = true;
  179. if (/^.*\.[^\.]*$/.test(filename)) {
  180. var ext = filename.replace(/^.*\.([^\.]*)$/, "$1");
  181. return toLower ? ext.toLowerCase(ext) : ext;
  182. } else
  183. return "";
  184. },
  185. escapeDirs: function(path) {
  186. var fullDirExpr = /^([a-z]+)\:\/\/([^\/^\:]+)(\:(\d+))?\/(.+)$/,
  187. prefix = "";
  188. if (fullDirExpr.test(path)) {
  189. var port = path.replace(fullDirExpr, "$4");
  190. prefix = path.replace(fullDirExpr, "$1://$2");
  191. if (port.length)
  192. prefix += ":" + port;
  193. prefix += "/";
  194. path = path.replace(fullDirExpr, "$5");
  195. }
  196. var dirs = path.split('/'),
  197. escapePath = '', i = 0;
  198. for (; i < dirs.length; i++)
  199. escapePath += encodeURIComponent(dirs[i]) + '/';
  200. return prefix + escapePath.substr(0, escapePath.length - 1);
  201. },
  202. kuki: {
  203. prefix: '',
  204. duration: 356,
  205. domain: '',
  206. path: '',
  207. secure: false,
  208. set: function(name, value, duration, domain, path, secure) {
  209. name = this.prefix + name;
  210. if (duration == null) duration = this.duration;
  211. if (secure == null) secure = this.secure;
  212. if ((domain == null) && this.domain) domain = this.domain;
  213. if ((path == null) && this.path) path = this.path;
  214. secure = secure ? true : false;
  215. var date = new Date();
  216. date.setTime(date.getTime() + (duration * 86400000));
  217. var expires = date.toGMTString();
  218. var str = name + '=' + value + '; expires=' + expires;
  219. if (domain != null) str += '; domain=' + domain;
  220. if (path != null) str += '; path=' + path;
  221. if (secure) str += '; secure';
  222. return (document.cookie = str) ? true : false;
  223. },
  224. get: function(name) {
  225. name = this.prefix + name;
  226. var nameEQ = name + '=';
  227. var kukis = document.cookie.split(';');
  228. var kuki;
  229. for (var i = 0; i < kukis.length; i++) {
  230. kuki = kukis[i];
  231. while (kuki.charAt(0) == ' ')
  232. kuki = kuki.substring(1, kuki.length);
  233. if (kuki.indexOf(nameEQ) == 0)
  234. return kuki.substring(nameEQ.length, kuki.length);
  235. }
  236. return null;
  237. },
  238. del: function(name) {
  239. return this.set(name, '', -1);
  240. },
  241. isSet: function(name) {
  242. return (this.get(name) != null);
  243. }
  244. }
  245. };
  246. })(jQuery);