jquery.dashboard.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. Jquery.dashboard V1.0
  3. @author V.carruesco aka idleman
  4. @mail idleman@idleman.fr
  5. @licence CC-by-nc-sa
  6. */
  7. var dashboard_handle = null;
  8. var dashboard_bloc = null;
  9. var clickpoint = null;
  10. var dashboard_handle_x = null;
  11. var dashboard_handle_y = null;
  12. (function($){
  13. $.dashboard = {
  14. addBloc : function(o) {
  15. //On créé le bloc avec un modèle par défaut
  16. var bloc = $('<div class="dashboard_bloc '+(o.minified==1?'dashboard_bloc_minified"':'')+'"> \
  17. <div class="dashboard_bloc_head">\
  18. <label> \
  19. <i class="fa fa-caret-right"></i> \
  20. <span>'+o.title+'</span> \
  21. </label> \
  22. <ul> \
  23. <li class="dashboard_minimize_button"> \
  24. <i class="fa fa-sort"></i> \
  25. </li> \
  26. <li class="dashboard_delete_button"> \
  27. <i class="fa fa-times"></i> \
  28. </li> \
  29. </ul> \
  30. </div> \
  31. <div class="dashboard_bloc_content">'+o.content+'</div> \
  32. </div>');
  33. var placement = $(o.placement).parent();
  34. if(placement.length==0) return;
  35. var column = placement.attr('id').split('_');
  36. column = column[column.length-1];
  37. var cell = bloc.index();
  38. if(o.widget!=null)bloc.attr('data-uid',o.widget.model);
  39. if(o.widget!=null)bloc.attr('data-id',o.widget.id);
  40. bloc.data('widget',{column: column,cell:cell});
  41. //on place le bloc
  42. if(o.placement!=null){
  43. $(o.placement).before(bloc);
  44. //Si le model est définit, on l'applique au bloc
  45. if(o.model!=null){
  46. $.dashboard.setBlocModel(bloc,o.model);
  47. //si on charge un module existant
  48. if(o.model.onLoad!=null) $.dashboard.loadBlocData(bloc,o.model.onLoad,o);
  49. }
  50. }
  51. },
  52. setBlocModel : function(bloc,model) {
  53. bloc.data('widget',model);
  54. $.dashboard.setBlocData(bloc,model);
  55. if(model.addToHead != null) bloc.find('.dashboard_bloc_head ul').prepend(model.addToHead);
  56. if(model.onEdit != null) bloc.find('.dashboard_bloc_head ul').prepend('<li class="dashboard_setting_button"><i class="fa fa-wrench"></i></li>');
  57. },
  58. loadBlocData : function(bloc,url,data){
  59. //Affichage du chargement
  60. $.dashboard.setBlocData(bloc,{title:"Chargement",content:"<div class='dashboard_loading'></div>"});
  61. //Chargement des donnees du bloc
  62. $.ajax({
  63. url: url,
  64. data : data,
  65. id : data.id,
  66. success:function(response){
  67. response.id = data.id;
  68. $.dashboard.setBlocData(bloc,response);
  69. },
  70. error:function(response){
  71. //Si la requete ne repond pas correctement, on affiche le message d'erreur
  72. $.dashboard.setBlocData(bloc,{title:"ERREUR",content:response.responseText});
  73. }
  74. });
  75. },
  76. setBlocData : function(bloc,data){
  77. var widget = bloc.data('widget');
  78. widget.id = data.id;
  79. bloc.data('widget',widget);
  80. if(data.id!=null) bloc.attr('id','dashboard_bloc_'+data.id);
  81. if(data.title!=null) bloc.find('.dashboard_bloc_head label span').html(data.title);
  82. if(data.content!=null) bloc.find('.dashboard_bloc_content').html(data.content);
  83. if(data.background!=null) bloc.find('.dashboard_bloc_head').css('background-color',data.background);
  84. if(data.color!=null) bloc.find('.dashboard_bloc_head').css('color',data.color);
  85. if(data.icon!=null) bloc.find('.dashboard_bloc_head label i').removeAttr('class').addClass(data.icon);
  86. if(data.minified!=null){
  87. if(data.minified==0){
  88. bloc.addClass('dashboard_bloc_minified');
  89. }else{
  90. bloc.removeClass('dashboard_bloc_minified');
  91. }
  92. }
  93. }
  94. };
  95. $.fn.extend({
  96. dashboard: function (options){
  97. var defaults = {
  98. column: 3,
  99. models: [],
  100. data : []
  101. }
  102. var o = $.extend(defaults, options);
  103. return this.each(function() {
  104. var obj = $(this);
  105. var columnWidth = (100/o.column) -2;
  106. obj.html('');
  107. $('.dashboard_placement').off( "click" );
  108. $(document).off('mousedown');
  109. $(document).off('mousemove');
  110. $(document).off('change');
  111. $(document).off('mousedown');
  112. $(document).off('mouseup');
  113. $('.dashboard_widget_picker').off('change');
  114. $('.dashboard_delete_button,.dashboard_bloc_head,.dashboard_setting_cancel_button,.dashboard_setting_save_button,.dashboard_setting_button,.dashboard_minimize_button').off('mousedown');
  115. /********************/
  116. /** INIT STRUCTURE **/
  117. /********************/
  118. for(i=0;i<o.column;i++){
  119. obj.append('<div style="min-width:300px;width:'+columnWidth+'%;" id="dashboard_column_'+i+'" class="dashboard_column"><div data-column="'+i+'" data-cell="0" class="dashboard_placement"></div></div>');
  120. }
  121. /***************/
  122. /** INIT DATA **/
  123. /***************/
  124. for(var key in o.data){
  125. var widget = o.data[key];
  126. var model = '';
  127. for(var key in o.models){
  128. if(o.models[key].uid == widget.model) model = o.models[key];
  129. }
  130. $.dashboard.addBloc({
  131. id : widget.id,
  132. minified : widget.minified,
  133. placement : '#dashboard_column_'+widget.column+' .dashboard_placement:eq(0)',
  134. model : model,
  135. widget : widget
  136. });
  137. if(o.onLoad!=null)
  138. o.onLoad(model,widget);
  139. }
  140. /*****************/
  141. /** CREATE BLOC **/
  142. /*****************/
  143. //Etape 1 : choix du modele de widget
  144. $('.dashboard_placement').on('click',function(){
  145. var content = '<select class="dashboard_widget_picker" id="dashboard_widget_picker"><option value=""> - </option>';
  146. for(var key in o.models){
  147. var widget = o.models[key];
  148. content += '<option value="'+key+'">'+widget.label+'</option>';
  149. }
  150. content += '</select>';
  151. $.dashboard.addBloc({
  152. title :'Choisissez un widget',
  153. content : content,
  154. placement : this
  155. });
  156. });
  157. //Etape 2 : creation du widget
  158. $(document).on('change','.dashboard_widget_picker',function(){
  159. var bloc = $(this).parent().parent();
  160. var widget = bloc.data('widget');
  161. var model = o.models[$(this).val()];
  162. $.dashboard.setBlocModel(bloc,model);
  163. if(o.onCreate!=null)
  164. o.onCreate(model,bloc,widget.column,widget.cell);
  165. });
  166. /*****************/
  167. /** DRAG & DROP **/
  168. /*****************/
  169. $( ".dashboard_column" ).sortable({
  170. connectWith: ".dashboard_column",
  171. handle: ".dashboard_bloc_head",
  172. placeholder: "dashboard_place_holder",
  173. update: function( event, ui ){
  174. var sort = {};
  175. sort.column = $('.dashboard_column').index(this);
  176. sort.cell = ui.item.index();
  177. var cells = [];
  178. $('.dashboard_column').each(function(i,column){
  179. $('.dashboard_bloc',column).each(function(i,bloc){
  180. var id = $(bloc).attr('id').replace('dashboard_bloc_','');
  181. cells[id] = {cell : $(bloc).index(),column: $('.dashboard_column').index(column)}
  182. });
  183. });
  184. sort.cells = cells;
  185. if(o.onMove!=null && sort.column!=null) o.onMove(ui.item.data('widget'),sort);
  186. }
  187. });
  188. /*************/
  189. /** Setting **/
  190. /*************/
  191. $(document).on('mousedown','.dashboard_setting_button',function(e){
  192. var bloc = $(this).parent().parent().parent();
  193. var widget = bloc.data('widget');
  194. widget.id = bloc.attr('id').replace('dashboard_bloc_','');
  195. bloc.data('widget',widget);
  196. bloc.find('.dashboard_bloc_content').load(widget.onEdit,{id:widget.id},function(){
  197. bloc.find('.dashboard_bloc_content').append('<div class="dashboard_setting_form_options"></div>');
  198. var options = bloc.find('.dashboard_bloc_content .dashboard_setting_form_options');
  199. if(widget.onSave!=null)
  200. options.append('<button class="dashboard_setting_save_button">Enregistrer</button> ');
  201. options.append('<button class="dashboard_setting_cancel_button">Annuler</button>');
  202. });
  203. e.preventDefault();
  204. e.stopPropagation();
  205. });
  206. $(document).on('mousedown','.dashboard_setting_save_button',function(e){
  207. var bloc = $(this).parent().parent().parent();
  208. var widget = bloc.data('widget');
  209. data = {id : widget.id};
  210. bloc.find('.dashboard_bloc_content').find('input,select,textarea').each(function(i,input){
  211. if(input.id!=null){
  212. var inp = $(input);
  213. if(inp.attr('type') == 'checkbox'){
  214. data[input.id] = inp.is(':checked')?1:0;
  215. }else{
  216. data[input.id] = $(input).val();
  217. }
  218. }
  219. });
  220. $.ajax({
  221. url : widget.onSave,
  222. data : data,
  223. complete : function(){
  224. $.dashboard.loadBlocData(bloc,widget.onLoad,{id:widget.id});
  225. }
  226. });
  227. e.preventDefault();
  228. e.stopPropagation();
  229. });
  230. $(document).on('mousedown','.dashboard_setting_cancel_button',function(e){
  231. var bloc = $(this).parent().parent().parent();
  232. var widget = bloc.data('widget');
  233. $.dashboard.loadBlocData(bloc,widget.onLoad,{id:widget.id});
  234. e.preventDefault();
  235. e.stopPropagation();
  236. });
  237. $(document).on('mousedown','.dashboard_delete_button',function(e){
  238. var bloc = $(this).parent().parent().parent();
  239. var widget = bloc.data('widget');
  240. bloc.remove();
  241. if(o.onDelete!=null) o.onDelete(widget,bloc);
  242. e.preventDefault();
  243. e.stopPropagation();
  244. });
  245. $(document).on('mousedown','.dashboard_minimize_button',function(e){
  246. var bloc = $(this).parent().parent().parent();
  247. var widget = bloc.data('widget');
  248. var content = bloc.find('.dashboard_bloc_content');
  249. if(content.is(':visible')){
  250. bloc.find('.dashboard_bloc_content').slideUp(150);
  251. bloc.addClass('dashboard_bloc_minified');
  252. if(o.onMinimize!=null) o.onMinimize(widget);
  253. }else{
  254. bloc.find('.dashboard_bloc_content').slideDown(150);
  255. bloc.removeClass('dashboard_bloc_minified');
  256. if(o.onMaximize!=null) o.onMaximize(widget);
  257. }
  258. e.preventDefault();
  259. e.stopPropagation();
  260. });
  261. });
  262. },
  263. });
  264. })(jQuery);
  265. function collision(div1, div2) {
  266. var x1 = div1.offset().left;
  267. var y1 = div1.offset().top;
  268. var h1 = div1.outerHeight(true);
  269. var w1 = div1.outerWidth(true);
  270. var b1 = y1 + h1;
  271. var r1 = x1 + w1;
  272. var x2 = div2.offset().left;
  273. var y2 = div2.offset().top;
  274. var h2 = div2.outerHeight(true);
  275. var w2 = div2.outerWidth(true);
  276. var b2 = y2 + h2;
  277. var r2 = x2 + w2;
  278. if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
  279. return true;
  280. }