Browse Source

Merge pull request #102 from theurere/formatBase1000-filter

Add filter - formatBase1000
pull/104/head
Simon Eisenmann 12 years ago
parent
commit
d68cf89fc4
  1. 6
      static/js/filters/filters.js
  2. 86
      static/js/filters/formatbase1000.js
  3. 4
      static/partials/geolocation.html

6
static/js/filters/filters.js

@ -25,14 +25,16 @@ define([ @@ -25,14 +25,16 @@ define([
'filters/buddyimagesrc',
'filters/displayconference',
'filters/displayuserid',
'filters/displaynameforsession'], function(_, displayName, buddyImageSrc, displayConference, displayUserid, displayNameForSession) {
'filters/displaynameforsession',
'filters/formatbase1000'], function(_, displayName, buddyImageSrc, displayConference, displayUserid, displayNameForSession, formatBase1000) {
var filters = {
displayName: displayName,
buddyImageSrc: buddyImageSrc,
displayConference: displayConference,
displayUserid: displayUserid,
displayNameForSession: displayNameForSession
displayNameForSession: displayNameForSession,
formatBase1000: formatBase1000
};
var initialize = function(angModule) {

86
static/js/filters/formatbase1000.js

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
/*
* Spreed WebRTC.
* Copyright (C) 2013-2014 struktur AG
*
* This file is part of Spreed WebRTC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
define([], function() {
// formatBase1000
/* Purpose: format number/unit appropriately.
Appends k+base-unit, base-unit, c+base-unit, m+base-unit to value depending on input.
Decimal accuracy is taken care of automatically but override is avaible.
Using: Number | formatBase1000[:'base-unit'][:'desired-precision']
*/
return [function() {
var defaultPrecisionUnder1 = 2;
var defaultPrecisionFrom1AndUnder10 = 2;
var defaultPrecisionFrom10AndUnder100 = 1;
var defaultPrecisionFrom100AndOver = 0;
return function(num, unit, precision) {
num = Number(num);
precision = Number(precision);
var getNumber = function(precision) {
var dot = num.toString().search(/\./);
if (dot === -1) {
precision += num.toString().length;
} else {
precision += dot;
}
// toPrecision supports range 1 - 21
if (precision > 21) {
precision = 21;
}
return num.toPrecision(precision);
};
var getPrecision = function() {
if (!isNaN(precision)) {
return precision;
}
if (num >= 100) {
return defaultPrecisionFrom100AndOver;
} else if (num < 100 && num >= 10) {
return defaultPrecisionFrom10AndUnder100;
} else if (num < 10 && num >= 1){
return defaultPrecisionFrom1AndUnder10;
} else {
return defaultPrecisionUnder1;
}
};
if (!unit) {
return getNumber(getPrecision());
}
// kilo
if (num >= 1000) {
num /= 1000;
return getNumber(getPrecision()) + 'k' + unit;
// centi
} else if (num < 1 && num >= 0.01) {
num *= 100;
return getNumber(getPrecision()) + 'c' + unit;
// milli
} else if (num < 0.01) {
num *= 1000;
return getNumber(getPrecision()) + 'm' + unit;
// base
} else {
return getNumber(getPrecision()) + unit;
}
};
}];
});

4
static/partials/geolocation.html

@ -1,3 +1,3 @@ @@ -1,3 +1,3 @@
<div class="geolocation">
<div><i class="fa fa-location-arrow"></i> <a rel="external" href="https://maps.google.com/maps?q={{info.latitude}},{{info.longitude}}">{{info.latitude|number:6}}&ordm; {{info.longitude|number:6}}&ordm; &plusmn;{{info.accuracy|number:0}}m</a></div>
</div>
<div><i class="fa fa-location-arrow"></i> <a rel="external" href="https://maps.google.com/maps?q={{info.latitude}},{{info.longitude}}">{{info.latitude|number:6}}&ordm; {{info.longitude|number:6}}&ordm; &plusmn;{{info.accuracy|formatBase1000:'m'}}</a></div>
</div>

Loading…
Cancel
Save