component.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. function init_components_dashboard(input){
  2. var data = $.extend({
  3. column: 8,
  4. line: 8,
  5. onMove: 'dashboard_widget_move',
  6. onRemove: 'dashboard_widget_delete',
  7. onResize: 'dashboard_widget_resize',
  8. onConfigure: 'dashboard_widget_configure',
  9. onRefresh: 'dashboard_widget_refresh',
  10. onAdd: 'dashboard_widget_add'
  11. },{
  12. column: input.attr('data-column'),
  13. line: input.attr('data-line'),
  14. scope: input.attr('data-scope'),
  15. uid: input.attr('data-uid'),
  16. id: input.attr('data-id'),
  17. });
  18. component = input.data('component');
  19. if(!component){
  20. component = new Dashboard({
  21. element : input,
  22. columnNumber : data.column,
  23. lineNumber : data.line
  24. });
  25. input.data('component',component);
  26. }
  27. if(data.scope){
  28. $.action({
  29. action: 'dashboard_widget_search',
  30. scope: data.scope,
  31. uid: data.uid
  32. },function(response){
  33. component.addWidgets(response.widgets);
  34. });
  35. }
  36. if(data.onRefresh){
  37. setInterval(function(){
  38. $.action({
  39. action : data.onRefresh,
  40. scope: data.scope,
  41. uid: data.uid
  42. }, function(response){
  43. for(var k in response.rows){
  44. var widget = $.extend(component.widgets[response.rows[k].id],response.rows[k]);
  45. component.widgets[widget.id] = widget;
  46. component.renderContent(widget.id);
  47. }
  48. });
  49. },1000);
  50. }
  51. /*component.addWidgets([{
  52. width : 1,
  53. height: 1,
  54. row: 3,
  55. column: 5,
  56. id: 1,
  57. icon : 'far fa-user',
  58. headerBackground : 'rgb(0, 123, 255)',
  59. label : 'Hello monde',
  60. content : '<p>Wazup ?</p>'
  61. },
  62. {
  63. width : 3,
  64. height: 3,
  65. row: 0,
  66. column: 1,
  67. id: 2,
  68. removable : false,
  69. resizeable : false,
  70. draggable : false,
  71. label : 'ByBy',
  72. content : '<p>Wazup 2 ?</p>'
  73. }]);*/
  74. component.on('placeholder-click',function(placeholder){
  75. $.action({
  76. action : data.onAdd,
  77. scope : data.scope,
  78. uid : data.uid,
  79. row : placeholder.row,
  80. column : placeholder.column
  81. },function(widget){
  82. component.addWidgets([widget]);
  83. });
  84. }).on('move',function(widget){
  85. console.log('Widget has moved', widget);
  86. $.action({
  87. action : data.onMove,
  88. widget : widget
  89. });
  90. }).on('resize',function(widget){
  91. console.log('Widget has resized', widget);
  92. $.action({
  93. action: data.onResize,
  94. widget: widget
  95. });
  96. }).on('delete',function(widget){
  97. if(!confirm('Êtes-vous sûr de vouloir supprimer ce widget?')) return true;
  98. console.log('Widget has removed', widget);
  99. $.action({
  100. action : data.onRemove,
  101. widget : widget
  102. });
  103. }).on('configure',function(id){
  104. $.action({
  105. action : 'dashboard_widget_by_id',
  106. id : id
  107. },function(response){
  108. var widget = $.extend(component.widgets[response.item.id],response.item);
  109. $('#dashboard-configure-menu li').off('click').click(function(){
  110. var li = $(this);
  111. var menu = li.attr('data-menu');
  112. li.addClass('active').parent().find('>li').removeClass('active');
  113. switch(menu){
  114. case "model":
  115. $('#dashboard-configure-content').html($('#configure-model').html());
  116. init_components('#dashboard-configure-content');
  117. var modelList = $('#widget-models');
  118. modelList.fill({
  119. action : 'dashboard_model_search',
  120. },function(response){
  121. $('li',modelList).removeClass('active');
  122. $('li[data-model="'+widget.model+'"]',modelList).addClass('active');
  123. $('li',modelList).click(function(){
  124. $('#widget-models li').removeClass('active');
  125. $(this).addClass('active');
  126. $.action({
  127. action : data.onConfigure,
  128. id : widget.id,
  129. model : $(this).attr('data-model')
  130. },function(response){
  131. });
  132. });
  133. });
  134. break;
  135. case "style":
  136. $('#dashboard-configure-content').html($('#configure-style').html());
  137. $.action({
  138. action : 'dashboard_widget_by_id',
  139. id : widget.id
  140. },function(response){
  141. $('#configure-style-table').fromJson(response.item).off('change').change(function(){
  142. var form = $('#configure-style-table').toJson();
  143. widget.headerBackground = form['widget-header-background'];
  144. widget.bodyBackground = form['widget-body-background'];
  145. widget.bodyColor= form['widget-body-color'];
  146. widget.iconColor = form['widget-header-icon-color'];
  147. widget.titleColor = form['widget-header-title-color'];
  148. widget.icon = form['widget-header-icon'];
  149. var customiser = $('.dashboard-widget-customizer');
  150. $('.dashboard-widget-header',customiser).css('backgroundColor',widget.headerBackground+'!important');
  151. $('.widget-header-title',customiser).css('color',widget.titleColor+'!important');
  152. $('.dashboard-widget-content',customiser).css({
  153. backgroundColor : widget.bodyBackground+'!important',
  154. color : widget.bodyColor+'!important'
  155. });
  156. $('.widget-header-icon i',customiser).css('color',widget.iconColor+'!important');
  157. $('.widget-header-icon i',customiser).attr('class',widget.icon);
  158. component.widgets[widget.id] = widget;
  159. component.renderContent(widget.id);
  160. $.action({
  161. action : data.onConfigure,
  162. id : widget.id,
  163. headerBackground : widget.headerBackground,
  164. bodyBackground : widget.bodyBackground,
  165. bodyColor : widget.bodyColor,
  166. iconColor : widget.iconColor,
  167. titleColor : widget.titleColor,
  168. icon : widget.icon
  169. });
  170. });
  171. });
  172. init_components('#dashboard-configure-content');
  173. break;
  174. }
  175. });
  176. $('#dashboard-configure-menu li[data-menu="model"]').click();
  177. });
  178. });
  179. }