Twizy: Drivemode Button Editor

../../../../_images/drivemode-config.png

This plugin has been added to the Twizy code. It’s used here as a more complex example of what can be done by plugins.

It’s an editor for the drivemode buttons the Twizy adds to the dashboard for quick tuning profile changes. The editor allows to change the layout (number and order of buttons) and the profiles to use.

It includes a profile selector built with the dialog widget, that retrieves the available profiles from the OVMS config store.

Install: not necessary if vehicle Twizy is configured (see “Twizy” menu). If you’d like to test this for another vehicle, add as a page plugin e.g. /test/dmconfig.

drivemode-config.htm (hint: right click, save as)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!--
  Twizy page plugin: Drivemode Button Configuration
  Note: included in firmware v3.2
-->

<style>
.fullscreened .panel-single .panel-body {
  padding: 10px;
}

.btn-group-lg>.btn,
.btn-lg {
  padding: 10px 3px;
  overflow: hidden;
}
#loadmenu .btn {
  font-weight: bold;
}
.btn-group.btn-group-lg.exchange {
  width: 2%;
}
#editor .btn:hover {
  background-color: #e6e6e6;
}
#editor .btn-group-lg>.btn,
#editor .btn-lg {
  padding: 10px 0px;
  font-size: 15px;
}
#editor .exchange .btn {
  font-weight: bold;
}
.night #editor .btn {
  color: #000;
  background: #888;
}
.night #editor .btn.focus,
.night #editor .btn:focus,
.night #editor .btn:hover {
  background-color: #e0e0e0;
}

.radio-list {
  height: 313px;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 15px;
}
.radio-list .radio {
  overflow: hidden;
}
.radio-list .key {
  min-width: 20px;
  display: inline-block;
  text-align: center;
  margin: 0 10px;
}
.radio-list kbd {
  min-width: 60px;
  display: inline-block;
  text-align: center;
  margin: 0 20px 0 10px;
}
.radio-list .radio label {
  width: 100%;
  text-align: left;
  padding: 8px 30px;
}
.radio-list .radio label.active {
  background-color: #337ab7;
  color: #fff;
  outline: none;
}
.radio-list .radio label.active input {
  outline: none;
}
.night .radio-list .radio label:hover {
  color: #fff;
}
</style>

<div class="panel panel-primary">
  <div class="panel-heading">Drivemode Button Configuration</div>
  <div class="panel-body">
    <form action="#">

    <p id="info">Loading button configuration…</p>

    <div id="loadmenu" class="btn-group btn-group-justified"></div>
    <div id="editor" class="btn-group btn-group-justified">
      <div class="btn-group btn-group-lg add back">
        <button type="button" class="btn" title="Add button"></button>
      </div>
    </div>

    <br>
    <br>
    <div class="text-center">
      <button type="button" class="btn btn-default" onclick="reloadpage()">Reset</button>
      <button type="button" class="btn btn-primary action-save">Save</button>
    </div>

    </form>
  </div>
  <div class="panel-footer">
    <a class="btn btn-sm btn-default" target="#main" href="/dashboard">Dashboard</a>
    <a class="btn btn-sm btn-default" target="#main" href="/xrt/drivemode">Drivemode</a>
  </div>
</div>

<div id="key-dialog" />

