This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
calcifer/web/semantic/dist/components/progress.js

795 lines
25 KiB
JavaScript
Raw Normal View History

2015-03-29 19:33:23 +02:00
/*!
2016-06-10 00:34:34 +02:00
* # Semantic UI 2.1.7 - Progress
2015-03-29 19:33:23 +02:00
* http://github.com/semantic-org/semantic-ui/
*
*
2016-06-10 00:34:34 +02:00
* Copyright 2015 Contributors
2015-03-29 19:33:23 +02:00
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
;(function ( $, window, document, undefined ) {
"use strict";
$.fn.progress = function(parameters) {
var
$allModules = $(this),
moduleSelector = $allModules.selector || '',
time = new Date().getTime(),
performance = [],
query = arguments[0],
methodInvoked = (typeof query == 'string'),
queryArguments = [].slice.call(arguments, 1),
returnedValue
;
$allModules
.each(function() {
var
settings = ( $.isPlainObject(parameters) )
? $.extend(true, {}, $.fn.progress.settings, parameters)
: $.extend({}, $.fn.progress.settings),
className = settings.className,
metadata = settings.metadata,
namespace = settings.namespace,
selector = settings.selector,
error = settings.error,
eventNamespace = '.' + namespace,
moduleNamespace = 'module-' + namespace,
$module = $(this),
$bar = $(this).find(selector.bar),
$progress = $(this).find(selector.progress),
$label = $(this).find(selector.label),
element = this,
instance = $module.data(moduleNamespace),
animating = false,
transitionEnd,
module
;
module = {
initialize: function() {
module.debug('Initializing progress bar', settings);
2016-06-10 00:34:34 +02:00
module.set.duration();
module.set.transitionEvent();
2015-03-29 19:33:23 +02:00
module.read.metadata();
2016-06-10 00:34:34 +02:00
module.read.settings();
2015-03-29 19:33:23 +02:00
module.instantiate();
},
instantiate: function() {
module.verbose('Storing instance of progress', module);
instance = module;
$module
.data(moduleNamespace, module)
;
},
destroy: function() {
module.verbose('Destroying previous progress for', $module);
clearInterval(instance.interval);
module.remove.state();
$module.removeData(moduleNamespace);
instance = undefined;
},
reset: function() {
module.set.percent(0);
2016-06-10 00:34:34 +02:00
module.set.value(0);
2015-03-29 19:33:23 +02:00
},
complete: function() {
if(module.percent === undefined || module.percent < 100) {
module.set.percent(100);
}
},
read: {
metadata: function() {
2016-06-10 00:34:34 +02:00
var
data = {
percent : $module.data(metadata.percent),
total : $module.data(metadata.total),
value : $module.data(metadata.value)
}
;
if(data.percent) {
module.debug('Current percent value set from metadata', data.percent);
module.set.percent(data.percent);
2015-03-29 19:33:23 +02:00
}
2016-06-10 00:34:34 +02:00
if(data.total) {
module.debug('Total value set from metadata', data.total);
module.set.total(data.total);
2015-03-29 19:33:23 +02:00
}
2016-06-10 00:34:34 +02:00
if(data.value) {
module.debug('Current value set from metadata', data.value);
module.set.value(data.value);
module.set.progress(data.value);
2015-03-29 19:33:23 +02:00
}
},
2016-06-10 00:34:34 +02:00
settings: function() {
if(settings.total !== false) {
module.debug('Current total set in settings', settings.total);
module.set.total(settings.total);
}
if(settings.value !== false) {
module.debug('Current value set in settings', settings.value);
module.set.value(settings.value);
module.set.progress(module.value);
}
if(settings.percent !== false) {
module.debug('Current percent set in settings', settings.percent);
module.set.percent(settings.percent);
}
2015-03-29 19:33:23 +02:00
}
},
increment: function(incrementValue) {
var
2016-06-10 00:34:34 +02:00
maxValue,
2015-03-29 19:33:23 +02:00
startValue,
newValue
;
2016-06-10 00:34:34 +02:00
if( module.has.total() ) {
startValue = module.get.value();
2015-03-29 19:33:23 +02:00
incrementValue = incrementValue || 1;
2016-06-10 00:34:34 +02:00
2015-03-29 19:33:23 +02:00
newValue = startValue + incrementValue;
2016-06-10 00:34:34 +02:00
maxValue = module.get.total();
module.debug('Incrementing value', startValue, newValue, maxValue);
if(newValue > maxValue ) {
module.debug('Value cannot increment above total', maxValue);
newValue = maxValue;
2015-03-29 19:33:23 +02:00
}
}
else {
2016-06-10 00:34:34 +02:00
startValue = module.get.percent();
2015-03-29 19:33:23 +02:00
incrementValue = incrementValue || module.get.randomValue();
2016-06-10 00:34:34 +02:00
2015-03-29 19:33:23 +02:00
newValue = startValue + incrementValue;
2016-06-10 00:34:34 +02:00
maxValue = 100;
module.debug('Incrementing percentage by', startValue, newValue);
if(newValue > maxValue ) {
2015-03-29 19:33:23 +02:00
module.debug('Value cannot increment above 100 percent');
2016-06-10 00:34:34 +02:00
newValue = maxValue;
2015-03-29 19:33:23 +02:00
}
}
2016-06-10 00:34:34 +02:00
module.set.progress(newValue);
2015-03-29 19:33:23 +02:00
},
decrement: function(decrementValue) {
var
2016-06-10 00:34:34 +02:00
total = module.get.total(),
2015-03-29 19:33:23 +02:00
startValue,
newValue
;
if(total) {
2016-06-10 00:34:34 +02:00
startValue = module.get.value();
2015-03-29 19:33:23 +02:00
decrementValue = decrementValue || 1;
newValue = startValue - decrementValue;
module.debug('Decrementing value by', decrementValue, startValue);
}
else {
2016-06-10 00:34:34 +02:00
startValue = module.get.percent();
2015-03-29 19:33:23 +02:00
decrementValue = decrementValue || module.get.randomValue();
newValue = startValue - decrementValue;
module.debug('Decrementing percentage by', decrementValue, startValue);
}
2016-06-10 00:34:34 +02:00
if(newValue < 0) {
2015-03-29 19:33:23 +02:00
module.debug('Value cannot decrement below 0');
newValue = 0;
}
module.set.progress(newValue);
},
2016-06-10 00:34:34 +02:00
has: {
total: function() {
return (module.get.total() !== false);
}
},
2015-03-29 19:33:23 +02:00
get: {
text: function(templateText) {
var
value = module.value || 0,
total = module.total || 0,
2016-06-10 00:34:34 +02:00
percent = (animating)
2015-03-29 19:33:23 +02:00
? module.get.displayPercent()
: module.percent || 0,
left = (module.total > 0)
? (total - value)
: (100 - percent)
;
templateText = templateText || '';
templateText = templateText
.replace('{value}', value)
.replace('{total}', total)
.replace('{left}', left)
.replace('{percent}', percent)
;
module.debug('Adding variables to progress bar text', templateText);
return templateText;
},
2016-06-10 00:34:34 +02:00
2015-03-29 19:33:23 +02:00
randomValue: function() {
module.debug('Generating random increment percentage');
return Math.floor((Math.random() * settings.random.max) + settings.random.min);
},
2016-06-10 00:34:34 +02:00
numericValue: function(value) {
return (typeof value === 'string')
? (value.replace(/[^\d.]/g, '') !== '')
? +(value.replace(/[^\d.]/g, ''))
: false
: value
;
},
2015-03-29 19:33:23 +02:00
transitionEnd: function() {
var
element = document.createElement('element'),
transitions = {
'transition' :'transitionend',
'OTransition' :'oTransitionEnd',
'MozTransition' :'transitionend',
'WebkitTransition' :'webkitTransitionEnd'
},
transition
;
for(transition in transitions){
if( element.style[transition] !== undefined ){
return transitions[transition];
}
}
},
// gets current displayed percentage (if animating values this is the intermediary value)
displayPercent: function() {
var
barWidth = $bar.width(),
totalWidth = $module.width(),
minDisplay = parseInt($bar.css('min-width'), 10),
displayPercent = (barWidth > minDisplay)
? (barWidth / totalWidth * 100)
: module.percent
;
2016-06-10 00:34:34 +02:00
return (settings.precision > 0)
? Math.round(displayPercent * (10 * settings.precision)) / (10 * settings.precision)
: Math.round(displayPercent)
;
2015-03-29 19:33:23 +02:00
},
percent: function() {
return module.percent || 0;
},
value: function() {
2016-06-10 00:34:34 +02:00
return module.value || 0;
2015-03-29 19:33:23 +02:00
},
total: function() {
return module.total || false;
}
},
is: {
success: function() {
return $module.hasClass(className.success);
},
warning: function() {
return $module.hasClass(className.warning);
},
error: function() {
return $module.hasClass(className.error);
},
active: function() {
return $module.hasClass(className.active);
},
visible: function() {
return $module.is(':visible');
}
},
remove: {
state: function() {
module.verbose('Removing stored state');
delete module.total;
delete module.percent;
delete module.value;
},
active: function() {
module.verbose('Removing active state');
$module.removeClass(className.active);
},
success: function() {
module.verbose('Removing success state');
$module.removeClass(className.success);
},
warning: function() {
module.verbose('Removing warning state');
$module.removeClass(className.warning);
},
error: function() {
module.verbose('Removing error state');
$module.removeClass(className.error);
}
},
set: {
barWidth: function(value) {
if(value > 100) {
module.error(error.tooHigh, value);
}
else if (value < 0) {
module.error(error.tooLow, value);
}
else {
$bar
.css('width', value + '%')
;
$module
.attr('data-percent', parseInt(value, 10))
;
}
},
duration: function(duration) {
duration = duration || settings.duration;
duration = (typeof duration == 'number')
? duration + 'ms'
: duration
;
module.verbose('Setting progress bar transition duration', duration);
$bar
.css({
'transition-duration': duration
})
;
},
percent: function(percent) {
percent = (typeof percent == 'string')
? +(percent.replace('%', ''))
: percent
;
2016-06-10 00:34:34 +02:00
// round display percentage
percent = (settings.precision > 0)
? Math.round(percent * (10 * settings.precision)) / (10 * settings.precision)
: Math.round(percent)
;
2015-03-29 19:33:23 +02:00
module.percent = percent;
2016-06-10 00:34:34 +02:00
if( !module.has.total() ) {
module.value = (settings.precision > 0)
? Math.round( (percent / 100) * module.total * (10 * settings.precision)) / (10 * settings.precision)
: Math.round( (percent / 100) * module.total * 10) / 10
2015-03-29 19:33:23 +02:00
;
2016-06-10 00:34:34 +02:00
if(settings.limitValues) {
module.value = (module.value > 100)
? 100
: (module.value < 0)
? 0
: module.value
;
}
2015-03-29 19:33:23 +02:00
}
module.set.barWidth(percent);
2016-06-10 00:34:34 +02:00
module.set.labelInterval();
2015-03-29 19:33:23 +02:00
module.set.labels();
settings.onChange.call(element, percent, module.value, module.total);
},
labelInterval: function() {
var
animationCallback = function() {
module.verbose('Bar finished animating, removing continuous label updates');
clearInterval(module.interval);
animating = false;
module.set.labels();
}
;
clearInterval(module.interval);
$bar.one(transitionEnd + eventNamespace, animationCallback);
module.timer = setTimeout(animationCallback, settings.duration + 100);
animating = true;
module.interval = setInterval(module.set.labels, settings.framerate);
},
labels: function() {
module.verbose('Setting both bar progress and outer label text');
module.set.barLabel();
module.set.state();
},
label: function(text) {
text = text || '';
if(text) {
text = module.get.text(text);
module.debug('Setting label to text', text);
$label.text(text);
}
},
state: function(percent) {
percent = (percent !== undefined)
? percent
: module.percent
;
if(percent === 100) {
if(settings.autoSuccess && !(module.is.warning() || module.is.error())) {
module.set.success();
module.debug('Automatically triggering success at 100%');
}
else {
module.verbose('Reached 100% removing active state');
module.remove.active();
}
}
else if(percent > 0) {
module.verbose('Adjusting active progress bar label', percent);
module.set.active();
}
else {
module.remove.active();
module.set.label(settings.text.active);
}
},
barLabel: function(text) {
if(text !== undefined) {
$progress.text( module.get.text(text) );
}
else if(settings.label == 'ratio' && module.total) {
module.debug('Adding ratio to bar label');
$progress.text( module.get.text(settings.text.ratio) );
}
else if(settings.label == 'percent') {
module.debug('Adding percentage to bar label');
$progress.text( module.get.text(settings.text.percent) );
}
},
active: function(text) {
text = text || settings.text.active;
module.debug('Setting active state');
if(settings.showActivity && !module.is.active() ) {
$module.addClass(className.active);
}
module.remove.warning();
module.remove.error();
module.remove.success();
if(text) {
module.set.label(text);
}
settings.onActive.call(element, module.value, module.total);
},
success : function(text) {
text = text || settings.text.success;
module.debug('Setting success state');
$module.addClass(className.success);
module.remove.active();
module.remove.warning();
module.remove.error();
module.complete();
if(text) {
module.set.label(text);
}
settings.onSuccess.call(element, module.total);
},
warning : function(text) {
text = text || settings.text.warning;
module.debug('Setting warning state');
$module.addClass(className.warning);
module.remove.active();
module.remove.success();
module.remove.error();
module.complete();
if(text) {
module.set.label(text);
}
settings.onWarning.call(element, module.value, module.total);
},
error : function(text) {
text = text || settings.text.error;
module.debug('Setting error state');
$module.addClass(className.error);
module.remove.active();
module.remove.success();
module.remove.warning();
module.complete();
if(text) {
module.set.label(text);
}
settings.onError.call(element, module.value, module.total);
},
2016-06-10 00:34:34 +02:00
transitionEvent: function() {
transitionEnd = module.get.transitionEnd();
},
2015-03-29 19:33:23 +02:00
total: function(totalValue) {
module.total = totalValue;
},
2016-06-10 00:34:34 +02:00
value: function(value) {
module.value = value;
},
2015-03-29 19:33:23 +02:00
progress: function(value) {
var
2016-06-10 00:34:34 +02:00
numericValue = module.get.numericValue(value),
2015-03-29 19:33:23 +02:00
percentComplete
;
if(numericValue === false) {
module.error(error.nonNumeric, value);
}
2016-06-10 00:34:34 +02:00
if( module.has.total() ) {
module.set.value(numericValue);
2015-03-29 19:33:23 +02:00
percentComplete = (numericValue / module.total) * 100;
module.debug('Calculating percent complete from total', percentComplete);
module.set.percent( percentComplete );
}
else {
percentComplete = numericValue;
module.debug('Setting value to exact percentage value', percentComplete);
module.set.percent( percentComplete );
}
}
},
setting: function(name, value) {
module.debug('Changing setting', name, value);
if( $.isPlainObject(name) ) {
$.extend(true, settings, name);
}
else if(value !== undefined) {
settings[name] = value;
}
else {
return settings[name];
}
},
internal: function(name, value) {
if( $.isPlainObject(name) ) {
$.extend(true, module, name);
}
else if(value !== undefined) {
module[name] = value;
}
else {
return module[name];
}
},
debug: function() {
if(settings.debug) {
if(settings.performance) {
module.performance.log(arguments);
}
else {
module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
module.debug.apply(console, arguments);
}
}
},
verbose: function() {
if(settings.verbose && settings.debug) {
if(settings.performance) {
module.performance.log(arguments);
}
else {
module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
module.verbose.apply(console, arguments);
}
}
},
error: function() {
module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
module.error.apply(console, arguments);
},
performance: {
log: function(message) {
var
currentTime,
executionTime,
previousTime
;
if(settings.performance) {
currentTime = new Date().getTime();
previousTime = time || currentTime;
executionTime = currentTime - previousTime;
time = currentTime;
performance.push({
'Name' : message[0],
'Arguments' : [].slice.call(message, 1) || '',
'Element' : element,
'Execution Time' : executionTime
});
}
clearTimeout(module.performance.timer);
2016-06-10 00:34:34 +02:00
module.performance.timer = setTimeout(module.performance.display, 500);
2015-03-29 19:33:23 +02:00
},
display: function() {
var
title = settings.name + ':',
totalTime = 0
;
time = false;
clearTimeout(module.performance.timer);
$.each(performance, function(index, data) {
totalTime += data['Execution Time'];
});
title += ' ' + totalTime + 'ms';
if(moduleSelector) {
title += ' \'' + moduleSelector + '\'';
}
if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
console.groupCollapsed(title);
if(console.table) {
console.table(performance);
}
else {
$.each(performance, function(index, data) {
console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
});
}
console.groupEnd();
}
performance = [];
}
},
invoke: function(query, passedArguments, context) {
var
object = instance,
maxDepth,
found,
response
;
passedArguments = passedArguments || queryArguments;
context = element || context;
if(typeof query == 'string' && object !== undefined) {
query = query.split(/[\. ]/);
maxDepth = query.length - 1;
$.each(query, function(depth, value) {
var camelCaseValue = (depth != maxDepth)
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
: query
;
if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
object = object[camelCaseValue];
}
else if( object[camelCaseValue] !== undefined ) {
found = object[camelCaseValue];
return false;
}
else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
object = object[value];
}
else if( object[value] !== undefined ) {
found = object[value];
return false;
}
else {
module.error(error.method, query);
return false;
}
});
}
if ( $.isFunction( found ) ) {
response = found.apply(context, passedArguments);
}
else if(found !== undefined) {
response = found;
}
if($.isArray(returnedValue)) {
returnedValue.push(response);
}
else if(returnedValue !== undefined) {
returnedValue = [returnedValue, response];
}
else if(response !== undefined) {
returnedValue = response;
}
return found;
}
};
if(methodInvoked) {
if(instance === undefined) {
module.initialize();
}
module.invoke(query);
}
else {
if(instance !== undefined) {
instance.invoke('destroy');
}
module.initialize();
}
})
;
return (returnedValue !== undefined)
? returnedValue
: this
;
};
$.fn.progress.settings = {
name : 'Progress',
namespace : 'progress',
debug : false,
2016-06-10 00:34:34 +02:00
verbose : false,
2015-03-29 19:33:23 +02:00
performance : true,
random : {
min : 2,
max : 5
},
duration : 300,
autoSuccess : true,
showActivity : true,
limitValues : true,
label : 'percent',
2016-06-10 00:34:34 +02:00
precision : 0,
2015-03-29 19:33:23 +02:00
framerate : (1000 / 30), /// 30 fps
percent : false,
total : false,
value : false,
onChange : function(percent, value, total){},
onSuccess : function(total){},
onActive : function(value, total){},
onError : function(value, total){},
onWarning : function(value, total){},
error : {
method : 'The method you called is not defined.',
nonNumeric : 'Progress value is non numeric',
tooHigh : 'Value specified is above 100%',
tooLow : 'Value specified is below 0%'
},
regExp: {
variable: /\{\$*[A-z0-9]+\}/g
},
metadata: {
percent : 'percent',
total : 'total',
value : 'value'
},
selector : {
bar : '> .bar',
label : '> .label',
progress : '.bar > .progress'
},
text : {
active : false,
error : false,
success : false,
warning : false,
percent : '{percent}%',
ratio : '{value} of {total}'
},
className : {
active : 'active',
error : 'error',
success : 'success',
warning : 'warning'
}
};
2016-06-10 00:34:34 +02:00
})( jQuery, window, document );