main.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //handle target
  2. var target = false;
  3. var refreshInterval = null;
  4. //CHARGEMENT DE LA PAGE
  5. $(function(){
  6. if($.page()!='index' || $.urlParam('module')!=null) return;
  7. dashboard_dashboardwidget_search();
  8. $('#dashboardView li').click(function(){
  9. $('#dashboardView li').removeAttr('data-selected');
  10. $(this).attr('data-selected',1);
  11. dashboard_dashboardwidget_search();
  12. });
  13. $('#widgetList').change(function(){
  14. var option = $('#widgetList option:selected').data();
  15. option.text = $('#widgetList option:selected').text();
  16. $('.widgetDescription').removeClass('hidden');
  17. $('.widgetDescription h1 span').text(option.text);
  18. $('.widgetDescription h1 i').attr('class',option.icon);
  19. $('.widgetDescription p').text(option.description);
  20. $('.widgetDescription .widgetColor small').css('background-color',option.background);
  21. $('.widgetDescription .widgetColor span').text(option.background);
  22. });
  23. $('#dashboard').sortable({
  24. update:function(event,ui){
  25. var data = dashboard_dashboardwidget_save_position();
  26. }
  27. });
  28. $( ".widget" ).disableSelection();
  29. });
  30. function init_setting_dashboard(){
  31. dashboard_dashboard_search();
  32. }
  33. //Enregistrement des configurations
  34. function dashboard_setting_save(){
  35. $.action({
  36. action : 'dashboard_setting_save',
  37. fields : $('#dashboard-setting-form').toJson()
  38. },function(){ $.message('info','Configuration enregistrée'); });
  39. }
  40. /** DASHBOARD **/
  41. //Récuperation d'une liste de dashboard dans le tableau #dashboards
  42. function dashboard_dashboard_search(callback){
  43. $('#dashboards').fill({
  44. action:'dashboard_dashboard_search'
  45. },function(){
  46. if(callback!=null) callback();
  47. });
  48. }
  49. //Ajout ou modification d'élément dashboard
  50. function dashboard_dashboard_save(){
  51. var data = $('#dashboard-form').toJson();
  52. $.action(data,function(r){
  53. $.message('success','Enregistré');
  54. dashboard_dashboard_search();
  55. $('#dashboard-form').attr('data-id','');
  56. });
  57. }
  58. //Récuperation ou edition d'élément dashboard
  59. function dashboard_dashboard_edit(element){
  60. var line = $(element).closest('tr');
  61. $.action({action:'dashboard_dashboard_edit',id:line.attr('data-id')},function(r){
  62. $.setForm('#dashboard-form',r);
  63. $('#dashboard-form').attr('data-id',r.id);
  64. });
  65. }
  66. //Suppression d'élement dashboard
  67. function dashboard_dashboard_delete(element){
  68. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  69. var line = $(element).closest('tr');
  70. $.action({
  71. action : 'dashboard_dashboard_delete',
  72. id : line.attr('data-id')
  73. },function(r){
  74. line.remove();
  75. $.message('info','Élement supprimé');
  76. });
  77. }
  78. /** DASHBOARDWIDGET **/
  79. //Récuperation d'une liste de dashboardwidget dans le tableau #dashboardwidgets
  80. function dashboard_dashboardwidget_search(callback){
  81. var dashboard = $('#dashboardView li[data-selected]').attr('data-id');
  82. if(!dashboard || dashboard=='') return;
  83. $.action({
  84. action : 'dashboard_dashboardwidget_search',
  85. dashboard : dashboard
  86. },function(r){
  87. $('#dashboard .widget:visible').remove();
  88. for(var i in r.rows){
  89. var widget = r.rows[i];
  90. dashboard_dashboardwidget_append(widget);
  91. dashboard_dashboardwidget_load(widget);
  92. }
  93. clearInterval(refreshInterval);
  94. refreshInterval = setInterval(function(){
  95. $.action({
  96. action : 'dashboard_dashboardwidget_refresh',
  97. dashboard : $('#dashboardView li[data-selected]').attr('data-id')
  98. },function(r){
  99. for(var id in r.rows){
  100. var widget = r.rows[id];
  101. if(widget.widget){
  102. var header = $('.widget[data-id="'+id+'"]').find('.widget_header');
  103. if(widget.widget.title) header.find('span').text(widget.widget.title);
  104. if(widget.widget.icon) header.find('i').attr('class','fa '+widget.widget.icon);
  105. if(widget.widget.background) header.css('backgroundColor',widget.widget.background);
  106. }
  107. if(widget.callback){
  108. if(window[widget.callback]!=null) window[widget.callback]($('.widget[data-id="'+id+'"]'),widget.data);
  109. }
  110. }
  111. });
  112. },3000);
  113. });
  114. }
  115. //Mise à jour des infos d'un élement widget à partir d'un object data
  116. function dashboard_dashboardwidget_render(widget,data){
  117. widget.attr('data-id',data.id);
  118. widget.removeClass (function (index, css) {
  119. return (css.match (/(^|\s)col-md-\S+/g) || []).join(' ');
  120. });
  121. widget.attr('data-id',data.id)
  122. .attr('data-load',data.load)
  123. .attr('data-configure',data.configure)
  124. .attr('data-delete',data.delete)
  125. .addClass('col-md-'+data.width)
  126. .find('.widget_header')
  127. .css('background',data.background)
  128. .find('i:eq(0)').attr('class','fa '+data.icon);
  129. widget.find('.widget_header span:eq(0)').html(data.title);
  130. widget.find('.widget_content').html(data.content);
  131. var options = '';
  132. for(var k in data.options){
  133. var option = data.options[k];
  134. options+='<li onclick="'+option.function+'"><i class="fa '+option.icon+'"></i> '+option.label+'</li>';
  135. }
  136. if(data.configure) options+="<li title='Configurer' onclick='configure_widget(this);'><i class='fa fa-wrench'></i></li>";
  137. options+="<li title='Supprimer' onclick='dashboard_dashboardwidget_delete(this);'><i class='fa fa-times'></i></li>";
  138. widget.find('.widget_options').html(options);
  139. widget.data('data',data);
  140. widget.show();
  141. return widget;
  142. }
  143. //Modification d'un widget existant
  144. function dashboard_dashboardwidget_update(data){
  145. var widget = $('.widget[data-id="'+data.id+'"]');
  146. var data = $.extend(widget.data('data'), data);
  147. dashboard_dashboardwidget_render(widget,data);
  148. }
  149. //Ajout ou modification d'élément dashboardwidget
  150. function dashboard_dashboardwidget_save(){
  151. var data = $('#dashboardwidget-form').toJson();
  152. $.action(data,function(r){
  153. $('#dashboardwidget-form').attr('data-id','');
  154. dashboard_dashboardwidget_search();
  155. $.message('success','Enregistré');
  156. });
  157. }
  158. //Chargement du contenu php du widget
  159. function dashboard_dashboardwidget_load(widget){
  160. $.getJSON(widget.load,$.extend(widget,{content:''}),function(r){
  161. dashboard_dashboardwidget_update(r);
  162. var data = $.extend($('.widget[data-id="'+widget.id+'"]').data('data'), r.widget);
  163. var init = 'widget_'+widget.model+'_init';
  164. if(window[init]!=null) window[init]();
  165. });
  166. }
  167. //Ajout (manuel par l'user) d'un widget
  168. function dashboard_dashboardwidget_add(){
  169. $.action({
  170. action : 'dashboard_dashboardwidget_add',
  171. dashboard : $('#dashboardView li[data-selected]').attr('data-id'),
  172. widget : $('#widgetList').val()
  173. },function(r){
  174. if(r.message) $.message('info',r.message);
  175. $('#dashboard_dashboardwidget_appendModal').modal('hide');
  176. dashboard_dashboardwidget_search();
  177. });
  178. }
  179. //Ajout (depuis le code) d'un widget
  180. function dashboard_dashboardwidget_append(data){
  181. var tpl = $('#dashboard .widget:hidden').get(0).outerHTML;
  182. var widget = $(tpl);
  183. $('#dashboard').append(widget);
  184. if(data.js!=null){
  185. for(k in data.js){
  186. var js = data.js[k];
  187. if($('script[src="'+js+'"]').length!=0) continue;
  188. var jsFile= document.createElement('script');
  189. jsFile.setAttribute("type","text/javascript");
  190. jsFile.setAttribute("src", js);
  191. document.getElementsByTagName("body")[0].appendChild(jsFile);
  192. }
  193. }
  194. if(data.css!=null){
  195. for(k in data.css){
  196. var css = data.css[k];
  197. if($('script[src="'+css+'"]').length!=0) continue;
  198. var cssFile= document.createElement('link');
  199. cssFile.setAttribute("rel","stylesheet");
  200. cssFile.setAttribute("type","text/css");
  201. cssFile.setAttribute("href", css);
  202. document.getElementsByTagName("body")[0].appendChild(cssFile);
  203. }
  204. }
  205. dashboard_dashboardwidget_render(widget,data);
  206. }
  207. //Récuperation ou edition d'élément dashboardwidget
  208. function dashboard_dashboardwidget_edit(widget){
  209. var line = $(element).closest('tr');
  210. $.action({action:'dashboard_dashboardwidget_edit',id:line.attr('data-id')},function(r){
  211. $.setForm('#dashboardwidget-form',r);
  212. $('#dashboardwidget-form').attr('data-id',r.id);
  213. });
  214. }
  215. //Suppression d'élement dashboardwidget
  216. function dashboard_dashboardwidget_delete(element){
  217. var element = $(element).closest('.widget');
  218. var data = element.data('data');
  219. $.action({
  220. action : 'dashboard_dashboardwidget_delete',
  221. dashboard : $('#dashboardView li[data-selected]').attr('data-id'),
  222. widget : data.id,
  223. },function(r){
  224. element.remove();
  225. if(r.message) $.message('info',r.message);
  226. if(data.delete != null){
  227. $.getJSON(data.delete,$.extend(data,{content:''}));
  228. }
  229. });
  230. }
  231. //Enregistrement de toutes les positions de widget
  232. function dashboard_dashboardwidget_save_position(){
  233. var positions = [];
  234. $('.widget:visible').each(function(i,element){
  235. positions.push({id:$(element).attr('data-id'),position:$(element).index()});
  236. });
  237. $.action({
  238. action : 'dashboard_dashboardwidget_save_position',
  239. dashboard : $('#dashboardView li[data-selected]').attr('data-id'),
  240. positions : positions,
  241. },function(r){
  242. });
  243. }