widgets_content.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Content widgets
  4. *
  5. * Demo JS code for widgets_content.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var ContentWidgets = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Dropzone file uploader
  15. var _componentDropzone = function() {
  16. if (typeof Dropzone == 'undefined') {
  17. console.warn('Warning - dropzone.min.js is not loaded.');
  18. return;
  19. }
  20. // Configure dropzone
  21. Dropzone.options.dropzoneMultiple = {
  22. paramName: 'file',
  23. dictDefaultMessage: 'Drop file to upload <span>or CLICK</span>',
  24. maxFilesize: 0.1
  25. };
  26. };
  27. // Uniform
  28. var _componentUniform = function() {
  29. if (!$().uniform) {
  30. console.warn('Warning - uniform.min.js is not loaded.');
  31. return;
  32. }
  33. // Initialize
  34. $('.form-input-styled').uniform({
  35. fileButtonClass: 'action btn bg-pink-400',
  36. fileButtonHtml: '<i class="icon-plus2"></i>'
  37. });
  38. };
  39. // Switchery
  40. var _componentSwitchery = function() {
  41. if (typeof Switchery == 'undefined') {
  42. console.warn('Warning - switchery.min.js is not loaded.');
  43. return;
  44. }
  45. // Initialize multiple switches
  46. var elems = Array.prototype.slice.call(document.querySelectorAll('.form-input-switchery'));
  47. elems.forEach(function(html) {
  48. var switchery = new Switchery(html);
  49. });
  50. };
  51. // Select2
  52. var _componentSelect2 = function() {
  53. if (!$().select2) {
  54. console.warn('Warning - select2.min.js is not loaded.');
  55. return;
  56. }
  57. // Initialize
  58. $('.form-control-select2').select2({
  59. minimumResultsForSearch: Infinity
  60. });
  61. };
  62. // Datepicker
  63. var _componentUiDatepicker = function() {
  64. if (!$().datepicker) {
  65. console.warn('Warning - jQuery UI components are not loaded.');
  66. return;
  67. }
  68. // Initialize
  69. $('.form-control-datepicker').datepicker();
  70. };
  71. // Chart
  72. var _chatMessagesStats = function() {
  73. if (typeof d3 == 'undefined') {
  74. console.warn('Warning - d3.min.js is not loaded.');
  75. return;
  76. }
  77. // Main variables
  78. var element = document.getElementById('messages-stats'),
  79. height = 60,
  80. color = '#26A69A';
  81. // Initialize chart only if element exsists in the DOM
  82. if(element) {
  83. // Define main variables
  84. var d3Container = d3.select(element),
  85. margin = {top: 0, right: 0, bottom: 0, left: 0},
  86. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
  87. height = height - margin.top - margin.bottom;
  88. // Date and time format
  89. var parseDate = d3.time.format('%Y-%m-%d').parse;
  90. // Create SVG
  91. // ------------------------------
  92. // Container
  93. var container = d3Container.append('svg');
  94. // SVG element
  95. var svg = container
  96. .attr('width', width + margin.left + margin.right)
  97. .attr('height', height + margin.top + margin.bottom)
  98. .append('g')
  99. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
  100. // Construct chart layout
  101. // ------------------------------
  102. // Area
  103. var area = d3.svg.area()
  104. .x(function(d) { return x(d.date); })
  105. .y0(height)
  106. .y1(function(d) { return y(d.value); })
  107. .interpolate('monotone')
  108. // Construct scales
  109. // ------------------------------
  110. // Horizontal
  111. var x = d3.time.scale().range([0, width ]);
  112. // Vertical
  113. var y = d3.scale.linear().range([height, 0]);
  114. // Load data
  115. // ------------------------------
  116. d3.json('../../../../global_assets/demo_data/dashboard/monthly_sales.json', function (error, data) {
  117. // Show what's wrong if error
  118. if (error) return console.error(error);
  119. // Pull out values
  120. data.forEach(function (d) {
  121. d.date = parseDate(d.date);
  122. d.value = +d.value;
  123. });
  124. // Get the maximum value in the given array
  125. var maxY = d3.max(data, function(d) { return d.value; });
  126. // Reset start data for animation
  127. var startData = data.map(function(datum) {
  128. return {
  129. date: datum.date,
  130. value: 0
  131. };
  132. });
  133. // Set input domains
  134. // ------------------------------
  135. // Horizontal
  136. x.domain(d3.extent(data, function(d, i) { return d.date; }));
  137. // Vertical
  138. y.domain([0, d3.max( data, function(d) { return d.value; })]);
  139. //
  140. // Append chart elements
  141. //
  142. // Add area path
  143. svg.append('path')
  144. .datum(data)
  145. .attr('class', 'd3-area')
  146. .style('fill', color)
  147. .attr('d', area)
  148. .transition() // begin animation
  149. .duration(1000)
  150. .attrTween('d', function() {
  151. var interpolator = d3.interpolateArray(startData, data);
  152. return function (t) {
  153. return area(interpolator (t));
  154. }
  155. });
  156. // Resize chart
  157. // ------------------------------
  158. // Call function on window resize
  159. $(window).on('resize', messagesAreaResize);
  160. // Call function on sidebar width change
  161. $(document).on('click', '.sidebar-control', messagesAreaResize);
  162. // Resize function
  163. //
  164. // Since D3 doesn't support SVG resize by default,
  165. // we need to manually specify parts of the graph that need to
  166. // be updated on window resize
  167. function messagesAreaResize() {
  168. // Layout variables
  169. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
  170. // Layout
  171. // -------------------------
  172. // Main svg width
  173. container.attr("width", width + margin.left + margin.right);
  174. // Width of appended group
  175. svg.attr("width", width + margin.left + margin.right);
  176. // Horizontal range
  177. x.range([0, width]);
  178. // Chart elements
  179. // -------------------------
  180. // Area path
  181. svg.selectAll('.d3-area').datum( data ).attr("d", area);
  182. }
  183. });
  184. }
  185. };
  186. //
  187. // Return objects assigned to module
  188. //
  189. return {
  190. init: function() {
  191. _componentUniform();
  192. _componentSwitchery();
  193. _componentSelect2();
  194. _componentUiDatepicker();
  195. _chatMessagesStats();
  196. },
  197. initDropzone: function() {
  198. _componentDropzone();
  199. }
  200. }
  201. }();
  202. // Initialize module
  203. // ------------------------------
  204. // When content loaded
  205. document.addEventListener('DOMContentLoaded', function() {
  206. ContentWidgets.init();
  207. });
  208. // Dropzone has specific condition
  209. ContentWidgets.initDropzone();