From 75687711f35355bc30e4829439384aab28fcac6d Mon Sep 17 00:00:00 2001 From: href Date: Wed, 1 Sep 2021 10:30:18 +0200 Subject: Commit all the changes that hasn't been committed + updates. --- priv/static/js/metricsgraphics/layout/button.js | 126 ++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 priv/static/js/metricsgraphics/layout/button.js (limited to 'priv/static/js/metricsgraphics/layout/button.js') diff --git a/priv/static/js/metricsgraphics/layout/button.js b/priv/static/js/metricsgraphics/layout/button.js new file mode 100644 index 0000000..b122273 --- /dev/null +++ b/priv/static/js/metricsgraphics/layout/button.js @@ -0,0 +1,126 @@ +MG.button_layout = function(target) { + 'use strict'; + this.target = target; + this.feature_set = {}; + this.public_name = {}; + this.sorters = {}; + this.manual = []; + this.manual_map = {}; + this.manual_callback = {}; + + this._strip_punctuation = function(s) { + var punctuationless = s.replace(/[^a-zA-Z0-9 _]+/g, ''); + var finalString = punctuationless.replace(/ +?/g, ''); + return finalString; + }; + + this.data = function(data) { + this._data = data; + return this; + }; + + this.manual_button = function(feature, feature_set, callback) { + this.feature_set[feature] = feature_set; + this.manual_map[this._strip_punctuation(feature)] = feature; + this.manual_callback[feature] = callback; // the default is going to be the first feature. + return this; + }; + + this.button = function(feature) { + if (arguments.length > 1) { + this.public_name[feature] = arguments[1]; + } + + if (arguments.length > 2) { + this.sorters[feature] = arguments[2]; + } + + this.feature_set[feature] = []; + return this; + }; + + this.callback = function(callback) { + this._callback = callback; + return this; + }; + + this.display = function() { + var callback = this._callback; + var manual_callback = this.manual_callback; + var manual_map = this.manual_map; + + var d, f, features, feat; + features = Object.keys(this.feature_set); + + var mapDtoF = function(f) { + return d[f]; }; + + var i; + + // build out this.feature_set with this.data + for (i = 0; i < this._data.length; i++) { + d = this._data[i]; + f = features.map(mapDtoF); + for (var j = 0; j < features.length; j++) { + feat = features[j]; + if (this.feature_set[feat].indexOf(f[j]) === -1) { + this.feature_set[feat].push(f[j]); + } + } + } + + for (feat in this.feature_set) { + if (this.sorters.hasOwnProperty(feat)) { + this.feature_set[feat].sort(this.sorters[feat]); + } + } + + $(this.target).empty(); + + $(this.target).append("
"); + + var dropdownLiAClick = function() { + var k = $(this).data('key'); + var feature = $(this).data('feature'); + var manual_feature; + $('.' + feature + '-btns button.btn span.title').html(k); + if (!manual_map.hasOwnProperty(feature)) { + callback(feature, k); + } else { + manual_feature = manual_map[feature]; + manual_callback[manual_feature](k); + } + + return false; + }; + + for (var feature in this.feature_set) { + features = this.feature_set[feature]; + $(this.target + ' div.segments').append( + '
' + // This never changes. + '' + + '' + '
'); + + for (i = 0; i < features.length; i++) { + if (features[i] !== 'all' && features[i] !== undefined) { // strange bug with undefined being added to manual buttons. + $(this.target + ' div.' + this._strip_punctuation(feature) + '-btns ul.dropdown-menu').append( + '
  • ' + features[i] + '
  • ' + ); + } + } + + $('.' + this._strip_punctuation(feature) + '-btns .dropdown-menu li a').on('click', dropdownLiAClick); + } + + return this; + }; + + return this; +}; -- cgit v1.2.3