Snippets
Created by
Former user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | // Anonymous "self-invoking" function
(function() {
/*\
|*|
|*| :: cookies.js ::
|*|
|*| A complete cookies reader/writer framework with full unicode support.
|*|
|*| Revision #1 - September 4, 2014
|*|
|*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
|*| https://developer.mozilla.org/User:fusionchess
|*|
|*| This framework is released under the GNU Public License, version 3 or later.
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*| Syntaxes:
|*|
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*| * docCookies.getItem(name)
|*| * docCookies.removeItem(name[, path[, domain]])
|*| * docCookies.hasItem(name)
|*| * docCookies.keys()
|*|
\*/
var docCookies = {
getItem: function(sKey) {
if (!sKey) {
return null;
}
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
return false;
}
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function(sKey, sPath, sDomain) {
if (!this.hasItem(sKey)) {
return false;
}
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function(sKey) {
if (!sKey) {
return false;
}
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: function() {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
}
return aKeys;
}
};
var url = window.location.href;
var timer = null;
if (url.indexOf('translateMode=1') !== -1) {
docCookies.setItem("SIMTranslatemode", "1")
console.log('translate enabled; cookie set');
} else if (url.indexOf('translateMode=0') !== -1) {
docCookies.removeItem("SIMTranslatemode");
console.log('translate disabled; cookie removed');
}
if (!(docCookies.getItem("SIMTranslatemode") && docCookies.getItem("SIMTranslatemode") === "1")) {
console.warn('translations not enabled');
return;
}
//add styling
var head = document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
head.appendChild(style);
style.innerHTML = 'span[data-namespaceid][data-language][data-name]{ outline: 1px solid #00ff00; display: inline-block; min-width: 15px; min-height: 5px; position: relative; }';
style.innerHTML += 'span[data-namespaceid][data-language][data-name]:after{ content:attr(data-name); position: absolute; display: inline-block; top: -1.4em; font-size: 1em; right: 0; opacity: 0.2; }';
var saveLabelURL = translationUrl;
if( typeof $ === 'undefined' && typeof jQuery ==='undefined' ){
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'http://code.jquery.com/jquery-2.1.4.min.js';
script.type = 'text/javascript';
head.appendChild(script);
}
// Poll for jQuery to come into existance
var checkReady = function(callback) {
if (window.jQuery) {
callback(jQuery);
} else {
window.setTimeout(function() {
checkReady(callback);
}, 20);
}
};
// Start polling...
checkReady(function($) {
$(function() {
setupHandlers();
});
});
function saveLabel( _name, _lang, _value, _nsid) {
if ( _name && _lang && _value && _nsid ) {
$.ajax({
method: "GET",
url: saveLabelURL,
data: {
"LBL_NAME": _name,
"LBL_LANGUAGE": _lang,
"LBL_VALUE": _value,
"LBL_NAMESPACEID": _nsid
}
})
.done(function(msg) {
if (msg.indexOf("ERR:") === -1) {
console.log("Label " + _name + " Saved ");
} else {
console.error("Error saving label: " + msg);
}
});
} else {
console.warn( "did not save label - missing attributes or value? " );
}
}
function setupHandlers() {
$('span[data-namespaceid][data-language][data-name]').on('mousedown', function(event) {
// console.log('mousedown translatable field');
var $label = $(this);
timer = setTimeout(function() {
showInSiteTranslationDialog($label);
timer = null;
}, 1000);
});
$('span[data-namespaceid][data-language][data-name]').on('mouseup', function(event) {
if (timer) {
clearTimeout(timer);
timer = null;
}
});
$('span[data-namespaceid][data-language][data-name]').on('mouseout', function(event) {
if (timer) {
clearTimeout(timer);
timer = null;
}
});
}
function showInSiteTranslationDialog($label) {
if (typeof($label.attr('data-name') && $label.attr('data-language') && $label.attr('data-namespaceid')) !== 'undefined') {
var LBL_NAME = $label.attr('data-name');
var LBL_LANGUAGE = $label.attr('data-language');
var LBL_VALUE = prompt("Please enter the new value for label " + LBL_NAME, $label.html());
var LBL_NAMESPACEID = $label.attr('data-namespaceid');
saveLabel(LBL_NAME, LBL_LANGUAGE, LBL_VALUE, LBL_NAMESPACEID);
} else {
console.error('malformed label - cannot change value');
}
}
})();
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.