| 1 |
/* |
| 2 |
* This file is part of unity-2d |
| 3 |
* |
| 4 |
* Copyright 2011 Canonical Ltd. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; version 3. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* The fromIso8601 function is based on: |
| 19 |
* Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601> |
| 20 |
* © 2011 Colin Snover <http://zetafleet.com> |
| 21 |
* Released under MIT license. |
| 22 |
* |
| 23 |
* Adapted by Ugo Riboni <[email protected]> |
| 24 |
*/ |
| 25 |
|
| 26 |
.pragma library |
| 27 |
|
| 28 |
/* Default color pallette */ |
| 29 |
var orange = "#DD4814" |
| 30 |
var canonicalAubergine = "#772953" |
| 31 |
var lightAubergine = "#77216F" |
| 32 |
var mediumAubergine = "#5E2750" |
| 33 |
var darkAubergine = "#2C001E" |
| 34 |
var darkAubergineDesaturated = "#2c1625" |
| 35 |
var warmGrey = "#AEA79F" |
| 36 |
var coolGrey = "#333333" |
| 37 |
var MS_PER_HOUR = 1000 * 3600 |
| 38 |
|
| 39 |
/* Convert strings like "one-two-three" to "OneTwoThree" */ |
| 40 |
function convertToCamelCase( name ) { |
| 41 |
var chunksArray = name.split('-') |
| 42 |
var camelName = '' |
| 43 |
for (var i=0; i<chunksArray.length; i++){ |
| 44 |
camelName = camelName + chunksArray[i].charAt(0).toUpperCase() + chunksArray[i].slice(1); |
| 45 |
} |
| 46 |
return camelName |
| 47 |
} |
| 48 |
|
| 49 |
function clamp(x, min, max) { |
| 50 |
return Math.max(Math.min(x, max), min) |
| 51 |
} |
| 52 |
|
| 53 |
function isLeftToRight() { |
| 54 |
return Qt.application.layoutDirection == Qt.LeftToRight |
| 55 |
} |
| 56 |
|
| 57 |
function isRightToLeft() { |
| 58 |
return Qt.application.layoutDirection == Qt.RightToLeft |
| 59 |
} |
| 60 |
|
| 61 |
function switchLeftRightKeys(key) { |
| 62 |
if (isRightToLeft()) { |
| 63 |
switch (key) { |
| 64 |
case Qt.Key_Right: |
| 65 |
return Qt.Key_Left |
| 66 |
case Qt.Key_Left: |
| 67 |
return Qt.Key_Right |
| 68 |
default: |
| 69 |
return key |
| 70 |
} |
| 71 |
} |
| 72 |
return key |
| 73 |
} |
| 74 |
|
| 75 |
function hashEmpty(hash) { |
| 76 |
for (var key in hash) return false; |
| 77 |
return true |
| 78 |
} |
| 79 |
|
| 80 |
function get_human_time_format(time_in_milliseconds) |
| 81 |
{ |
| 82 |
var date = new Date(time_in_milliseconds) |
| 83 |
|
| 84 |
if(date.getUTCHours() == 0) return "mm:ss" |
| 85 |
else return "hh:mm:ss" |
| 86 |
} |
| 87 |
|
| 88 |
function removeExt(uri) { |
| 89 |
return uri.substring(0, uri.lastIndexOf(".")) |
| 90 |
} |
| 91 |
|
| 92 |
function format_time(time_in_milliseconds, formatting) { |
| 93 |
/* We are using Qt.formatTime to format the time_in_milliseconds because |
| 94 |
this is more practical that doing the formatting manually (JavaScript |
| 95 |
does not provide any string formatting function natively). |
| 96 |
|
| 97 |
WARNING: this will break for media longer than 24 hours. |
| 98 |
*/ |
| 99 |
|
| 100 |
/* Hack JavaScript's Date to ignore timeone offset */ |
| 101 |
var zero = new Date(0) |
| 102 |
var offset = zero.getTimezoneOffset()*60*1000 |
| 103 |
var date = new Date(time_in_milliseconds+offset) |
| 104 |
|
| 105 |
return Qt.formatTime(date, formatting) |
| 106 |
} |
| 107 |
|
| 108 |
function fromIso8601(date) { |
| 109 |
var timestamp, struct, minutesOffset = 0; |
| 110 |
var numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ]; |
| 111 |
|
| 112 |
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string |
| 113 |
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native |
| 114 |
// implementations could be faster |
| 115 |
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm |
| 116 |
if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) { |
| 117 |
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC |
| 118 |
for (var i = 0, k; (k = numericKeys[i]); ++i) { |
| 119 |
struct[k] = +struct[k] || 0; |
| 120 |
} |
| 121 |
|
| 122 |
// allow undefined days and months |
| 123 |
struct[2] = (+struct[2] || 1) - 1; |
| 124 |
struct[3] = +struct[3] || 1; |
| 125 |
|
| 126 |
if (struct[8] !== 'Z' && struct[9] !== undefined) { |
| 127 |
minutesOffset = struct[10] * 60 + struct[11]; |
| 128 |
|
| 129 |
if (struct[9] === '+') { |
| 130 |
minutesOffset = 0 - minutesOffset; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]); |
| 135 |
return new Date(timestamp); |
| 136 |
} |
| 137 |
else return null; |
| 138 |
} |
| 139 |
|
| 140 |
function isSameDay(date1, date2) { |
| 141 |
return (date1.getFullYear() == date2.getFullYear() && |
| 142 |
date1.getMonth == date2.getMonth && |
| 143 |
date1.getDay() == date2.getDay()) |
| 144 |
} |
| 145 |
|
| 146 |
function pad(text, len, paddingChar) { |
| 147 |
var padding = "" |
| 148 |
for (var i = text.length; i < len; i++) padding += paddingChar |
| 149 |
return padding + text |
| 150 |
} |
| 151 |
|
| 152 |
/* DOCME */ |
| 153 |
function linearInterpolation(xa, xb, a, b, x) { |
| 154 |
return (clamp(x, xa, xb) - xa)/(xb - xa) * (b - a) + a |
| 155 |
} |
| 156 |
|
| 157 |
/* DOCME */ |
| 158 |
function segmentsLinearInterpolation(lx, ly, x) { |
| 159 |
/* FIXME: quick, 5 minutes implementation. Needs cleanup */ |
| 160 |
var cxa, cxb, cya, cyb |
| 161 |
for (var i = 0, len = lx.length; i < len; i++) { |
| 162 |
cxa = lx[i] |
| 163 |
cya = ly[i] |
| 164 |
if (cxa <= x) { |
| 165 |
if (i >= len-1) return cya |
| 166 |
cxb = lx[i+1] |
| 167 |
if (x < cxb) { |
| 168 |
cyb = ly[i+1] |
| 169 |
return linearInterpolation(cxa, cxb, cya, cyb, x) |
| 170 |
} |
| 171 |
} else { |
| 172 |
return cya |
| 173 |
} |
| 174 |
} |
| 175 |
} |