Snippets

Foreach Selligent translations in-page editing

Created by Andy Somers
CREATE PROCEDURE SP_SETLABEL
(
    @LBL_NAME Varchar(50),
    @LBL_LANGUAGE Varchar(50),
    @LBL_VALUE Varchar(250),
    @LBL_NAMESPACEID int
)
AS
BEGIN
SET NOCOUNT ON;

    IF EXISTS (SELECT 1 FROM LABELS
        WHERE NAME=@LBL_NAME
            and LANGUAGE=@LBL_LANGUAGE
            and NAMESPACEID=@LBL_NAMESPACEID 
    )
    BEGIN
        UPDATE LABELS set VALUE=@LBL_VALUE, MODIFIED_DT=getdate()
        WHERE NAME=@LBL_NAME
            and LANGUAGE=@LBL_LANGUAGE
            and NAMESPACEID=@LBL_NAMESPACEID 
    END
ELSE
    BEGIN
        INSERT into LABELS ( NAME, LANGUAGE, VALUE, NAMESPACEID, CREATED_DT) Values ( @LBL_NAME, @LBL_LANGUAGE, @LBL_VALUE, @LBL_NAMESPACEID, getdate() )
    END

END
// 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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.