perkeep/app/scanningcabinet/ui/jQCloud.js

9 lines
8.0 KiB
JavaScript
Raw Normal View History

/*!
* jQCloud 2.0.2
* Copyright 2011 Luca Ongaro (http://www.lucaongaro.eu)
* Copyright 2013 Daniel White (http://www.developerdan.com)
* Copyright 20142016 Damien "Mistic" Sorel (http://www.strangeplanet.fr)
* Licensed under MIT (http://opensource.org/licenses/MIT)
*/
!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],b):"object"==typeof module&&module.exports?module.exports=b(require("jquery")):b(a.jQuery)}(this,function(a){"use strict";function b(a,b,c){var d={pid:null,last:0};return function(){function e(){return d.last=(new Date).getTime(),a.apply(c||h,Array.prototype.slice.call(g))}var f=(new Date).getTime()-d.last,g=arguments,h=this;return f>b?e():(clearTimeout(d.pid),void(d.pid=setTimeout(e,b-f)))}}var c=function(b,c,d){this.$element=a(b),this.word_array=c||[],this.options=d,this.sizeGenerator=null,this.colorGenerator=null,this.data={placed_words:[],timeouts:{},namespace:null,step:null,angle:null,aspect_ratio:null,max_weight:null,min_weight:null,sizes:[],colors:[]},this.initialize()};c.DEFAULTS={width:100,height:100,center:{x:.5,y:.5},steps:10,delay:null,shape:"elliptic",classPattern:"w{n}",encodeURI:!0,removeOverflowing:!0,afterCloudRender:null,autoResize:!1,colors:null,fontSize:null,template:null},c.prototype={initialize:function(){if(this.options.width?this.$element.width(this.options.width):this.options.width=this.$element.width(),this.options.height?this.$element.height(this.options.height):this.options.height=this.$element.height(),this.options=a.extend(!0,{},c.DEFAULTS,this.options),null===this.options.delay&&(this.options.delay=this.word_array.length>50?10:0),this.options.center.x>1&&(this.options.center.x=this.options.center.x/this.options.width,this.options.center.y=this.options.center.y/this.options.height),"function"==typeof this.options.colors)this.colorGenerator=this.options.colors;else if(a.isArray(this.options.colors)){var d=this.options.colors.length;if(d>0){if(d<this.options.steps)for(var e=d;e<this.options.steps;e++)this.options.colors[e]=this.options.colors[d-1];this.colorGenerator=function(a){return this.options.colors[this.options.steps-a]}}}if("function"==typeof this.options.fontSize)this.sizeGenerator=this.options.fontSize;else if(a.isPlainObject(this.options.fontSize))this.sizeGenerator=function(a,b,c){var d=a*this.options.fontSize.from,e=a*this.options.fontSize.to;return Math.round(e+1*(d-e)/(this.options.steps-1)*(c-1))+"px"};else if(a.isArray(this.options.fontSize)){var f=this.options.fontSize.length;if(f>0){if(f<this.options.steps)for(var g=f;g<this.options.steps;g++)this.options.fontSize[g]=this.options.fontSize[f-1];this.sizeGenerator=function(a,b,c){return this.options.fontSize[this.options.steps-c]}}}this.data.angle=6.28*Math.random(),this.data.step="rectangular"===this.options.shape?18:2,this.data.aspect_ratio=this.options.width/this.options.height,this.clearTimeouts(),this.data.namespace=(this.$element.attr("id")||Math.floor(1e6*Math.random()).toString(36))+"_word_",this.$element.addClass("jqcloud"),"static"===this.$element.css("position")&&this.$element.css("position","relative"),this.createTimeout(a.proxy(this.drawWordCloud,this),10),this.options.autoResize&&a(window).on("resize",b(this.resize,50,this))},createTimeout:function(b,c){var d=setTimeout(a.proxy(function(){delete this.data.timeouts[d],b()},this),c);this.data.timeouts[d]=!0},clearTimeouts:function(){a.each(this.data.timeouts,function(a){clearTimeout(a)}),this.data.timeouts={}},overlapping:function(a,b){return Math.abs(2*a.left+a.width-2*b.left-b.width)<a.width+b.width&&Math.abs(2*a.top+a.height-2*b.top-b.height)<a.height+b.height?!0:!1},hitTest:function(a){for(var b=0,c=this.data.placed_words.length;c>b;b++)if(this.overlapping(a,this.data.placed_words[b]))return!0;return!1},drawWordCloud:function(){var a,b;if(this.$element.children('[id^="'+this.data.namespace+'"]').remove(),0!==this.word_array.length){for(a=0,b=this.word_array.length;b>a;a++)this.word_array[a].weight=parseFloat(this.word_array[a].weight,10);if(this.word_array.sort(function(a,b){return b.weight-a.weight}),this.data.max_weight=this.word_array[0].weight,this.data.min_weight=this.word_array[this.word_array.length-1].weight,this.data.colors=[],this.colorGenerator)for(a=0;a<this.options.steps;a++)this.data.colors.push(this.colorGenerator(a+1));if(this.data.sizes=[],this.sizeGenerator)for(a=0;a<this.