IE ES5 Javascript Konvertierung

  • 23 November 2018
  • GeeX

default contructors

IE kennt keine default Constructors

function appendLine(text="",color="#333")
------------------------------------------------------------------------------------
function appendLine(text,color){
    if(!color||color=="undefined")color="#333";
    if(!text||text=="undefined")text="";
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------

startsWith

Protoype Funktion für startsWith

str.startWith('cmd')
------------------------------------------------------------------------------------
if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(searchString, position) {
     position = position || 0;
    return this.indexOf(searchString, position) === position;
  };
}
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------

 Arrow Functions

 data.forEach(user => {});
------------------------------------------------------------------------------------

 data.forEach(function (user) {});

 

 

Promises

IE kennt keinen Arrow Operator =>

daher verwenden ich like Bluebird Promises

new Promise(function(resolve, reject){ });

statt

new Promise((resolve, reject)=>{}); 

Klassen

IE kennt keine Klassen, diese können durch Protoypen emuliert werden. 

"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var AWRIError = function (_Error) {
    _inherits(AWRIError, _Error);

    function AWRIError(msg) {
        _classCallCheck(this, AWRIError);

        var _this = _possibleConstructorReturn(this, (AWRIError.__proto__ || Object.getPrototypeOf(AWRIError)).call(this, msg));

        _this.msg = msg;
        _this.name = "AWRIConnectError";
        return _this;
    }

    return AWRIError;
}(Error);

 

'use strict';

class AWRIError extends Error{
    constructor(msg){
    super(msg);
    this.msg=msg;
    this.name="AWRIConnectError"   
    }
}

 

Beispiel AWRI connect ES5

"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var AWRI = function () {
    function AWRI(host, endpoint) {
        _classCallCheck(this, AWRI);

        this.host = host;
        this.endpoint = endpoint;
        this._uid = null;
        this._name = null;
        this._token = null;
        this._session = null;
        console.log('AWRI constructed:' + host + endpoint);
    }

    _createClass(AWRI, [{
        key: "connect",
        value: function connect() {
            return new Promise(function (resolve, reject) {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.withCredentials = true;
                xmlhttp.crossDomain = true;
                xmlhttp.open("POST", awri.host + "/" + awri.endpoint + "/system/connect.json", false);
                xmlhttp.setRequestHeader("Content-Type", "application/json");
                xmlhttp.setRequestHeader("X-CSRF-Token", awri._token);

                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
                        var response = JSON.parse(xmlhttp.responseText);
                        var user = response.user;
                        awri._session = response.session_name + "_" + response.sessid;
                        resolve(user);
                        if (typeof awriconnect_user_connected == 'function') awriconnect_user_connected(user);
                    }
                    if (xmlhttp.status == 404) return reject( new AWRIError(xmlhttp.status+' Nicht gefunden.'));
                    if (xmlhttp.status != 200) return reject(new AWRIError(xmlhttp.status + ' Fehler connect.'));
                };
                xmlhttp.send();
            });
        }
    }, {
        key: "token",
        value: function token() {
            return new Promise(function (resolve, reject) {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.withCredentials = true;
                xmlhttp.crossDomain = true;
                xmlhttp.open("GET", awri.host + "/services/session/token", false);
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
                        awri._token = xmlhttp.responseText;
                        //AWRI.log('connect:'+awri._token);
                        console.log(awri._token);

                        resolve(awri._token);
                    }

                    if (xmlhttp.status != 200) reject(new AWRIError(xmlhttp.status + ' Fehler in connect.'));
                };
                xmlhttp.send(null);
            });
        }        
    },{  //Func
...

 

 

class AWRI{
    constructor(host,endpoint){ 
           
    this.host=host;
    this.endpoint=endpoint;
    this._uid=null;
    this._name=null;
    this._token=null;
    this._session=null;
    AWRI.log("AWRI.constructed: "+ this.host+'/'+this.endpoint);        
    }
    static async(){
        return false;
    }

    static log(line){
    if(AWRI.log)AWRI.log(line);
    }
    
    static setLogger(log){
        AWRI.log=log;
    }

     connect() {        
        return new Promise(function (resolve, reject) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.withCredentials = true;
            xmlhttp.crossDomain = true;
            xmlhttp.open("POST", awri.host + "/" + awri.endpoint + "/system/connect.json", false);
            xmlhttp.setRequestHeader("Content-Type", "application/json");
            xmlhttp.setRequestHeader("X-CSRF-Token", awri._token);
        
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
                   var response = JSON.parse(xmlhttp.responseText);
                   var  user = response.user;
                   awri._session=response.session_name+"_"+response.sessid;
                   resolve(user);
                if(typeof awriconnect_user_connected == 'function') awriconnect_user_connected(user);                                                                                    
                }
                //if (xmlhttp.status == 404) return reject( new AWRIError(xmlhttp.status+' Nicht gefunden.'));
                if (xmlhttp.status != 200) return reject( new AWRIError(xmlhttp.status+' Fehler connect.'));
            };
        xmlhttp._send(null);
        })
        }
    
    
    token() {
        return new Promise(function (resolve, reject) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.withCredentials = true;
            xmlhttp.crossDomain = true;
            xmlhttp.open("GET",  awri.host + "/services/session/token", false);
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
                    awri._token = xmlhttp.responseText;
                   //AWRI.log('connect:'+awri._token);
                   console.log(awri._token);
          
                resolve(awri._token);
            }
             
                if (xmlhttp.status != 200) reject(  new AWRIError(xmlhttp.status+' Fehler in connect.'));
            };
            xmlhttp.send();
            });
           
        }
...