From f757061cd7baf55b8607492b2665aa1dcdbe4f4e Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sun, 18 Nov 2018 18:33:56 +0100 Subject: [PATCH] Add the comparator in Devices --- src/css_composer/view/CssRulesView.js | 19 ++++++------------- src/device_manager/model/Device.js | 9 ++++++++- src/device_manager/model/Devices.js | 11 ++++++++++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/css_composer/view/CssRulesView.js b/src/css_composer/view/CssRulesView.js index 91381f6cd..9b6cd0684 100644 --- a/src/css_composer/view/CssRulesView.js +++ b/src/css_composer/view/CssRulesView.js @@ -3,9 +3,8 @@ const CssRuleView = require('./CssRuleView'); const CssGroupRuleView = require('./CssGroupRuleView'); const $ = Backbone.$; -// % is not a valid character for classes -const getBlockId = (pfx, widthMedia) => - `${pfx}${widthMedia ? `-${widthMedia.replace('%', 'pc')}` : ''}`; +const getBlockId = (pfx, order) => + `${pfx}${order ? `-${parseFloat(order)}` : ''}`; module.exports = Backbone.View.extend({ initialize(o) { @@ -90,6 +89,7 @@ module.exports = Backbone.View.extend({ // Try to find a specific container for the rule (if it // containes a media query), otherwise get the default one try { + console.log('try to find ID', blockId); contRules = container.querySelector(`#${blockId}`); } catch (e) {} @@ -124,16 +124,9 @@ module.exports = Backbone.View.extend({ this.em .get('DeviceManager') .getAll() - .map(model => model.get('widthMedia')) - .sort( - (left, right) => - ((right && right.replace('px', '')) || Number.MAX_VALUE) - - ((left && left.replace('px', '')) || Number.MAX_VALUE) - ) - .forEach(widthMedia => { - $(`
`).appendTo( - frag - ); + .pluck('priority') + .forEach(priority => { + $(`
`).appendTo(frag); }); this.collection.each(model => this.addToCollection(model, frag)); diff --git a/src/device_manager/model/Device.js b/src/device_manager/model/Device.js index 5d3198083..19ae096b7 100644 --- a/src/device_manager/model/Device.js +++ b/src/device_manager/model/Device.js @@ -14,12 +14,19 @@ module.exports = Backbone.Model.extend({ // The width which will be used in media queries, // If empty the width will be used - widthMedia: null + widthMedia: null, + + // Setup the order of media queries + priority: null }, initialize() { if (this.get('widthMedia') == null) { this.set('widthMedia', this.get('width')); } + + if (!this.get('priority')) { + this.set('priority', parseFloat(this.get('widthMedia')) || 0); + } } }); diff --git a/src/device_manager/model/Devices.js b/src/device_manager/model/Devices.js index a917b9338..3e1214674 100644 --- a/src/device_manager/model/Devices.js +++ b/src/device_manager/model/Devices.js @@ -2,5 +2,14 @@ import Backbone from 'backbone'; var Device = require('./Device'); module.exports = Backbone.Collection.extend({ - model: Device + model: Device, + + comparator: (left, right) => { + const max = Number.MAX_VALUE; + return (right.get('priority') || max) - (left.get('priority') || max); + }, + + getSorted() { + return this.sort(); + } });