Support for 'adopted' buttongroups and anchors to prepend to

Change-Id: I2925fbc0a34cdc3e105a43669ab0082b000a4178
diff --git a/dev/js/src/buttongroup.js b/dev/js/src/buttongroup.js
index 755ad7f..040637f 100644
--- a/dev/js/src/buttongroup.js
+++ b/dev/js/src/buttongroup.js
@@ -9,7 +9,16 @@
     create : function (classes) {
       return Object.create(this)._init(classes);
     },
-    
+
+    /**
+     * Adopt existing button group element
+     */
+    adopt : function (element) {
+      const obj = Object.create(this);
+      obj._el = element;
+      return obj;
+    },
+
     // Initialize button group
     _init : function (classes) {
       const e = document.createElement('div');
@@ -21,7 +30,6 @@
       this._el = e;
       return this;
     },
-
     
     /**
      * Return main element
@@ -30,6 +38,16 @@
       return this._el;
     },
 
+    /**
+     * Define element following newly added buttons.
+     */
+    anchor : function (anchor) {
+      if (anchor.parentNode == this._el) {
+        this._anchor = anchor;
+        return true;
+      };
+      return false;
+    },
     
     /**
      * Upgrade this object to another object, 
@@ -44,6 +62,14 @@
       return this;
     },
 
+    _insert : function (tag = 'span') {
+      const span = document.createElement(tag);
+      if (this._anchor) {
+        this._el.insertBefore(span, this._anchor);
+        return span;
+      }
+      return this._el.appendChild(span);
+    },
     
     /**
      * Add button in order
@@ -51,8 +77,7 @@
      * Returns the button element
      */
     add : function (title, data, cb) {
-      
-      const b = this._el.addE('span');
+      const b = this._insert('span');
       b.setAttribute('title',title);
 
       if (data !== undefined) {
@@ -112,7 +137,7 @@
      * for use as action plugins.
      */
     addToggle : function (title, data, state) {
-      const b = this._el.addE('span');
+      const b = this._insert('span');
       b.setAttribute('title',title);
 
       if (data != undefined) {