Guest User

Untitled

a guest
Jul 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. YUI.add('gallery-progress-bar', function(Y) {
  2.  
  3. Y.ProgressBar = Y.Base.create('progressBar', Y.Widget, [], {
  4.  
  5. currentLabel: '',
  6.  
  7. _anim: null,
  8.  
  9.  
  10. /** set up **/
  11. renderUI: function() {
  12. Y.log('renderUI', 'info', 'Y.ProgressBar');
  13. this.get('contentBox').append(Y.substitute(this.get('layout'), {
  14. sliderClass: this.getClassName('slider'),
  15. labelClass: this.getClassName('label')
  16. }));
  17. },
  18.  
  19. bindUI: function() {
  20. Y.log('bindUI', 'info', 'Y.ProgressBar');
  21. this.after('labelChange', this._updateLabel);
  22. this.after('progressChange', this._updateBackground);
  23. },
  24.  
  25. syncUI: function() {
  26. Y.log('syncUI', 'info', 'Y.ProgressBar');
  27. this._updateBackground();
  28. },
  29.  
  30. /** A little bit of sugar **/
  31. increment: function(step) {
  32. Y.log('increment', 'info', 'Y.ProgressBar');
  33. step = step || 1;
  34. this.set('progress', this.get('progress') + parseFloat(step, 10));
  35. return this;
  36. },
  37.  
  38. decrement: function(step) {
  39. Y.log('decrement', 'info', 'Y.ProgressBar');
  40. step = step || 1;
  41. this.set('progress', this.get('progress') - parseFloat(step, 10));
  42. return this;
  43. },
  44.  
  45. update: function(progress) {
  46. Y.log('update', 'info', 'Y.ProgressBar');
  47. this.set('progress', progress);
  48. return this;
  49. },
  50.  
  51. setLabelAt: function(position, value) {
  52. Y.log('setLabelAt', 'info', 'Y.ProgressBar');
  53. var label = this.get('label');
  54. position = parseFloat(position, 10);
  55. label[position] = value;
  56. this.set('label', label);
  57. return this;
  58. },
  59.  
  60. removeLabelAt: function(position) {
  61. Y.log('removeLabelAt', 'info', 'Y.ProgressBar');
  62. var label = this.get('label');
  63. position = parseFloat(position, 10);
  64. if (label[position] !== undefined && label[position] !== null) {
  65. delete label[position];
  66. }
  67. this.get('label', label);
  68. return this;
  69. },
  70.  
  71. /** protected updaters **/
  72. _updateLabel: function(e) {
  73. Y.log('_updateLabel', 'info', 'Y.ProgressBar');
  74. var attrs = this.getAttrs(),
  75. label = this.get('label'),
  76. progress = this.get('progress'),
  77. keys, labelString, i = -1,
  78. l;
  79.  
  80. if (label[progress] !== undefined && label[progress] !== null) {
  81. labelString = label[progress];
  82. } else {
  83. keys = Y.Object.keys(label);
  84. keys.sort(Y.Array.numericSort);
  85. l = keys.length;
  86. while (++i < l) {
  87. if (keys[i] <= progress) {
  88. labelString = label[keys[i]];
  89. }
  90. }
  91. }
  92.  
  93. attrs.label = labelString || '';
  94. this.get('contentBox').one('.' + this.getClassName('label')).set('text', Y.substitute(this.get('labelTemplate'), attrs));
  95. },
  96.  
  97. _updateBackground: function(e) {
  98. Y.log('_updateBackground', 'info', 'Y.ProgressBar');
  99. var cb = this.get('contentBox'),
  100. position = cb.get('offsetWidth') * this.get('progress') / 100;
  101. Y.log('cd offsetWidth: ' + cb.get('offsetWidth'), 'warn');
  102. Y.log('this.progress: ' + this.get('progress'), 'warn');
  103. Y.log('new position: ' + position, 'warn');
  104.  
  105. if (!this._anim) {
  106. this._makeAnim();
  107. }
  108.  
  109. if (this._anim && this._anim.get('running')) {
  110. this._anim.pause();
  111. }
  112.  
  113. this._anim.set('to.width', position);
  114.  
  115. this._anim.run();
  116.  
  117. this._updateLabel();
  118. },
  119.  
  120. _makeAnim: function() {
  121. Y.log('_makeAnim', 'info', 'Y.ProgressBar');
  122. var animConfig = this.get('animation'),
  123. node = this.get('contentBox').one(this.get('animateNode'));
  124.  
  125. animConfig.node = node;
  126.  
  127. if (!animConfig.to) {
  128. animConfig.to = {
  129. width: 0
  130. };
  131. }
  132. this._anim = new Y.Anim(animConfig);
  133. },
  134.  
  135. _getAnimateNode: function() {
  136. return ('.' + this.getClassName('slider'));
  137. }
  138.  
  139. }, {
  140. ATTRS: {
  141.  
  142. animation: {
  143. value: {
  144. easing: Y.Easing.easeIn,
  145. duration: 0.5
  146. }
  147. },
  148.  
  149. animateNode: {
  150. valueFn: '_getAnimateNode'
  151. },
  152.  
  153. /* REMOVED FOR TRANSITION BUG
  154. transition : {
  155. value : {
  156. easing : 'ease-out',
  157. duration: 0.5
  158. }
  159. },
  160. */
  161.  
  162. labelTemplate: {
  163. value: '{label} - {progress}%'
  164. },
  165.  
  166. label: {
  167. value: {
  168. 0: 'Loading',
  169. 100: 'Complete'
  170. },
  171. validator: function(val) {
  172. return (Y.Lang.isString(val) || Y.Lang.isObject(val));
  173. },
  174. setter: function(val) {
  175. if (Y.Lang.isString(val)) {
  176. val = {
  177. 0: val
  178. };
  179. }
  180. return val;
  181. }
  182. },
  183.  
  184. layout: {
  185. value: '<div class="{sliderClass} {labelClass}"></div>'
  186. },
  187.  
  188. precision: {
  189. value: 2,
  190. setter: function(val) {
  191. return parseInt(val, 10);
  192. }
  193. },
  194.  
  195. progress: {
  196. value: 0,
  197. setter: function(val) {
  198. var precision = Math.pow(10, this.get('precision'));
  199. val = parseFloat(val, 10);
  200. if (val < 0) {
  201. val = 0;
  202. } else if (val > 100) {
  203. val = 100;
  204. }
  205. return Math.round(val * precision) / precision;
  206. }
  207. }
  208. }
  209. });
  210.  
  211.  
  212.  
  213.  
  214.  
  215. }, '@VERSION@' ,{requires:['widget','substitute','anim','base-build']});
Add Comment
Please, Sign In to add comment