<script>
(function(){

  var $menu = $('#loadmenu'), $edit = $('#editor'), $back = $edit.find('.back');

  var pbuttons = [0,1,2,3];
  var plist = [
    { label: "STD", title: "Standard" },
    { label: "PWR", title: "Power" },
    { label: "ECO", title: "Economy" },
    { label: "ICE", title: "Winter" },
  ];
  
  // load profile list & button config:
  $('.panel').addClass("loading");
  var plistloader = loadcmd('config list xrt').then(function(data) {
    var lines = data.split('\n'), line, i, key;
    for (i = 0; i < lines.length; i++) {
      line = lines[i].match(/profile([0-9]{2})\.?([^:]*): (.*)/);
      if (line && line.length == 4) {
        key = Number(line[1]);
        if (key < 1 || key > 99) continue;
        if (!plist[key]) plist[key] = {};
        plist[key][line[2]||"profile"] = line[3];
        continue;
      }
      line = lines[i].match(/profile_buttons: (.*)/);
      if (line && line.length == 2) {
        try {
          pbuttons = JSON.parse("[" + line[1] + "]");
        } catch (e) {
          console.error(e);
        }
      }
    }
  });

  // prep key dialog:
  $('#key-dialog').dialog({
    show: false,
    title: "Select Profile",
    buttons: [{ label: 'Cancel', btnClass: 'default' },{ label: 'Select', btnClass: 'primary' }],
    onShow: function(input) {
      var $this = $(this), dlg = $this.data("dialog");
      $this.addClass("loading");
      plistloader.then(function(data) {
        var curkey = dlg.options.key || 0, i, label, title;
        $plist = $('<div class="radio-list" data-toggle="buttons" />');
        for (i = 0; i <= Math.min(99, plist.length-1); i++) {
          if (plist[i] && (i==0 || plist[i].profile)) {
            label = plist[i].label || "–";
            title = plist[i].title || (plist[i].profile.substr(0, 10) + "…");
          } else {
            label = "–";
            title = "–new–";
          }
          $plist.append('<div class="radio"><label class="btn">' +
            '<input type="radio" name="key" value="' + i + '"><span class="key">' +
            ((i==0) ? "STD" : ("#" + ((i<10)?'0':'') + i)) + '</span> <kbd>' +
            encode_html(label) + '</kbd> ' + encode_html(title) + '</label></div>');
        }
        $this.find('.modal-body').html($plist).find('input[value="'+curkey+'"]')
          .prop("checked", true).parent().addClass("active");
        $plist
          .on('dblclick', 'label.btn', function(ev) { $this.dialog('triggerButton', 1); })
          .on('keypress', function(ev) { if (ev.which==13) $this.dialog('triggerButton', 1); });
        $this.removeClass("loading");
      });
    },
    onShown: function(input) {
      $(this).find('.btn.active').focus();
    },
    onHide: function(input) {
      var $this = $(this), dlg = $this.data("dialog");
      var key = $this.find('input[name="key"]:checked').val();
      if (key !== undefined && input.button && input.button.index)
        dlg.options.onAction.call(this, key);
    },
  });

  // profile selection:
  $('#loadmenu').on('click', 'button', function(ev) {
    var $this = $(this);
    $('#key-dialog').dialog({
      show: true,
      key: $this.val(),
      onAction: function(key) {
        var prof = plist[key] || {};
        $this.val(key);
        $this.attr("title", prof.title || "");
        $this.text(prof.label || ("#"+((key<10)?"0":"")+key));
      },
    });
  });

  // create buttons:
  
  function addButton(key, front) {
    var prof = plist[key] || {};
    if ($menu[0].childElementCount == 0) {
      $back.before('\
        <div class="btn-group btn-group-lg add front">\
          <button type="button" class="btn" title="Add button">✚</button>\
        </div>\
        <div class="btn-group btn-group-lg remove">\
          <button type="button" class="btn" title="Remove button">✖</button>\
        </div>');
    } else {
      $back.before('\
        <div class="btn-group btn-group-lg exchange">\
          <button type="button" class="btn" title="Exchange buttons">⇄</button>\
        </div>\
        <div class="btn-group btn-group-lg remove">\
          <button type="button" class="btn" title="Remove button">✖</button>\
        </div>');
    }
    var $btn = $('\
      <div class="btn-group btn-group-lg">\
        <button type="button" value="{key}" class="btn btn-default" title="{title}">{label}</button>\
      </div>'
      .replace("{key}", key)
      .replace("{title}", encode_html(prof.title || ""))
      .replace("{label}", encode_html(prof.label || "#"+((key<10)?"0":"")+key)));
    if (front)
      $menu.prepend($btn);
    else
      $menu.append($btn);
  }
  
  plistloader.then(function() {
    var key, prof;
    for (var i = 0; i < pbuttons.length; i++) {
      addButton(pbuttons[i]);
    }
    $('.panel').removeClass("loading");
    $('#info').text("Click button to select profile:");
  });

  // editor buttons:
  $('#editor').on('click', '.add .btn', function(ev) {
    addButton(0, $(this).parent().hasClass("front"));
  }).on('click', '.remove .btn', function(ev) {
    var $this = $(this), pos = $edit.find('.btn').index(this) >> 1;
    $($menu.children().get(pos)).detach();
    if (pos > 0 || $menu[0].childElementCount == 0)
      $this.parent().prev().detach();
    else if ($menu[0].childElementCount != 0)
      $this.parent().next().detach();
    $this.parent().detach();
  }).on('click', '.exchange .btn', function(ev) {
    var pos = $edit.find('.btn').index(this) >> 1;
    if (pos > 0) $($menu.children().get(pos)).insertBefore($($menu.children().get(pos-1)));
  });

  // save:
  $('.action-save').on('click', function(ev) {
    pbuttons = [];
    $menu.find('.btn').each(function() { pbuttons.push(this.value) });
    loadcmd('config set xrt profile_buttons ' + pbuttons.toString())
      .fail(function(request, textStatus, errorThrown) {
        confirmdialog("Save Configuration", xhrErrorInfo(request, textStatus, errorThrown), ["Close"]);
      })
      .done(function(res) {
        confirmdialog("Save Configuration", res, ["Close"]);
      });
  });

})();
</script>