@@ 11848-12768 (lines=921) @@ | ||
11845 | * |
|
11846 | */ |
|
11847 | ||
11848 | ;(function ($, window, document, undefined) { |
|
11849 | ||
11850 | "use strict"; |
|
11851 | ||
11852 | window = (typeof window != 'undefined' && window.Math == Math) |
|
11853 | ? window |
|
11854 | : (typeof self != 'undefined' && self.Math == Math) |
|
11855 | ? self |
|
11856 | : Function('return this')() |
|
11857 | ; |
|
11858 | ||
11859 | var |
|
11860 | global = (typeof window != 'undefined' && window.Math == Math) |
|
11861 | ? window |
|
11862 | : (typeof self != 'undefined' && self.Math == Math) |
|
11863 | ? self |
|
11864 | : Function('return this')() |
|
11865 | ; |
|
11866 | ||
11867 | $.fn.progress = function(parameters) { |
|
11868 | var |
|
11869 | $allModules = $(this), |
|
11870 | ||
11871 | moduleSelector = $allModules.selector || '', |
|
11872 | ||
11873 | time = new Date().getTime(), |
|
11874 | performance = [], |
|
11875 | ||
11876 | query = arguments[0], |
|
11877 | methodInvoked = (typeof query == 'string'), |
|
11878 | queryArguments = [].slice.call(arguments, 1), |
|
11879 | ||
11880 | returnedValue |
|
11881 | ; |
|
11882 | ||
11883 | $allModules |
|
11884 | .each(function() { |
|
11885 | var |
|
11886 | settings = ( $.isPlainObject(parameters) ) |
|
11887 | ? $.extend(true, {}, $.fn.progress.settings, parameters) |
|
11888 | : $.extend({}, $.fn.progress.settings), |
|
11889 | ||
11890 | className = settings.className, |
|
11891 | metadata = settings.metadata, |
|
11892 | namespace = settings.namespace, |
|
11893 | selector = settings.selector, |
|
11894 | error = settings.error, |
|
11895 | ||
11896 | eventNamespace = '.' + namespace, |
|
11897 | moduleNamespace = 'module-' + namespace, |
|
11898 | ||
11899 | $module = $(this), |
|
11900 | $bar = $(this).find(selector.bar), |
|
11901 | $progress = $(this).find(selector.progress), |
|
11902 | $label = $(this).find(selector.label), |
|
11903 | ||
11904 | element = this, |
|
11905 | instance = $module.data(moduleNamespace), |
|
11906 | ||
11907 | animating = false, |
|
11908 | transitionEnd, |
|
11909 | module |
|
11910 | ; |
|
11911 | ||
11912 | module = { |
|
11913 | ||
11914 | initialize: function() { |
|
11915 | module.debug('Initializing progress bar', settings); |
|
11916 | ||
11917 | module.set.duration(); |
|
11918 | module.set.transitionEvent(); |
|
11919 | ||
11920 | module.read.metadata(); |
|
11921 | module.read.settings(); |
|
11922 | ||
11923 | module.instantiate(); |
|
11924 | }, |
|
11925 | ||
11926 | instantiate: function() { |
|
11927 | module.verbose('Storing instance of progress', module); |
|
11928 | instance = module; |
|
11929 | $module |
|
11930 | .data(moduleNamespace, module) |
|
11931 | ; |
|
11932 | }, |
|
11933 | destroy: function() { |
|
11934 | module.verbose('Destroying previous progress for', $module); |
|
11935 | clearInterval(instance.interval); |
|
11936 | module.remove.state(); |
|
11937 | $module.removeData(moduleNamespace); |
|
11938 | instance = undefined; |
|
11939 | }, |
|
11940 | ||
11941 | reset: function() { |
|
11942 | module.remove.nextValue(); |
|
11943 | module.update.progress(0); |
|
11944 | }, |
|
11945 | ||
11946 | complete: function() { |
|
11947 | if(module.percent === undefined || module.percent < 100) { |
|
11948 | module.remove.progressPoll(); |
|
11949 | module.set.percent(100); |
|
11950 | } |
|
11951 | }, |
|
11952 | ||
11953 | read: { |
|
11954 | metadata: function() { |
|
11955 | var |
|
11956 | data = { |
|
11957 | percent : $module.data(metadata.percent), |
|
11958 | total : $module.data(metadata.total), |
|
11959 | value : $module.data(metadata.value) |
|
11960 | } |
|
11961 | ; |
|
11962 | if(data.percent) { |
|
11963 | module.debug('Current percent value set from metadata', data.percent); |
|
11964 | module.set.percent(data.percent); |
|
11965 | } |
|
11966 | if(data.total) { |
|
11967 | module.debug('Total value set from metadata', data.total); |
|
11968 | module.set.total(data.total); |
|
11969 | } |
|
11970 | if(data.value) { |
|
11971 | module.debug('Current value set from metadata', data.value); |
|
11972 | module.set.value(data.value); |
|
11973 | module.set.progress(data.value); |
|
11974 | } |
|
11975 | }, |
|
11976 | settings: function() { |
|
11977 | if(settings.total !== false) { |
|
11978 | module.debug('Current total set in settings', settings.total); |
|
11979 | module.set.total(settings.total); |
|
11980 | } |
|
11981 | if(settings.value !== false) { |
|
11982 | module.debug('Current value set in settings', settings.value); |
|
11983 | module.set.value(settings.value); |
|
11984 | module.set.progress(module.value); |
|
11985 | } |
|
11986 | if(settings.percent !== false) { |
|
11987 | module.debug('Current percent set in settings', settings.percent); |
|
11988 | module.set.percent(settings.percent); |
|
11989 | } |
|
11990 | } |
|
11991 | }, |
|
11992 | ||
11993 | bind: { |
|
11994 | transitionEnd: function(callback) { |
|
11995 | var |
|
11996 | transitionEnd = module.get.transitionEnd() |
|
11997 | ; |
|
11998 | $bar |
|
11999 | .one(transitionEnd + eventNamespace, function(event) { |
|
12000 | clearTimeout(module.failSafeTimer); |
|
12001 | callback.call(this, event); |
|
12002 | }) |
|
12003 | ; |
|
12004 | module.failSafeTimer = setTimeout(function() { |
|
12005 | $bar.triggerHandler(transitionEnd); |
|
12006 | }, settings.duration + settings.failSafeDelay); |
|
12007 | module.verbose('Adding fail safe timer', module.timer); |
|
12008 | } |
|
12009 | }, |
|
12010 | ||
12011 | increment: function(incrementValue) { |
|
12012 | var |
|
12013 | maxValue, |
|
12014 | startValue, |
|
12015 | newValue |
|
12016 | ; |
|
12017 | if( module.has.total() ) { |
|
12018 | startValue = module.get.value(); |
|
12019 | incrementValue = incrementValue || 1; |
|
12020 | newValue = startValue + incrementValue; |
|
12021 | } |
|
12022 | else { |
|
12023 | startValue = module.get.percent(); |
|
12024 | incrementValue = incrementValue || module.get.randomValue(); |
|
12025 | ||
12026 | newValue = startValue + incrementValue; |
|
12027 | maxValue = 100; |
|
12028 | module.debug('Incrementing percentage by', startValue, newValue); |
|
12029 | } |
|
12030 | newValue = module.get.normalizedValue(newValue); |
|
12031 | module.set.progress(newValue); |
|
12032 | }, |
|
12033 | decrement: function(decrementValue) { |
|
12034 | var |
|
12035 | total = module.get.total(), |
|
12036 | startValue, |
|
12037 | newValue |
|
12038 | ; |
|
12039 | if(total) { |
|
12040 | startValue = module.get.value(); |
|
12041 | decrementValue = decrementValue || 1; |
|
12042 | newValue = startValue - decrementValue; |
|
12043 | module.debug('Decrementing value by', decrementValue, startValue); |
|
12044 | } |
|
12045 | else { |
|
12046 | startValue = module.get.percent(); |
|
12047 | decrementValue = decrementValue || module.get.randomValue(); |
|
12048 | newValue = startValue - decrementValue; |
|
12049 | module.debug('Decrementing percentage by', decrementValue, startValue); |
|
12050 | } |
|
12051 | newValue = module.get.normalizedValue(newValue); |
|
12052 | module.set.progress(newValue); |
|
12053 | }, |
|
12054 | ||
12055 | has: { |
|
12056 | progressPoll: function() { |
|
12057 | return module.progressPoll; |
|
12058 | }, |
|
12059 | total: function() { |
|
12060 | return (module.get.total() !== false); |
|
12061 | } |
|
12062 | }, |
|
12063 | ||
12064 | get: { |
|
12065 | text: function(templateText) { |
|
12066 | var |
|
12067 | value = module.value || 0, |
|
12068 | total = module.total || 0, |
|
12069 | percent = (animating) |
|
12070 | ? module.get.displayPercent() |
|
12071 | : module.percent || 0, |
|
12072 | left = (module.total > 0) |
|
12073 | ? (total - value) |
|
12074 | : (100 - percent) |
|
12075 | ; |
|
12076 | templateText = templateText || ''; |
|
12077 | templateText = templateText |
|
12078 | .replace('{value}', value) |
|
12079 | .replace('{total}', total) |
|
12080 | .replace('{left}', left) |
|
12081 | .replace('{percent}', percent) |
|
12082 | ; |
|
12083 | module.verbose('Adding variables to progress bar text', templateText); |
|
12084 | return templateText; |
|
12085 | }, |
|
12086 | ||
12087 | normalizedValue: function(value) { |
|
12088 | if(value < 0) { |
|
12089 | module.debug('Value cannot decrement below 0'); |
|
12090 | return 0; |
|
12091 | } |
|
12092 | if(module.has.total()) { |
|
12093 | if(value > module.total) { |
|
12094 | module.debug('Value cannot increment above total', module.total); |
|
12095 | return module.total; |
|
12096 | } |
|
12097 | } |
|
12098 | else if(value > 100 ) { |
|
12099 | module.debug('Value cannot increment above 100 percent'); |
|
12100 | return 100; |
|
12101 | } |
|
12102 | return value; |
|
12103 | }, |
|
12104 | ||
12105 | updateInterval: function() { |
|
12106 | if(settings.updateInterval == 'auto') { |
|
12107 | return settings.duration; |
|
12108 | } |
|
12109 | return settings.updateInterval; |
|
12110 | }, |
|
12111 | ||
12112 | randomValue: function() { |
|
12113 | module.debug('Generating random increment percentage'); |
|
12114 | return Math.floor((Math.random() * settings.random.max) + settings.random.min); |
|
12115 | }, |
|
12116 | ||
12117 | numericValue: function(value) { |
|
12118 | return (typeof value === 'string') |
|
12119 | ? (value.replace(/[^\d.]/g, '') !== '') |
|
12120 | ? +(value.replace(/[^\d.]/g, '')) |
|
12121 | : false |
|
12122 | : value |
|
12123 | ; |
|
12124 | }, |
|
12125 | ||
12126 | transitionEnd: function() { |
|
12127 | var |
|
12128 | element = document.createElement('element'), |
|
12129 | transitions = { |
|
12130 | 'transition' :'transitionend', |
|
12131 | 'OTransition' :'oTransitionEnd', |
|
12132 | 'MozTransition' :'transitionend', |
|
12133 | 'WebkitTransition' :'webkitTransitionEnd' |
|
12134 | }, |
|
12135 | transition |
|
12136 | ; |
|
12137 | for(transition in transitions){ |
|
12138 | if( element.style[transition] !== undefined ){ |
|
12139 | return transitions[transition]; |
|
12140 | } |
|
12141 | } |
|
12142 | }, |
|
12143 | ||
12144 | // gets current displayed percentage (if animating values this is the intermediary value) |
|
12145 | displayPercent: function() { |
|
12146 | var |
|
12147 | barWidth = $bar.width(), |
|
12148 | totalWidth = $module.width(), |
|
12149 | minDisplay = parseInt($bar.css('min-width'), 10), |
|
12150 | displayPercent = (barWidth > minDisplay) |
|
12151 | ? (barWidth / totalWidth * 100) |
|
12152 | : module.percent |
|
12153 | ; |
|
12154 | return (settings.precision > 0) |
|
12155 | ? Math.round(displayPercent * (10 * settings.precision)) / (10 * settings.precision) |
|
12156 | : Math.round(displayPercent) |
|
12157 | ; |
|
12158 | }, |
|
12159 | ||
12160 | percent: function() { |
|
12161 | return module.percent || 0; |
|
12162 | }, |
|
12163 | value: function() { |
|
12164 | return module.nextValue || module.value || 0; |
|
12165 | }, |
|
12166 | total: function() { |
|
12167 | return module.total || false; |
|
12168 | } |
|
12169 | }, |
|
12170 | ||
12171 | create: { |
|
12172 | progressPoll: function() { |
|
12173 | module.progressPoll = setTimeout(function() { |
|
12174 | module.update.toNextValue(); |
|
12175 | module.remove.progressPoll(); |
|
12176 | }, module.get.updateInterval()); |
|
12177 | }, |
|
12178 | }, |
|
12179 | ||
12180 | is: { |
|
12181 | complete: function() { |
|
12182 | return module.is.success() || module.is.warning() || module.is.error(); |
|
12183 | }, |
|
12184 | success: function() { |
|
12185 | return $module.hasClass(className.success); |
|
12186 | }, |
|
12187 | warning: function() { |
|
12188 | return $module.hasClass(className.warning); |
|
12189 | }, |
|
12190 | error: function() { |
|
12191 | return $module.hasClass(className.error); |
|
12192 | }, |
|
12193 | active: function() { |
|
12194 | return $module.hasClass(className.active); |
|
12195 | }, |
|
12196 | visible: function() { |
|
12197 | return $module.is(':visible'); |
|
12198 | } |
|
12199 | }, |
|
12200 | ||
12201 | remove: { |
|
12202 | progressPoll: function() { |
|
12203 | module.verbose('Removing progress poll timer'); |
|
12204 | if(module.progressPoll) { |
|
12205 | clearTimeout(module.progressPoll); |
|
12206 | delete module.progressPoll; |
|
12207 | } |
|
12208 | }, |
|
12209 | nextValue: function() { |
|
12210 | module.verbose('Removing progress value stored for next update'); |
|
12211 | delete module.nextValue; |
|
12212 | }, |
|
12213 | state: function() { |
|
12214 | module.verbose('Removing stored state'); |
|
12215 | delete module.total; |
|
12216 | delete module.percent; |
|
12217 | delete module.value; |
|
12218 | }, |
|
12219 | active: function() { |
|
12220 | module.verbose('Removing active state'); |
|
12221 | $module.removeClass(className.active); |
|
12222 | }, |
|
12223 | success: function() { |
|
12224 | module.verbose('Removing success state'); |
|
12225 | $module.removeClass(className.success); |
|
12226 | }, |
|
12227 | warning: function() { |
|
12228 | module.verbose('Removing warning state'); |
|
12229 | $module.removeClass(className.warning); |
|
12230 | }, |
|
12231 | error: function() { |
|
12232 | module.verbose('Removing error state'); |
|
12233 | $module.removeClass(className.error); |
|
12234 | } |
|
12235 | }, |
|
12236 | ||
12237 | set: { |
|
12238 | barWidth: function(value) { |
|
12239 | if(value > 100) { |
|
12240 | module.error(error.tooHigh, value); |
|
12241 | } |
|
12242 | else if (value < 0) { |
|
12243 | module.error(error.tooLow, value); |
|
12244 | } |
|
12245 | else { |
|
12246 | $bar |
|
12247 | .css('width', value + '%') |
|
12248 | ; |
|
12249 | $module |
|
12250 | .attr('data-percent', parseInt(value, 10)) |
|
12251 | ; |
|
12252 | } |
|
12253 | }, |
|
12254 | duration: function(duration) { |
|
12255 | duration = duration || settings.duration; |
|
12256 | duration = (typeof duration == 'number') |
|
12257 | ? duration + 'ms' |
|
12258 | : duration |
|
12259 | ; |
|
12260 | module.verbose('Setting progress bar transition duration', duration); |
|
12261 | $bar |
|
12262 | .css({ |
|
12263 | 'transition-duration': duration |
|
12264 | }) |
|
12265 | ; |
|
12266 | }, |
|
12267 | percent: function(percent) { |
|
12268 | percent = (typeof percent == 'string') |
|
12269 | ? +(percent.replace('%', '')) |
|
12270 | : percent |
|
12271 | ; |
|
12272 | // round display percentage |
|
12273 | percent = (settings.precision > 0) |
|
12274 | ? Math.round(percent * (10 * settings.precision)) / (10 * settings.precision) |
|
12275 | : Math.round(percent) |
|
12276 | ; |
|
12277 | module.percent = percent; |
|
12278 | if( !module.has.total() ) { |
|
12279 | module.value = (settings.precision > 0) |
|
12280 | ? Math.round( (percent / 100) * module.total * (10 * settings.precision)) / (10 * settings.precision) |
|
12281 | : Math.round( (percent / 100) * module.total * 10) / 10 |
|
12282 | ; |
|
12283 | if(settings.limitValues) { |
|
12284 | module.value = (module.value > 100) |
|
12285 | ? 100 |
|
12286 | : (module.value < 0) |
|
12287 | ? 0 |
|
12288 | : module.value |
|
12289 | ; |
|
12290 | } |
|
12291 | } |
|
12292 | module.set.barWidth(percent); |
|
12293 | module.set.labelInterval(); |
|
12294 | module.set.labels(); |
|
12295 | settings.onChange.call(element, percent, module.value, module.total); |
|
12296 | }, |
|
12297 | labelInterval: function() { |
|
12298 | var |
|
12299 | animationCallback = function() { |
|
12300 | module.verbose('Bar finished animating, removing continuous label updates'); |
|
12301 | clearInterval(module.interval); |
|
12302 | animating = false; |
|
12303 | module.set.labels(); |
|
12304 | } |
|
12305 | ; |
|
12306 | clearInterval(module.interval); |
|
12307 | module.bind.transitionEnd(animationCallback); |
|
12308 | animating = true; |
|
12309 | module.interval = setInterval(function() { |
|
12310 | var |
|
12311 | isInDOM = $.contains(document.documentElement, element) |
|
12312 | ; |
|
12313 | if(!isInDOM) { |
|
12314 | clearInterval(module.interval); |
|
12315 | animating = false; |
|
12316 | } |
|
12317 | module.set.labels(); |
|
12318 | }, settings.framerate); |
|
12319 | }, |
|
12320 | labels: function() { |
|
12321 | module.verbose('Setting both bar progress and outer label text'); |
|
12322 | module.set.barLabel(); |
|
12323 | module.set.state(); |
|
12324 | }, |
|
12325 | label: function(text) { |
|
12326 | text = text || ''; |
|
12327 | if(text) { |
|
12328 | text = module.get.text(text); |
|
12329 | module.verbose('Setting label to text', text); |
|
12330 | $label.text(text); |
|
12331 | } |
|
12332 | }, |
|
12333 | state: function(percent) { |
|
12334 | percent = (percent !== undefined) |
|
12335 | ? percent |
|
12336 | : module.percent |
|
12337 | ; |
|
12338 | if(percent === 100) { |
|
12339 | if(settings.autoSuccess && !(module.is.warning() || module.is.error() || module.is.success())) { |
|
12340 | module.set.success(); |
|
12341 | module.debug('Automatically triggering success at 100%'); |
|
12342 | } |
|
12343 | else { |
|
12344 | module.verbose('Reached 100% removing active state'); |
|
12345 | module.remove.active(); |
|
12346 | module.remove.progressPoll(); |
|
12347 | } |
|
12348 | } |
|
12349 | else if(percent > 0) { |
|
12350 | module.verbose('Adjusting active progress bar label', percent); |
|
12351 | module.set.active(); |
|
12352 | } |
|
12353 | else { |
|
12354 | module.remove.active(); |
|
12355 | module.set.label(settings.text.active); |
|
12356 | } |
|
12357 | }, |
|
12358 | barLabel: function(text) { |
|
12359 | if(text !== undefined) { |
|
12360 | $progress.text( module.get.text(text) ); |
|
12361 | } |
|
12362 | else if(settings.label == 'ratio' && module.total) { |
|
12363 | module.verbose('Adding ratio to bar label'); |
|
12364 | $progress.text( module.get.text(settings.text.ratio) ); |
|
12365 | } |
|
12366 | else if(settings.label == 'percent') { |
|
12367 | module.verbose('Adding percentage to bar label'); |
|
12368 | $progress.text( module.get.text(settings.text.percent) ); |
|
12369 | } |
|
12370 | }, |
|
12371 | active: function(text) { |
|
12372 | text = text || settings.text.active; |
|
12373 | module.debug('Setting active state'); |
|
12374 | if(settings.showActivity && !module.is.active() ) { |
|
12375 | $module.addClass(className.active); |
|
12376 | } |
|
12377 | module.remove.warning(); |
|
12378 | module.remove.error(); |
|
12379 | module.remove.success(); |
|
12380 | text = settings.onLabelUpdate('active', text, module.value, module.total); |
|
12381 | if(text) { |
|
12382 | module.set.label(text); |
|
12383 | } |
|
12384 | module.bind.transitionEnd(function() { |
|
12385 | settings.onActive.call(element, module.value, module.total); |
|
12386 | }); |
|
12387 | }, |
|
12388 | success : function(text) { |
|
12389 | text = text || settings.text.success || settings.text.active; |
|
12390 | module.debug('Setting success state'); |
|
12391 | $module.addClass(className.success); |
|
12392 | module.remove.active(); |
|
12393 | module.remove.warning(); |
|
12394 | module.remove.error(); |
|
12395 | module.complete(); |
|
12396 | if(settings.text.success) { |
|
12397 | text = settings.onLabelUpdate('success', text, module.value, module.total); |
|
12398 | module.set.label(text); |
|
12399 | } |
|
12400 | else { |
|
12401 | text = settings.onLabelUpdate('active', text, module.value, module.total); |
|
12402 | module.set.label(text); |
|
12403 | } |
|
12404 | module.bind.transitionEnd(function() { |
|
12405 | settings.onSuccess.call(element, module.total); |
|
12406 | }); |
|
12407 | }, |
|
12408 | warning : function(text) { |
|
12409 | text = text || settings.text.warning; |
|
12410 | module.debug('Setting warning state'); |
|
12411 | $module.addClass(className.warning); |
|
12412 | module.remove.active(); |
|
12413 | module.remove.success(); |
|
12414 | module.remove.error(); |
|
12415 | module.complete(); |
|
12416 | text = settings.onLabelUpdate('warning', text, module.value, module.total); |
|
12417 | if(text) { |
|
12418 | module.set.label(text); |
|
12419 | } |
|
12420 | module.bind.transitionEnd(function() { |
|
12421 | settings.onWarning.call(element, module.value, module.total); |
|
12422 | }); |
|
12423 | }, |
|
12424 | error : function(text) { |
|
12425 | text = text || settings.text.error; |
|
12426 | module.debug('Setting error state'); |
|
12427 | $module.addClass(className.error); |
|
12428 | module.remove.active(); |
|
12429 | module.remove.success(); |
|
12430 | module.remove.warning(); |
|
12431 | module.complete(); |
|
12432 | text = settings.onLabelUpdate('error', text, module.value, module.total); |
|
12433 | if(text) { |
|
12434 | module.set.label(text); |
|
12435 | } |
|
12436 | module.bind.transitionEnd(function() { |
|
12437 | settings.onError.call(element, module.value, module.total); |
|
12438 | }); |
|
12439 | }, |
|
12440 | transitionEvent: function() { |
|
12441 | transitionEnd = module.get.transitionEnd(); |
|
12442 | }, |
|
12443 | total: function(totalValue) { |
|
12444 | module.total = totalValue; |
|
12445 | }, |
|
12446 | value: function(value) { |
|
12447 | module.value = value; |
|
12448 | }, |
|
12449 | progress: function(value) { |
|
12450 | if(!module.has.progressPoll()) { |
|
12451 | module.debug('First update in progress update interval, immediately updating', value); |
|
12452 | module.update.progress(value); |
|
12453 | module.create.progressPoll(); |
|
12454 | } |
|
12455 | else { |
|
12456 | module.debug('Updated within interval, setting next update to use new value', value); |
|
12457 | module.set.nextValue(value); |
|
12458 | } |
|
12459 | }, |
|
12460 | nextValue: function(value) { |
|
12461 | module.nextValue = value; |
|
12462 | } |
|
12463 | }, |
|
12464 | ||
12465 | update: { |
|
12466 | toNextValue: function() { |
|
12467 | var |
|
12468 | nextValue = module.nextValue |
|
12469 | ; |
|
12470 | if(nextValue) { |
|
12471 | module.debug('Update interval complete using last updated value', nextValue); |
|
12472 | module.update.progress(nextValue); |
|
12473 | module.remove.nextValue(); |
|
12474 | } |
|
12475 | }, |
|
12476 | progress: function(value) { |
|
12477 | var |
|
12478 | percentComplete |
|
12479 | ; |
|
12480 | value = module.get.numericValue(value); |
|
12481 | if(value === false) { |
|
12482 | module.error(error.nonNumeric, value); |
|
12483 | } |
|
12484 | value = module.get.normalizedValue(value); |
|
12485 | if( module.has.total() ) { |
|
12486 | module.set.value(value); |
|
12487 | percentComplete = (value / module.total) * 100; |
|
12488 | module.debug('Calculating percent complete from total', percentComplete); |
|
12489 | module.set.percent( percentComplete ); |
|
12490 | } |
|
12491 | else { |
|
12492 | percentComplete = value; |
|
12493 | module.debug('Setting value to exact percentage value', percentComplete); |
|
12494 | module.set.percent( percentComplete ); |
|
12495 | } |
|
12496 | } |
|
12497 | }, |
|
12498 | ||
12499 | setting: function(name, value) { |
|
12500 | module.debug('Changing setting', name, value); |
|
12501 | if( $.isPlainObject(name) ) { |
|
12502 | $.extend(true, settings, name); |
|
12503 | } |
|
12504 | else if(value !== undefined) { |
|
12505 | if($.isPlainObject(settings[name])) { |
|
12506 | $.extend(true, settings[name], value); |
|
12507 | } |
|
12508 | else { |
|
12509 | settings[name] = value; |
|
12510 | } |
|
12511 | } |
|
12512 | else { |
|
12513 | return settings[name]; |
|
12514 | } |
|
12515 | }, |
|
12516 | internal: function(name, value) { |
|
12517 | if( $.isPlainObject(name) ) { |
|
12518 | $.extend(true, module, name); |
|
12519 | } |
|
12520 | else if(value !== undefined) { |
|
12521 | module[name] = value; |
|
12522 | } |
|
12523 | else { |
|
12524 | return module[name]; |
|
12525 | } |
|
12526 | }, |
|
12527 | debug: function() { |
|
12528 | if(!settings.silent && settings.debug) { |
|
12529 | if(settings.performance) { |
|
12530 | module.performance.log(arguments); |
|
12531 | } |
|
12532 | else { |
|
12533 | module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':'); |
|
12534 | module.debug.apply(console, arguments); |
|
12535 | } |
|
12536 | } |
|
12537 | }, |
|
12538 | verbose: function() { |
|
12539 | if(!settings.silent && settings.verbose && settings.debug) { |
|
12540 | if(settings.performance) { |
|
12541 | module.performance.log(arguments); |
|
12542 | } |
|
12543 | else { |
|
12544 | module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':'); |
|
12545 | module.verbose.apply(console, arguments); |
|
12546 | } |
|
12547 | } |
|
12548 | }, |
|
12549 | error: function() { |
|
12550 | if(!settings.silent) { |
|
12551 | module.error = Function.prototype.bind.call(console.error, console, settings.name + ':'); |
|
12552 | module.error.apply(console, arguments); |
|
12553 | } |
|
12554 | }, |
|
12555 | performance: { |
|
12556 | log: function(message) { |
|
12557 | var |
|
12558 | currentTime, |
|
12559 | executionTime, |
|
12560 | previousTime |
|
12561 | ; |
|
12562 | if(settings.performance) { |
|
12563 | currentTime = new Date().getTime(); |
|
12564 | previousTime = time || currentTime; |
|
12565 | executionTime = currentTime - previousTime; |
|
12566 | time = currentTime; |
|
12567 | performance.push({ |
|
12568 | 'Name' : message[0], |
|
12569 | 'Arguments' : [].slice.call(message, 1) || '', |
|
12570 | 'Element' : element, |
|
12571 | 'Execution Time' : executionTime |
|
12572 | }); |
|
12573 | } |
|
12574 | clearTimeout(module.performance.timer); |
|
12575 | module.performance.timer = setTimeout(module.performance.display, 500); |
|
12576 | }, |
|
12577 | display: function() { |
|
12578 | var |
|
12579 | title = settings.name + ':', |
|
12580 | totalTime = 0 |
|
12581 | ; |
|
12582 | time = false; |
|
12583 | clearTimeout(module.performance.timer); |
|
12584 | $.each(performance, function(index, data) { |
|
12585 | totalTime += data['Execution Time']; |
|
12586 | }); |
|
12587 | title += ' ' + totalTime + 'ms'; |
|
12588 | if(moduleSelector) { |
|
12589 | title += ' \'' + moduleSelector + '\''; |
|
12590 | } |
|
12591 | if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) { |
|
12592 | console.groupCollapsed(title); |
|
12593 | if(console.table) { |
|
12594 | console.table(performance); |
|
12595 | } |
|
12596 | else { |
|
12597 | $.each(performance, function(index, data) { |
|
12598 | console.log(data['Name'] + ': ' + data['Execution Time']+'ms'); |
|
12599 | }); |
|
12600 | } |
|
12601 | console.groupEnd(); |
|
12602 | } |
|
12603 | performance = []; |
|
12604 | } |
|
12605 | }, |
|
12606 | invoke: function(query, passedArguments, context) { |
|
12607 | var |
|
12608 | object = instance, |
|
12609 | maxDepth, |
|
12610 | found, |
|
12611 | response |
|
12612 | ; |
|
12613 | passedArguments = passedArguments || queryArguments; |
|
12614 | context = element || context; |
|
12615 | if(typeof query == 'string' && object !== undefined) { |
|
12616 | query = query.split(/[\. ]/); |
|
12617 | maxDepth = query.length - 1; |
|
12618 | $.each(query, function(depth, value) { |
|
12619 | var camelCaseValue = (depth != maxDepth) |
|
12620 | ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1) |
|
12621 | : query |
|
12622 | ; |
|
12623 | if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) { |
|
12624 | object = object[camelCaseValue]; |
|
12625 | } |
|
12626 | else if( object[camelCaseValue] !== undefined ) { |
|
12627 | found = object[camelCaseValue]; |
|
12628 | return false; |
|
12629 | } |
|
12630 | else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) { |
|
12631 | object = object[value]; |
|
12632 | } |
|
12633 | else if( object[value] !== undefined ) { |
|
12634 | found = object[value]; |
|
12635 | return false; |
|
12636 | } |
|
12637 | else { |
|
12638 | module.error(error.method, query); |
|
12639 | return false; |
|
12640 | } |
|
12641 | }); |
|
12642 | } |
|
12643 | if ( $.isFunction( found ) ) { |
|
12644 | response = found.apply(context, passedArguments); |
|
12645 | } |
|
12646 | else if(found !== undefined) { |
|
12647 | response = found; |
|
12648 | } |
|
12649 | if($.isArray(returnedValue)) { |
|
12650 | returnedValue.push(response); |
|
12651 | } |
|
12652 | else if(returnedValue !== undefined) { |
|
12653 | returnedValue = [returnedValue, response]; |
|
12654 | } |
|
12655 | else if(response !== undefined) { |
|
12656 | returnedValue = response; |
|
12657 | } |
|
12658 | return found; |
|
12659 | } |
|
12660 | }; |
|
12661 | ||
12662 | if(methodInvoked) { |
|
12663 | if(instance === undefined) { |
|
12664 | module.initialize(); |
|
12665 | } |
|
12666 | module.invoke(query); |
|
12667 | } |
|
12668 | else { |
|
12669 | if(instance !== undefined) { |
|
12670 | instance.invoke('destroy'); |
|
12671 | } |
|
12672 | module.initialize(); |
|
12673 | } |
|
12674 | }) |
|
12675 | ; |
|
12676 | ||
12677 | return (returnedValue !== undefined) |
|
12678 | ? returnedValue |
|
12679 | : this |
|
12680 | ; |
|
12681 | }; |
|
12682 | ||
12683 | $.fn.progress.settings = { |
|
12684 | ||
12685 | name : 'Progress', |
|
12686 | namespace : 'progress', |
|
12687 | ||
12688 | silent : false, |
|
12689 | debug : false, |
|
12690 | verbose : false, |
|
12691 | performance : true, |
|
12692 | ||
12693 | random : { |
|
12694 | min : 2, |
|
12695 | max : 5 |
|
12696 | }, |
|
12697 | ||
12698 | duration : 300, |
|
12699 | ||
12700 | updateInterval : 'auto', |
|
12701 | ||
12702 | autoSuccess : true, |
|
12703 | showActivity : true, |
|
12704 | limitValues : true, |
|
12705 | ||
12706 | label : 'percent', |
|
12707 | precision : 0, |
|
12708 | framerate : (1000 / 30), /// 30 fps |
|
12709 | ||
12710 | percent : false, |
|
12711 | total : false, |
|
12712 | value : false, |
|
12713 | ||
12714 | // delay in ms for fail safe animation callback |
|
12715 | failSafeDelay : 100, |
|
12716 | ||
12717 | onLabelUpdate : function(state, text, value, total){ |
|
12718 | return text; |
|
12719 | }, |
|
12720 | onChange : function(percent, value, total){}, |
|
12721 | onSuccess : function(total){}, |
|
12722 | onActive : function(value, total){}, |
|
12723 | onError : function(value, total){}, |
|
12724 | onWarning : function(value, total){}, |
|
12725 | ||
12726 | error : { |
|
12727 | method : 'The method you called is not defined.', |
|
12728 | nonNumeric : 'Progress value is non numeric', |
|
12729 | tooHigh : 'Value specified is above 100%', |
|
12730 | tooLow : 'Value specified is below 0%' |
|
12731 | }, |
|
12732 | ||
12733 | regExp: { |
|
12734 | variable: /\{\$*[A-z0-9]+\}/g |
|
12735 | }, |
|
12736 | ||
12737 | metadata: { |
|
12738 | percent : 'percent', |
|
12739 | total : 'total', |
|
12740 | value : 'value' |
|
12741 | }, |
|
12742 | ||
12743 | selector : { |
|
12744 | bar : '> .bar', |
|
12745 | label : '> .label', |
|
12746 | progress : '.bar > .progress' |
|
12747 | }, |
|
12748 | ||
12749 | text : { |
|
12750 | active : false, |
|
12751 | error : false, |
|
12752 | success : false, |
|
12753 | warning : false, |
|
12754 | percent : '{percent}%', |
|
12755 | ratio : '{value} of {total}' |
|
12756 | }, |
|
12757 | ||
12758 | className : { |
|
12759 | active : 'active', |
|
12760 | error : 'error', |
|
12761 | success : 'success', |
|
12762 | warning : 'warning' |
|
12763 | } |
|
12764 | ||
12765 | }; |
|
12766 | ||
12767 | ||
12768 | })( jQuery, window, document ); |
|
12769 | ||
12770 | /*! |
|
12771 | * # Semantic UI 2.2.11 - Rating |
@@ 11-931 (lines=921) @@ | ||
8 | * |
|
9 | */ |
|
10 | ||
11 | ;(function ($, window, document, undefined) { |
|
12 | ||
13 | "use strict"; |
|
14 | ||
15 | window = (typeof window != 'undefined' && window.Math == Math) |
|
16 | ? window |
|
17 | : (typeof self != 'undefined' && self.Math == Math) |
|
18 | ? self |
|
19 | : Function('return this')() |
|
20 | ; |
|
21 | ||
22 | var |
|
23 | global = (typeof window != 'undefined' && window.Math == Math) |
|
24 | ? window |
|
25 | : (typeof self != 'undefined' && self.Math == Math) |
|
26 | ? self |
|
27 | : Function('return this')() |
|
28 | ; |
|
29 | ||
30 | $.fn.progress = function(parameters) { |
|
31 | var |
|
32 | $allModules = $(this), |
|
33 | ||
34 | moduleSelector = $allModules.selector || '', |
|
35 | ||
36 | time = new Date().getTime(), |
|
37 | performance = [], |
|
38 | ||
39 | query = arguments[0], |
|
40 | methodInvoked = (typeof query == 'string'), |
|
41 | queryArguments = [].slice.call(arguments, 1), |
|
42 | ||
43 | returnedValue |
|
44 | ; |
|
45 | ||
46 | $allModules |
|
47 | .each(function() { |
|
48 | var |
|
49 | settings = ( $.isPlainObject(parameters) ) |
|
50 | ? $.extend(true, {}, $.fn.progress.settings, parameters) |
|
51 | : $.extend({}, $.fn.progress.settings), |
|
52 | ||
53 | className = settings.className, |
|
54 | metadata = settings.metadata, |
|
55 | namespace = settings.namespace, |
|
56 | selector = settings.selector, |
|
57 | error = settings.error, |
|
58 | ||
59 | eventNamespace = '.' + namespace, |
|
60 | moduleNamespace = 'module-' + namespace, |
|
61 | ||
62 | $module = $(this), |
|
63 | $bar = $(this).find(selector.bar), |
|
64 | $progress = $(this).find(selector.progress), |
|
65 | $label = $(this).find(selector.label), |
|
66 | ||
67 | element = this, |
|
68 | instance = $module.data(moduleNamespace), |
|
69 | ||
70 | animating = false, |
|
71 | transitionEnd, |
|
72 | module |
|
73 | ; |
|
74 | ||
75 | module = { |
|
76 | ||
77 | initialize: function() { |
|
78 | module.debug('Initializing progress bar', settings); |
|
79 | ||
80 | module.set.duration(); |
|
81 | module.set.transitionEvent(); |
|
82 | ||
83 | module.read.metadata(); |
|
84 | module.read.settings(); |
|
85 | ||
86 | module.instantiate(); |
|
87 | }, |
|
88 | ||
89 | instantiate: function() { |
|
90 | module.verbose('Storing instance of progress', module); |
|
91 | instance = module; |
|
92 | $module |
|
93 | .data(moduleNamespace, module) |
|
94 | ; |
|
95 | }, |
|
96 | destroy: function() { |
|
97 | module.verbose('Destroying previous progress for', $module); |
|
98 | clearInterval(instance.interval); |
|
99 | module.remove.state(); |
|
100 | $module.removeData(moduleNamespace); |
|
101 | instance = undefined; |
|
102 | }, |
|
103 | ||
104 | reset: function() { |
|
105 | module.remove.nextValue(); |
|
106 | module.update.progress(0); |
|
107 | }, |
|
108 | ||
109 | complete: function() { |
|
110 | if(module.percent === undefined || module.percent < 100) { |
|
111 | module.remove.progressPoll(); |
|
112 | module.set.percent(100); |
|
113 | } |
|
114 | }, |
|
115 | ||
116 | read: { |
|
117 | metadata: function() { |
|
118 | var |
|
119 | data = { |
|
120 | percent : $module.data(metadata.percent), |
|
121 | total : $module.data(metadata.total), |
|
122 | value : $module.data(metadata.value) |
|
123 | } |
|
124 | ; |
|
125 | if(data.percent) { |
|
126 | module.debug('Current percent value set from metadata', data.percent); |
|
127 | module.set.percent(data.percent); |
|
128 | } |
|
129 | if(data.total) { |
|
130 | module.debug('Total value set from metadata', data.total); |
|
131 | module.set.total(data.total); |
|
132 | } |
|
133 | if(data.value) { |
|
134 | module.debug('Current value set from metadata', data.value); |
|
135 | module.set.value(data.value); |
|
136 | module.set.progress(data.value); |
|
137 | } |
|
138 | }, |
|
139 | settings: function() { |
|
140 | if(settings.total !== false) { |
|
141 | module.debug('Current total set in settings', settings.total); |
|
142 | module.set.total(settings.total); |
|
143 | } |
|
144 | if(settings.value !== false) { |
|
145 | module.debug('Current value set in settings', settings.value); |
|
146 | module.set.value(settings.value); |
|
147 | module.set.progress(module.value); |
|
148 | } |
|
149 | if(settings.percent !== false) { |
|
150 | module.debug('Current percent set in settings', settings.percent); |
|
151 | module.set.percent(settings.percent); |
|
152 | } |
|
153 | } |
|
154 | }, |
|
155 | ||
156 | bind: { |
|
157 | transitionEnd: function(callback) { |
|
158 | var |
|
159 | transitionEnd = module.get.transitionEnd() |
|
160 | ; |
|
161 | $bar |
|
162 | .one(transitionEnd + eventNamespace, function(event) { |
|
163 | clearTimeout(module.failSafeTimer); |
|
164 | callback.call(this, event); |
|
165 | }) |
|
166 | ; |
|
167 | module.failSafeTimer = setTimeout(function() { |
|
168 | $bar.triggerHandler(transitionEnd); |
|
169 | }, settings.duration + settings.failSafeDelay); |
|
170 | module.verbose('Adding fail safe timer', module.timer); |
|
171 | } |
|
172 | }, |
|
173 | ||
174 | increment: function(incrementValue) { |
|
175 | var |
|
176 | maxValue, |
|
177 | startValue, |
|
178 | newValue |
|
179 | ; |
|
180 | if( module.has.total() ) { |
|
181 | startValue = module.get.value(); |
|
182 | incrementValue = incrementValue || 1; |
|
183 | newValue = startValue + incrementValue; |
|
184 | } |
|
185 | else { |
|
186 | startValue = module.get.percent(); |
|
187 | incrementValue = incrementValue || module.get.randomValue(); |
|
188 | ||
189 | newValue = startValue + incrementValue; |
|
190 | maxValue = 100; |
|
191 | module.debug('Incrementing percentage by', startValue, newValue); |
|
192 | } |
|
193 | newValue = module.get.normalizedValue(newValue); |
|
194 | module.set.progress(newValue); |
|
195 | }, |
|
196 | decrement: function(decrementValue) { |
|
197 | var |
|
198 | total = module.get.total(), |
|
199 | startValue, |
|
200 | newValue |
|
201 | ; |
|
202 | if(total) { |
|
203 | startValue = module.get.value(); |
|
204 | decrementValue = decrementValue || 1; |
|
205 | newValue = startValue - decrementValue; |
|
206 | module.debug('Decrementing value by', decrementValue, startValue); |
|
207 | } |
|
208 | else { |
|
209 | startValue = module.get.percent(); |
|
210 | decrementValue = decrementValue || module.get.randomValue(); |
|
211 | newValue = startValue - decrementValue; |
|
212 | module.debug('Decrementing percentage by', decrementValue, startValue); |
|
213 | } |
|
214 | newValue = module.get.normalizedValue(newValue); |
|
215 | module.set.progress(newValue); |
|
216 | }, |
|
217 | ||
218 | has: { |
|
219 | progressPoll: function() { |
|
220 | return module.progressPoll; |
|
221 | }, |
|
222 | total: function() { |
|
223 | return (module.get.total() !== false); |
|
224 | } |
|
225 | }, |
|
226 | ||
227 | get: { |
|
228 | text: function(templateText) { |
|
229 | var |
|
230 | value = module.value || 0, |
|
231 | total = module.total || 0, |
|
232 | percent = (animating) |
|
233 | ? module.get.displayPercent() |
|
234 | : module.percent || 0, |
|
235 | left = (module.total > 0) |
|
236 | ? (total - value) |
|
237 | : (100 - percent) |
|
238 | ; |
|
239 | templateText = templateText || ''; |
|
240 | templateText = templateText |
|
241 | .replace('{value}', value) |
|
242 | .replace('{total}', total) |
|
243 | .replace('{left}', left) |
|
244 | .replace('{percent}', percent) |
|
245 | ; |
|
246 | module.verbose('Adding variables to progress bar text', templateText); |
|
247 | return templateText; |
|
248 | }, |
|
249 | ||
250 | normalizedValue: function(value) { |
|
251 | if(value < 0) { |
|
252 | module.debug('Value cannot decrement below 0'); |
|
253 | return 0; |
|
254 | } |
|
255 | if(module.has.total()) { |
|
256 | if(value > module.total) { |
|
257 | module.debug('Value cannot increment above total', module.total); |
|
258 | return module.total; |
|
259 | } |
|
260 | } |
|
261 | else if(value > 100 ) { |
|
262 | module.debug('Value cannot increment above 100 percent'); |
|
263 | return 100; |
|
264 | } |
|
265 | return value; |
|
266 | }, |
|
267 | ||
268 | updateInterval: function() { |
|
269 | if(settings.updateInterval == 'auto') { |
|
270 | return settings.duration; |
|
271 | } |
|
272 | return settings.updateInterval; |
|
273 | }, |
|
274 | ||
275 | randomValue: function() { |
|
276 | module.debug('Generating random increment percentage'); |
|
277 | return Math.floor((Math.random() * settings.random.max) + settings.random.min); |
|
278 | }, |
|
279 | ||
280 | numericValue: function(value) { |
|
281 | return (typeof value === 'string') |
|
282 | ? (value.replace(/[^\d.]/g, '') !== '') |
|
283 | ? +(value.replace(/[^\d.]/g, '')) |
|
284 | : false |
|
285 | : value |
|
286 | ; |
|
287 | }, |
|
288 | ||
289 | transitionEnd: function() { |
|
290 | var |
|
291 | element = document.createElement('element'), |
|
292 | transitions = { |
|
293 | 'transition' :'transitionend', |
|
294 | 'OTransition' :'oTransitionEnd', |
|
295 | 'MozTransition' :'transitionend', |
|
296 | 'WebkitTransition' :'webkitTransitionEnd' |
|
297 | }, |
|
298 | transition |
|
299 | ; |
|
300 | for(transition in transitions){ |
|
301 | if( element.style[transition] !== undefined ){ |
|
302 | return transitions[transition]; |
|
303 | } |
|
304 | } |
|
305 | }, |
|
306 | ||
307 | // gets current displayed percentage (if animating values this is the intermediary value) |
|
308 | displayPercent: function() { |
|
309 | var |
|
310 | barWidth = $bar.width(), |
|
311 | totalWidth = $module.width(), |
|
312 | minDisplay = parseInt($bar.css('min-width'), 10), |
|
313 | displayPercent = (barWidth > minDisplay) |
|
314 | ? (barWidth / totalWidth * 100) |
|
315 | : module.percent |
|
316 | ; |
|
317 | return (settings.precision > 0) |
|
318 | ? Math.round(displayPercent * (10 * settings.precision)) / (10 * settings.precision) |
|
319 | : Math.round(displayPercent) |
|
320 | ; |
|
321 | }, |
|
322 | ||
323 | percent: function() { |
|
324 | return module.percent || 0; |
|
325 | }, |
|
326 | value: function() { |
|
327 | return module.nextValue || module.value || 0; |
|
328 | }, |
|
329 | total: function() { |
|
330 | return module.total || false; |
|
331 | } |
|
332 | }, |
|
333 | ||
334 | create: { |
|
335 | progressPoll: function() { |
|
336 | module.progressPoll = setTimeout(function() { |
|
337 | module.update.toNextValue(); |
|
338 | module.remove.progressPoll(); |
|
339 | }, module.get.updateInterval()); |
|
340 | }, |
|
341 | }, |
|
342 | ||
343 | is: { |
|
344 | complete: function() { |
|
345 | return module.is.success() || module.is.warning() || module.is.error(); |
|
346 | }, |
|
347 | success: function() { |
|
348 | return $module.hasClass(className.success); |
|
349 | }, |
|
350 | warning: function() { |
|
351 | return $module.hasClass(className.warning); |
|
352 | }, |
|
353 | error: function() { |
|
354 | return $module.hasClass(className.error); |
|
355 | }, |
|
356 | active: function() { |
|
357 | return $module.hasClass(className.active); |
|
358 | }, |
|
359 | visible: function() { |
|
360 | return $module.is(':visible'); |
|
361 | } |
|
362 | }, |
|
363 | ||
364 | remove: { |
|
365 | progressPoll: function() { |
|
366 | module.verbose('Removing progress poll timer'); |
|
367 | if(module.progressPoll) { |
|
368 | clearTimeout(module.progressPoll); |
|
369 | delete module.progressPoll; |
|
370 | } |
|
371 | }, |
|
372 | nextValue: function() { |
|
373 | module.verbose('Removing progress value stored for next update'); |
|
374 | delete module.nextValue; |
|
375 | }, |
|
376 | state: function() { |
|
377 | module.verbose('Removing stored state'); |
|
378 | delete module.total; |
|
379 | delete module.percent; |
|
380 | delete module.value; |
|
381 | }, |
|
382 | active: function() { |
|
383 | module.verbose('Removing active state'); |
|
384 | $module.removeClass(className.active); |
|
385 | }, |
|
386 | success: function() { |
|
387 | module.verbose('Removing success state'); |
|
388 | $module.removeClass(className.success); |
|
389 | }, |
|
390 | warning: function() { |
|
391 | module.verbose('Removing warning state'); |
|
392 | $module.removeClass(className.warning); |
|
393 | }, |
|
394 | error: function() { |
|
395 | module.verbose('Removing error state'); |
|
396 | $module.removeClass(className.error); |
|
397 | } |
|
398 | }, |
|
399 | ||
400 | set: { |
|
401 | barWidth: function(value) { |
|
402 | if(value > 100) { |
|
403 | module.error(error.tooHigh, value); |
|
404 | } |
|
405 | else if (value < 0) { |
|
406 | module.error(error.tooLow, value); |
|
407 | } |
|
408 | else { |
|
409 | $bar |
|
410 | .css('width', value + '%') |
|
411 | ; |
|
412 | $module |
|
413 | .attr('data-percent', parseInt(value, 10)) |
|
414 | ; |
|
415 | } |
|
416 | }, |
|
417 | duration: function(duration) { |
|
418 | duration = duration || settings.duration; |
|
419 | duration = (typeof duration == 'number') |
|
420 | ? duration + 'ms' |
|
421 | : duration |
|
422 | ; |
|
423 | module.verbose('Setting progress bar transition duration', duration); |
|
424 | $bar |
|
425 | .css({ |
|
426 | 'transition-duration': duration |
|
427 | }) |
|
428 | ; |
|
429 | }, |
|
430 | percent: function(percent) { |
|
431 | percent = (typeof percent == 'string') |
|
432 | ? +(percent.replace('%', '')) |
|
433 | : percent |
|
434 | ; |
|
435 | // round display percentage |
|
436 | percent = (settings.precision > 0) |
|
437 | ? Math.round(percent * (10 * settings.precision)) / (10 * settings.precision) |
|
438 | : Math.round(percent) |
|
439 | ; |
|
440 | module.percent = percent; |
|
441 | if( !module.has.total() ) { |
|
442 | module.value = (settings.precision > 0) |
|
443 | ? Math.round( (percent / 100) * module.total * (10 * settings.precision)) / (10 * settings.precision) |
|
444 | : Math.round( (percent / 100) * module.total * 10) / 10 |
|
445 | ; |
|
446 | if(settings.limitValues) { |
|
447 | module.value = (module.value > 100) |
|
448 | ? 100 |
|
449 | : (module.value < 0) |
|
450 | ? 0 |
|
451 | : module.value |
|
452 | ; |
|
453 | } |
|
454 | } |
|
455 | module.set.barWidth(percent); |
|
456 | module.set.labelInterval(); |
|
457 | module.set.labels(); |
|
458 | settings.onChange.call(element, percent, module.value, module.total); |
|
459 | }, |
|
460 | labelInterval: function() { |
|
461 | var |
|
462 | animationCallback = function() { |
|
463 | module.verbose('Bar finished animating, removing continuous label updates'); |
|
464 | clearInterval(module.interval); |
|
465 | animating = false; |
|
466 | module.set.labels(); |
|
467 | } |
|
468 | ; |
|
469 | clearInterval(module.interval); |
|
470 | module.bind.transitionEnd(animationCallback); |
|
471 | animating = true; |
|
472 | module.interval = setInterval(function() { |
|
473 | var |
|
474 | isInDOM = $.contains(document.documentElement, element) |
|
475 | ; |
|
476 | if(!isInDOM) { |
|
477 | clearInterval(module.interval); |
|
478 | animating = false; |
|
479 | } |
|
480 | module.set.labels(); |
|
481 | }, settings.framerate); |
|
482 | }, |
|
483 | labels: function() { |
|
484 | module.verbose('Setting both bar progress and outer label text'); |
|
485 | module.set.barLabel(); |
|
486 | module.set.state(); |
|
487 | }, |
|
488 | label: function(text) { |
|
489 | text = text || ''; |
|
490 | if(text) { |
|
491 | text = module.get.text(text); |
|
492 | module.verbose('Setting label to text', text); |
|
493 | $label.text(text); |
|
494 | } |
|
495 | }, |
|
496 | state: function(percent) { |
|
497 | percent = (percent !== undefined) |
|
498 | ? percent |
|
499 | : module.percent |
|
500 | ; |
|
501 | if(percent === 100) { |
|
502 | if(settings.autoSuccess && !(module.is.warning() || module.is.error() || module.is.success())) { |
|
503 | module.set.success(); |
|
504 | module.debug('Automatically triggering success at 100%'); |
|
505 | } |
|
506 | else { |
|
507 | module.verbose('Reached 100% removing active state'); |
|
508 | module.remove.active(); |
|
509 | module.remove.progressPoll(); |
|
510 | } |
|
511 | } |
|
512 | else if(percent > 0) { |
|
513 | module.verbose('Adjusting active progress bar label', percent); |
|
514 | module.set.active(); |
|
515 | } |
|
516 | else { |
|
517 | module.remove.active(); |
|
518 | module.set.label(settings.text.active); |
|
519 | } |
|
520 | }, |
|
521 | barLabel: function(text) { |
|
522 | if(text !== undefined) { |
|
523 | $progress.text( module.get.text(text) ); |
|
524 | } |
|
525 | else if(settings.label == 'ratio' && module.total) { |
|
526 | module.verbose('Adding ratio to bar label'); |
|
527 | $progress.text( module.get.text(settings.text.ratio) ); |
|
528 | } |
|
529 | else if(settings.label == 'percent') { |
|
530 | module.verbose('Adding percentage to bar label'); |
|
531 | $progress.text( module.get.text(settings.text.percent) ); |
|
532 | } |
|
533 | }, |
|
534 | active: function(text) { |
|
535 | text = text || settings.text.active; |
|
536 | module.debug('Setting active state'); |
|
537 | if(settings.showActivity && !module.is.active() ) { |
|
538 | $module.addClass(className.active); |
|
539 | } |
|
540 | module.remove.warning(); |
|
541 | module.remove.error(); |
|
542 | module.remove.success(); |
|
543 | text = settings.onLabelUpdate('active', text, module.value, module.total); |
|
544 | if(text) { |
|
545 | module.set.label(text); |
|
546 | } |
|
547 | module.bind.transitionEnd(function() { |
|
548 | settings.onActive.call(element, module.value, module.total); |
|
549 | }); |
|
550 | }, |
|
551 | success : function(text) { |
|
552 | text = text || settings.text.success || settings.text.active; |
|
553 | module.debug('Setting success state'); |
|
554 | $module.addClass(className.success); |
|
555 | module.remove.active(); |
|
556 | module.remove.warning(); |
|
557 | module.remove.error(); |
|
558 | module.complete(); |
|
559 | if(settings.text.success) { |
|
560 | text = settings.onLabelUpdate('success', text, module.value, module.total); |
|
561 | module.set.label(text); |
|
562 | } |
|
563 | else { |
|
564 | text = settings.onLabelUpdate('active', text, module.value, module.total); |
|
565 | module.set.label(text); |
|
566 | } |
|
567 | module.bind.transitionEnd(function() { |
|
568 | settings.onSuccess.call(element, module.total); |
|
569 | }); |
|
570 | }, |
|
571 | warning : function(text) { |
|
572 | text = text || settings.text.warning; |
|
573 | module.debug('Setting warning state'); |
|
574 | $module.addClass(className.warning); |
|
575 | module.remove.active(); |
|
576 | module.remove.success(); |
|
577 | module.remove.error(); |
|
578 | module.complete(); |
|
579 | text = settings.onLabelUpdate('warning', text, module.value, module.total); |
|
580 | if(text) { |
|
581 | module.set.label(text); |
|
582 | } |
|
583 | module.bind.transitionEnd(function() { |
|
584 | settings.onWarning.call(element, module.value, module.total); |
|
585 | }); |
|
586 | }, |
|
587 | error : function(text) { |
|
588 | text = text || settings.text.error; |
|
589 | module.debug('Setting error state'); |
|
590 | $module.addClass(className.error); |
|
591 | module.remove.active(); |
|
592 | module.remove.success(); |
|
593 | module.remove.warning(); |
|
594 | module.complete(); |
|
595 | text = settings.onLabelUpdate('error', text, module.value, module.total); |
|
596 | if(text) { |
|
597 | module.set.label(text); |
|
598 | } |
|
599 | module.bind.transitionEnd(function() { |
|
600 | settings.onError.call(element, module.value, module.total); |
|
601 | }); |
|
602 | }, |
|
603 | transitionEvent: function() { |
|
604 | transitionEnd = module.get.transitionEnd(); |
|
605 | }, |
|
606 | total: function(totalValue) { |
|
607 | module.total = totalValue; |
|
608 | }, |
|
609 | value: function(value) { |
|
610 | module.value = value; |
|
611 | }, |
|
612 | progress: function(value) { |
|
613 | if(!module.has.progressPoll()) { |
|
614 | module.debug('First update in progress update interval, immediately updating', value); |
|
615 | module.update.progress(value); |
|
616 | module.create.progressPoll(); |
|
617 | } |
|
618 | else { |
|
619 | module.debug('Updated within interval, setting next update to use new value', value); |
|
620 | module.set.nextValue(value); |
|
621 | } |
|
622 | }, |
|
623 | nextValue: function(value) { |
|
624 | module.nextValue = value; |
|
625 | } |
|
626 | }, |
|
627 | ||
628 | update: { |
|
629 | toNextValue: function() { |
|
630 | var |
|
631 | nextValue = module.nextValue |
|
632 | ; |
|
633 | if(nextValue) { |
|
634 | module.debug('Update interval complete using last updated value', nextValue); |
|
635 | module.update.progress(nextValue); |
|
636 | module.remove.nextValue(); |
|
637 | } |
|
638 | }, |
|
639 | progress: function(value) { |
|
640 | var |
|
641 | percentComplete |
|
642 | ; |
|
643 | value = module.get.numericValue(value); |
|
644 | if(value === false) { |
|
645 | module.error(error.nonNumeric, value); |
|
646 | } |
|
647 | value = module.get.normalizedValue(value); |
|
648 | if( module.has.total() ) { |
|
649 | module.set.value(value); |
|
650 | percentComplete = (value / module.total) * 100; |
|
651 | module.debug('Calculating percent complete from total', percentComplete); |
|
652 | module.set.percent( percentComplete ); |
|
653 | } |
|
654 | else { |
|
655 | percentComplete = value; |
|
656 | module.debug('Setting value to exact percentage value', percentComplete); |
|
657 | module.set.percent( percentComplete ); |
|
658 | } |
|
659 | } |
|
660 | }, |
|
661 | ||
662 | setting: function(name, value) { |
|
663 | module.debug('Changing setting', name, value); |
|
664 | if( $.isPlainObject(name) ) { |
|
665 | $.extend(true, settings, name); |
|
666 | } |
|
667 | else if(value !== undefined) { |
|
668 | if($.isPlainObject(settings[name])) { |
|
669 | $.extend(true, settings[name], value); |
|
670 | } |
|
671 | else { |
|
672 | settings[name] = value; |
|
673 | } |
|
674 | } |
|
675 | else { |
|
676 | return settings[name]; |
|
677 | } |
|
678 | }, |
|
679 | internal: function(name, value) { |
|
680 | if( $.isPlainObject(name) ) { |
|
681 | $.extend(true, module, name); |
|
682 | } |
|
683 | else if(value !== undefined) { |
|
684 | module[name] = value; |
|
685 | } |
|
686 | else { |
|
687 | return module[name]; |
|
688 | } |
|
689 | }, |
|
690 | debug: function() { |
|
691 | if(!settings.silent && settings.debug) { |
|
692 | if(settings.performance) { |
|
693 | module.performance.log(arguments); |
|
694 | } |
|
695 | else { |
|
696 | module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':'); |
|
697 | module.debug.apply(console, arguments); |
|
698 | } |
|
699 | } |
|
700 | }, |
|
701 | verbose: function() { |
|
702 | if(!settings.silent && settings.verbose && settings.debug) { |
|
703 | if(settings.performance) { |
|
704 | module.performance.log(arguments); |
|
705 | } |
|
706 | else { |
|
707 | module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':'); |
|
708 | module.verbose.apply(console, arguments); |
|
709 | } |
|
710 | } |
|
711 | }, |
|
712 | error: function() { |
|
713 | if(!settings.silent) { |
|
714 | module.error = Function.prototype.bind.call(console.error, console, settings.name + ':'); |
|
715 | module.error.apply(console, arguments); |
|
716 | } |
|
717 | }, |
|
718 | performance: { |
|
719 | log: function(message) { |
|
720 | var |
|
721 | currentTime, |
|
722 | executionTime, |
|
723 | previousTime |
|
724 | ; |
|
725 | if(settings.performance) { |
|
726 | currentTime = new Date().getTime(); |
|
727 | previousTime = time || currentTime; |
|
728 | executionTime = currentTime - previousTime; |
|
729 | time = currentTime; |
|
730 | performance.push({ |
|
731 | 'Name' : message[0], |
|
732 | 'Arguments' : [].slice.call(message, 1) || '', |
|
733 | 'Element' : element, |
|
734 | 'Execution Time' : executionTime |
|
735 | }); |
|
736 | } |
|
737 | clearTimeout(module.performance.timer); |
|
738 | module.performance.timer = setTimeout(module.performance.display, 500); |
|
739 | }, |
|
740 | display: function() { |
|
741 | var |
|
742 | title = settings.name + ':', |
|
743 | totalTime = 0 |
|
744 | ; |
|
745 | time = false; |
|
746 | clearTimeout(module.performance.timer); |
|
747 | $.each(performance, function(index, data) { |
|
748 | totalTime += data['Execution Time']; |
|
749 | }); |
|
750 | title += ' ' + totalTime + 'ms'; |
|
751 | if(moduleSelector) { |
|
752 | title += ' \'' + moduleSelector + '\''; |
|
753 | } |
|
754 | if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) { |
|
755 | console.groupCollapsed(title); |
|
756 | if(console.table) { |
|
757 | console.table(performance); |
|
758 | } |
|
759 | else { |
|
760 | $.each(performance, function(index, data) { |
|
761 | console.log(data['Name'] + ': ' + data['Execution Time']+'ms'); |
|
762 | }); |
|
763 | } |
|
764 | console.groupEnd(); |
|
765 | } |
|
766 | performance = []; |
|
767 | } |
|
768 | }, |
|
769 | invoke: function(query, passedArguments, context) { |
|
770 | var |
|
771 | object = instance, |
|
772 | maxDepth, |
|
773 | found, |
|
774 | response |
|
775 | ; |
|
776 | passedArguments = passedArguments || queryArguments; |
|
777 | context = element || context; |
|
778 | if(typeof query == 'string' && object !== undefined) { |
|
779 | query = query.split(/[\. ]/); |
|
780 | maxDepth = query.length - 1; |
|
781 | $.each(query, function(depth, value) { |
|
782 | var camelCaseValue = (depth != maxDepth) |
|
783 | ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1) |
|
784 | : query |
|
785 | ; |
|
786 | if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) { |
|
787 | object = object[camelCaseValue]; |
|
788 | } |
|
789 | else if( object[camelCaseValue] !== undefined ) { |
|
790 | found = object[camelCaseValue]; |
|
791 | return false; |
|
792 | } |
|
793 | else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) { |
|
794 | object = object[value]; |
|
795 | } |
|
796 | else if( object[value] !== undefined ) { |
|
797 | found = object[value]; |
|
798 | return false; |
|
799 | } |
|
800 | else { |
|
801 | module.error(error.method, query); |
|
802 | return false; |
|
803 | } |
|
804 | }); |
|
805 | } |
|
806 | if ( $.isFunction( found ) ) { |
|
807 | response = found.apply(context, passedArguments); |
|
808 | } |
|
809 | else if(found !== undefined) { |
|
810 | response = found; |
|
811 | } |
|
812 | if($.isArray(returnedValue)) { |
|
813 | returnedValue.push(response); |
|
814 | } |
|
815 | else if(returnedValue !== undefined) { |
|
816 | returnedValue = [returnedValue, response]; |
|
817 | } |
|
818 | else if(response !== undefined) { |
|
819 | returnedValue = response; |
|
820 | } |
|
821 | return found; |
|
822 | } |
|
823 | }; |
|
824 | ||
825 | if(methodInvoked) { |
|
826 | if(instance === undefined) { |
|
827 | module.initialize(); |
|
828 | } |
|
829 | module.invoke(query); |
|
830 | } |
|
831 | else { |
|
832 | if(instance !== undefined) { |
|
833 | instance.invoke('destroy'); |
|
834 | } |
|
835 | module.initialize(); |
|
836 | } |
|
837 | }) |
|
838 | ; |
|
839 | ||
840 | return (returnedValue !== undefined) |
|
841 | ? returnedValue |
|
842 | : this |
|
843 | ; |
|
844 | }; |
|
845 | ||
846 | $.fn.progress.settings = { |
|
847 | ||
848 | name : 'Progress', |
|
849 | namespace : 'progress', |
|
850 | ||
851 | silent : false, |
|
852 | debug : false, |
|
853 | verbose : false, |
|
854 | performance : true, |
|
855 | ||
856 | random : { |
|
857 | min : 2, |
|
858 | max : 5 |
|
859 | }, |
|
860 | ||
861 | duration : 300, |
|
862 | ||
863 | updateInterval : 'auto', |
|
864 | ||
865 | autoSuccess : true, |
|
866 | showActivity : true, |
|
867 | limitValues : true, |
|
868 | ||
869 | label : 'percent', |
|
870 | precision : 0, |
|
871 | framerate : (1000 / 30), /// 30 fps |
|
872 | ||
873 | percent : false, |
|
874 | total : false, |
|
875 | value : false, |
|
876 | ||
877 | // delay in ms for fail safe animation callback |
|
878 | failSafeDelay : 100, |
|
879 | ||
880 | onLabelUpdate : function(state, text, value, total){ |
|
881 | return text; |
|
882 | }, |
|
883 | onChange : function(percent, value, total){}, |
|
884 | onSuccess : function(total){}, |
|
885 | onActive : function(value, total){}, |
|
886 | onError : function(value, total){}, |
|
887 | onWarning : function(value, total){}, |
|
888 | ||
889 | error : { |
|
890 | method : 'The method you called is not defined.', |
|
891 | nonNumeric : 'Progress value is non numeric', |
|
892 | tooHigh : 'Value specified is above 100%', |
|
893 | tooLow : 'Value specified is below 0%' |
|
894 | }, |
|
895 | ||
896 | regExp: { |
|
897 | variable: /\{\$*[A-z0-9]+\}/g |
|
898 | }, |
|
899 | ||
900 | metadata: { |
|
901 | percent : 'percent', |
|
902 | total : 'total', |
|
903 | value : 'value' |
|
904 | }, |
|
905 | ||
906 | selector : { |
|
907 | bar : '> .bar', |
|
908 | label : '> .label', |
|
909 | progress : '.bar > .progress' |
|
910 | }, |
|
911 | ||
912 | text : { |
|
913 | active : false, |
|
914 | error : false, |
|
915 | success : false, |
|
916 | warning : false, |
|
917 | percent : '{percent}%', |
|
918 | ratio : '{value} of {total}' |
|
919 | }, |
|
920 | ||
921 | className : { |
|
922 | active : 'active', |
|
923 | error : 'error', |
|
924 | success : 'success', |
|
925 | warning : 'warning' |
|
926 | } |
|
927 | ||
928 | }; |
|
929 | ||
930 | ||
931 | })( jQuery, window, document ); |
|
932 |