| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItem';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = false;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'sorts element attributes (disabled by default)';
|
|---|
| 8 |
|
|---|
| 9 | exports.params = {
|
|---|
| 10 | order: [
|
|---|
| 11 | 'id',
|
|---|
| 12 | 'width', 'height',
|
|---|
| 13 | 'x', 'x1', 'x2',
|
|---|
| 14 | 'y', 'y1', 'y2',
|
|---|
| 15 | 'cx', 'cy', 'r',
|
|---|
| 16 | 'fill', 'stroke', 'marker',
|
|---|
| 17 | 'd', 'points'
|
|---|
| 18 | ]
|
|---|
| 19 | };
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Sort element attributes for epic readability.
|
|---|
| 23 | *
|
|---|
| 24 | * @param {Object} item current iteration item
|
|---|
| 25 | * @param {Object} params plugin params
|
|---|
| 26 | *
|
|---|
| 27 | * @author Nikolay Frantsev
|
|---|
| 28 | */
|
|---|
| 29 | exports.fn = function(item, params) {
|
|---|
| 30 |
|
|---|
| 31 | var attrs = [],
|
|---|
| 32 | sorted = {},
|
|---|
| 33 | orderlen = params.order.length + 1,
|
|---|
| 34 | xmlnsOrder = params.xmlnsOrder || 'front';
|
|---|
| 35 |
|
|---|
| 36 | if (item.elem) {
|
|---|
| 37 |
|
|---|
| 38 | item.eachAttr(function(attr) {
|
|---|
| 39 | attrs.push(attr);
|
|---|
| 40 | });
|
|---|
| 41 |
|
|---|
| 42 | attrs.sort(function(a, b) {
|
|---|
| 43 | if (a.prefix != b.prefix) {
|
|---|
| 44 | // xmlns attributes implicitly have the prefix xmlns
|
|---|
| 45 | if (xmlnsOrder == 'front') {
|
|---|
| 46 | if (a.prefix == 'xmlns')
|
|---|
| 47 | return -1;
|
|---|
| 48 | if (b.prefix == 'xmlns')
|
|---|
| 49 | return 1;
|
|---|
| 50 | }
|
|---|
| 51 | return a.prefix < b.prefix ? -1 : 1;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | var aindex = orderlen;
|
|---|
| 55 | var bindex = orderlen;
|
|---|
| 56 |
|
|---|
| 57 | for (var i = 0; i < params.order.length; i++) {
|
|---|
| 58 | if (a.name == params.order[i]) {
|
|---|
| 59 | aindex = i;
|
|---|
| 60 | } else if (a.name.indexOf(params.order[i] + '-') === 0) {
|
|---|
| 61 | aindex = i + .5;
|
|---|
| 62 | }
|
|---|
| 63 | if (b.name == params.order[i]) {
|
|---|
| 64 | bindex = i;
|
|---|
| 65 | } else if (b.name.indexOf(params.order[i] + '-') === 0) {
|
|---|
| 66 | bindex = i + .5;
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (aindex != bindex) {
|
|---|
| 71 | return aindex - bindex;
|
|---|
| 72 | }
|
|---|
| 73 | return a.name < b.name ? -1 : 1;
|
|---|
| 74 | });
|
|---|
| 75 |
|
|---|
| 76 | attrs.forEach(function (attr) {
|
|---|
| 77 | sorted[attr.name] = attr;
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | item.attrs = sorted;
|
|---|
| 81 |
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | };
|
|---|