[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [plasma-desktop] containments/panel/contents/code: [Panel Containment] Replace restore order logic b
From:       Kai Uwe Broulik <null () kde ! org>
Date:       2016-12-31 12:45:29
Message-ID: E1cNJ2P-00011M-0x () code ! kde ! org
[Download RAW message or body]

Git commit efd188db583821f092acbfb60ced1fe528bf306c by Kai Uwe Broulik.
Committed on 31/12/2016 at 12:44.
Pushed by broulik into branch 'master'.

[Panel Containment] Replace restore order logic by "algorithms"

Replace the convoluted make an object with the order of applets by iterating
them and then add them in order by a sort() and forEach().
This makes the code much easier to follow.

Differential Revision: https://phabricator.kde.org/D3834

M  +16   -21   containments/panel/contents/code/LayoutManager.js

https://commits.kde.org/plasma-desktop/efd188db583821f092acbfb60ced1fe528bf306c

diff --git a/containments/panel/contents/code/LayoutManager.js \
b/containments/panel/contents/code/LayoutManager.js index 45309b31..11deeddc 100644
--- a/containments/panel/contents/code/LayoutManager.js
+++ b/containments/panel/contents/code/LayoutManager.js
@@ -29,30 +29,25 @@ function restore() {
     //array, a cell for encoded item order
     var itemsArray = configString.split(";");
 
-    //map applet id->order in panel
-    var idsOrder = new Object();
-    //map order in panel -> applet pointer
-    var appletsOrder = new Object();
+    var applets = plasmoid.applets
+    applets.sort(function (a, b) {
+        var aIdx = itemsArray.indexOf(String(a.id))
+        var bIdx = itemsArray.indexOf(String(b.id))
+
+        // applets that aren't listed in the order are sorted last
+        if (aIdx === -1 && bIdx !== -1) {
+            return 1
+        } else if (aIdx !== -1 && bIdx === -1) {
+            return -1
+        }
 
-    for (var i = 0; i < itemsArray.length; i++) {
-        //property name: applet id
-        //property value: order
-        idsOrder[itemsArray[i]] = i;
-    }
+        return aIdx - bIdx
+    })
 
-    for (var i = 0; i < plasmoid.applets.length; ++i) {
-        if (idsOrder[plasmoid.applets[i].id] !== undefined) {
-            appletsOrder[idsOrder[plasmoid.applets[i].id]] = plasmoid.applets[i];
-        //ones that weren't saved in AppletOrder go to the end
-        } else {
-            appletsOrder["unordered"+i] = plasmoid.applets[i];
-        }
-    }
+    applets.forEach(function (applet) {
+        root.addApplet(applet, -1, -1)
+    })
 
-    //finally, restore the applets in the correct order
-    for (var i in appletsOrder) {
-        root.addApplet(appletsOrder[i], -1, -1)
-    }
     //rewrite, so if in the orders there were now invalid ids or if some were \
missing creates a correct list instead  save();
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic