src/pyams_skin/resources/js/myams-browser.js
changeset 556 3b84c6308d55
child 486 48b7cea0d903
equal deleted inserted replaced
-1:000000000000 556:3b84c6308d55
       
     1 /**
       
     2  * MyAMS browser related features
       
     3  */
       
     4 (function($, globals) {
       
     5 
       
     6 	var MyAMS = globals.MyAMS,
       
     7 		ams = MyAMS;
       
     8 
       
     9 	MyAMS.browser = {
       
    10 
       
    11 		/**
       
    12 		 * Get IE version
       
    13 		 */
       
    14 		getInternetExplorerVersion: function() {
       
    15 			var rv = -1;
       
    16 			if (navigator.appName === "Microsoft Internet Explorer") {
       
    17 				var ua = navigator.userAgent;
       
    18 				var re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
       
    19 				if (re.exec(ua) !== null) {
       
    20 					rv = parseFloat(RegExp.$1);
       
    21 				}
       
    22 			}
       
    23 			return rv;
       
    24 		},
       
    25 
       
    26 		/**
       
    27 		 * Display alert for old IE version
       
    28 		 */
       
    29 		checkVersion: function() {
       
    30 			var msg = "You're not using Windows Internet Explorer.";
       
    31 			var ver = this.getInternetExplorerVersion();
       
    32 			if (ver > -1) {
       
    33 				if (ver >= 8) {
       
    34 					msg = "You're using a recent copy of Windows Internet Explorer.";
       
    35 				} else {
       
    36 					msg = "You should upgrade your copy of Windows Internet Explorer.";
       
    37 				}
       
    38 			}
       
    39 			if (globals.alert) {
       
    40 				globals.alert(msg);
       
    41 			}
       
    42 		},
       
    43 
       
    44 		/**
       
    45 		 * Check if IE is in version 8 or lower
       
    46 		 */
       
    47 		isIE8orlower: function() {
       
    48 			var msg = "0";
       
    49 			var ver = this.getInternetExplorerVersion();
       
    50 			if (ver > -1) {
       
    51 				if (ver >= 9) {
       
    52 					msg = 0;
       
    53 				} else {
       
    54 					msg = 1;
       
    55 				}
       
    56 			}
       
    57 			return msg;
       
    58 		},
       
    59 
       
    60 
       
    61 		/**
       
    62 		 * Copy selection to clipboard
       
    63 		 *
       
    64 		 * If 'text' argument is provided, given text is copied to clipboard.
       
    65 		 * Otherwise, text ou event's source is copied.
       
    66 		 * Several methods are tested to do clipboard copy (based on browser features); il copy can't be done,
       
    67 		 * a prompt is displayed to allow user to make a manual copy.
       
    68 		 */
       
    69 		copyToClipboard: function(text) {
       
    70 
       
    71 			function doCopy(text) {
       
    72 				var copied = false;
       
    73 				if (window.clipboardData && window.clipboardData.setData) {
       
    74 					// IE specific code
       
    75 					copied = clipboardData.setData("Text", text);
       
    76 				} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
       
    77 					var textarea = $("<textarea>");
       
    78 					textarea.val(text);
       
    79 					textarea.css('position', 'fixed');  // Prevent scrolling to bottom of page in MS Edge.
       
    80 					textarea.appendTo($('body'));
       
    81 					textarea.get(0).select();
       
    82 					try {
       
    83 						document.execCommand("copy");  // Security exception may be thrown by some browsers.
       
    84 						copied = true;
       
    85 					} catch (ex) {
       
    86 						if (console) {
       
    87 							console.warn && console.warn("Copy to clipboard failed.", ex);
       
    88 						}
       
    89 					} finally {
       
    90 						textarea.remove();
       
    91 					}
       
    92 				}
       
    93 				if (copied) {
       
    94 					ams.skin.smallBox('success',
       
    95 									  {
       
    96 										  title: text.length > 1
       
    97 											  ? ams.i18n.CLIPBOARD_TEXT_COPY_OK
       
    98 											  : ams.i18n.CLIPBOARD_CHARACTER_COPY_OK,
       
    99 										  icon: 'fa fa-fw fa-info-circle font-xs align-top margin-top-10',
       
   100 										  timeout: 3000
       
   101 									  });
       
   102 				} else if (globals.prompt) {
       
   103 					globals.prompt(MyAMS.i18n.CLIPBOARD_COPY, text);
       
   104 				}
       
   105 			}
       
   106 
       
   107 			if (text === undefined) {
       
   108 				return function() {
       
   109 					var source = $(this);
       
   110 					var text = source.text();
       
   111 					source.parents('.btn-group').removeClass('open');
       
   112 					doCopy(text);
       
   113 				};
       
   114 			} else {
       
   115 				doCopy(text);
       
   116 			}
       
   117 		}
       
   118 	};
       
   119 
       
   120 })(jQuery, this);