XWidgetSoft Forum

XWidget & XLaunchpad , Desktop customization
It is currently May 7th, 2025, 12:17 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: January 30th, 2018, 10:56 am 
Offline
User avatar

Joined: July 29th, 2013, 9:13 am
Posts: 609
Xwidget is based upon JScript and as such has inherited some of the limitations of that product. JScript is a fully featured version of javascript built by Microsoft, however it lacks certain key features that would make it more usable. One such missing feature is the foreach construct.

Code:
 var arr = [1, 2, 3, 4];
 arr.forEach(function (item) {
  // do something
        .
        .
        .
 });


These loops are not valid under Xwidget and a runtime error is generated: "runtime error: Object doesn't support this property or method". This is because JScript uses the JavaScript feature set as it existed in IE8. Even in Windows 10, the Windows Script Host is limited to JScript 5.7 that does not support forEach. Instead, all the forEach loops have to be replaced by standard for...loops in the following manner:

Code:
var arr = [1, 2, 3, 4];
for (var i = 0, len = arr.length; i < len; i++) {
  // do something
        .
        .
        .
}


The for loops are a little more cumbersome but this conversion needs to be done. Strangely enough the Yahoo widget engine supports forEach loops even though it is based upon an earlier version of the ECMAscript standards. I'm not sure what is going on here and why, suffice to say that any of your Yahoo widget code that has forEach loops in it will have to be replaced by simple old-school for loops.

Here is a real example:

Code:
Code:
   playListText.forEach(function (ele) {
       ele.data = "";
   });


Code:
Code:
   for (var i = 0, len = playListText.length; i < len; i++ ) {
           playListText[i].text = "";
   }


The lack of a foreach statement does remove some powerful capabilities from Xwidget. In the YWE you can declare a mousedrag function in code for each occurrence of the object within a loop:

Code:
Code:
// playListText is an array of 11 members
playListText.forEach(function (ele, i) {
   ele.onMouseDrag = function () {
        dropPosition = 0;
        catchFileDrop(Data);
   };
});


The above simply can't be done in Xwidgets due to the way that all events are defined within the main.xul file in combination with the lack of a foreach statement. In Xwidget each playListText object (of which there are 11 in this widget) has to be defined in the xul and then have its own function within script.js, lengthening this specific code by a factor of 12.

Code:

Code:
function playListText0OnDragDrop(Sender,Data,Point)
{
 dropPosition = 0;
 catchFileDrop(Data);
}

function playListText1OnDragDrop(Sender,Data,Point)
{
 dropPosition = 1;
 catchFileDrop(Data);
}

function playListText3OnDragDrop(Sender,Data,Point)
{
 dropPosition = 3;
 catchFileDrop(Data);
}

function playListText4OnDragDrop(Sender,Data,Point)
{
 dropPosition = 4;
 catchFileDrop(Data);
}
.
.
.
// and so on until 11 on drag/drops have been defined



This could be fixed by Tony implementing the foreach statement as a polyfill, ie implementing the foreach functionality in his own code, that would mean we gain functionality that javascript has that jscript lacks. It means that code designed for javascript would work in Xwidgets with little modification.

Such a polyfill already exists in javascript for those implementations that don't recognise it:

Including this code at the beginning of your widget's javascript should implement the javascript foreach function as a polyfill, then you will have access to the foreach functionality:

Code:
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {

  Array.prototype.forEach = function(callback/*, thisArg*/) {

    var T, k;

    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    // 1. Let O be the result of calling toObject() passing the
    // |this| value as the argument.
    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get() internal
    // method of O with the argument "length".
    // 3. Let len be toUint32(lenValue).
    var len = O.length >>> 0;

    // 4. If isCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    // 5. If thisArg was supplied, let T be thisArg; else let
    // T be undefined.
    if (arguments.length > 1) {
      T = arguments[1];
    }

    // 6. Let k be 0.
    k = 0;

    // 7. Repeat while k < len.
    while (k < len) {

      var kValue;

      // a. Let Pk be ToString(k).
      //    This is implicit for LHS operands of the in operator.
      // b. Let kPresent be the result of calling the HasProperty
      //    internal method of O with argument Pk.
      //    This step can be combined with c.
      // c. If kPresent is true, then
      if (k in O) {

        // i. Let kValue be the result of calling the Get internal
        // method of O with argument Pk.
        kValue = O[k];

        // ii. Call the Call internal method of callback with T as
        // the this value and argument list containing kValue, k, and O.
        callback.call(T, kValue, k, O);
      }
      // d. Increase k by 1.
      k++;
    }
    // 8. return undefined.
  };
}


The above polyfill won't give you the ability to assign functions dynamically within a foreach loop but it will give you basic foreach capabilities.

PS. Jim, add this please to the list of suggestions.


Top
 Profile  
 
PostPosted: March 9th, 2018, 6:28 am 
Offline
User avatar

Joined: July 29th, 2013, 9:13 am
Posts: 609
Jim, please - add to the bug list


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: Google [Bot] and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron

Powered by phpBB® Forum Software © phpBB Group