main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. $(document).ready(function(){
  2. if($.urlParam('embeded') =="1"){
  3. $('.navbar,.resources-options,#sketch').hide();
  4. $('.container-fluid,.col-md-9,.col-md-3').css('padding','0');
  5. $('.list-group-item,#resource').css('padding','3px').css('border-radius','0px');
  6. $('.jumbotron').css('border-radius','0');
  7. $('.row').css('margin','0');
  8. $('#resources').before('<div style="width:100%;border-radius:0px;" onclick="'+$('#download').attr('onclick')+'" class="btn btn-success" ><i class="fa fa-arrow-circle-o-down"></i> Télécharger</div>');
  9. $('.col-md-3,.col-md-9').attr('style','width:20%;float:left;padding:0;');
  10. $('.col-md-9').attr('style','width:70%;float:left;padding:0;');
  11. }
  12. if($.urlParam('sidebar') =="0"){
  13. $('#resourceMenu').hide();
  14. $('#resourceContent').removeClass('col-md-9').addClass('col-md-12');
  15. }
  16. var init = 'init_'+$.page();
  17. if(window[init]!=null) window[init]();
  18. if($.page()=='' ) init_index();
  19. Dropzone.autoDiscover = false;
  20. });
  21. function init_index(){
  22. search_sketch();
  23. $('#label').enter(function(){
  24. create_sketch();
  25. });
  26. $('#importJsonSketch').dropzone({
  27. url : 'action.php?action=import_sketch',
  28. complete : function(useless){
  29. if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
  30. search_sketch();
  31. }
  32. },
  33. success: function(file, response){
  34. if(response.errors.length>0) alert(response.errors.join(','));
  35. },
  36. sending : function(file, xhr, formData){
  37. formData.append('from','file');
  38. }
  39. });
  40. }
  41. //COMPONENT
  42. function init_component(){
  43. search_component();
  44. $('#imageUpload').dropzone({
  45. url : 'action.php?action=upload_component_image',
  46. success : function(useless,server){
  47. $('#imageUpload').attr('src',server);
  48. },
  49. sending : function(file, xhr, formData){
  50. //formData.append('id', $('#sketch').attr('data-id'));
  51. }
  52. });
  53. $('#label').autocomplete({
  54. source : 'http://hack.idleman.fr/action.php?action=find_component',
  55. minLength: 4,
  56. select: function( event, ui ) {
  57. $.setForm('#editComponent',ui.item.value);
  58. $('#imageUpload').attr('src',ui.item.value.image);
  59. return false;
  60. }
  61. }).data("uiAutocomplete")._renderItem = function (ul, item) {
  62. return $("<li />")
  63. .data("item.autocomplete", item)
  64. .append("<a><img style='height:50px;width:auto;' src='" + item.value.image + "' /> " + item.label + "</a>")
  65. .appendTo(ul);
  66. };
  67. };
  68. function save_component(){
  69. var data = $.getForm('#editComponent');
  70. if($('#imageUpload').attr('src').substring(0,10)=='data:image')
  71. data.image = $('#imageUpload').attr('src');
  72. $.action(data,function(r){
  73. search_component();
  74. });
  75. }
  76. function search_component(){
  77. $('#components').fill({action:'search_component'});
  78. }
  79. function edit_component(element){
  80. var data = {action : 'edit_component'};
  81. $('#editComponent input').val('');
  82. $('#imageUpload').attr('src','img/default_image.png');
  83. if(element!=null){
  84. var line = $(element).closest('tr');
  85. data.id = line.attr('data-id');
  86. }
  87. $.action(data,function(r){
  88. $('#editComponent').modal('show');
  89. $.setForm('#editComponent',r);
  90. $('#imageUpload').attr('src',r.image);
  91. $('#editComponent').attr('data-id',r.id);
  92. });
  93. }
  94. function delete_component(element){
  95. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  96. var line = $(element).closest('tr');
  97. $.action({action : 'delete_component',id : line.attr('data-id')},function(r){
  98. line.remove();
  99. });
  100. }
  101. //SKETCH
  102. function init_sketch(){
  103. search_resources(function(){
  104. var resource = $.hashData('resource');
  105. resource = resource == '' ? $('#resources a:visible():eq(1)').attr('data-id') : resource;
  106. select_resource(resource);
  107. });
  108. $('#importResource i').dropzone({
  109. url : 'action.php?action=import_resource',
  110. complete : function(useless,server){
  111. if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
  112. search_resources();
  113. }
  114. },
  115. sending : function(file, xhr, formData){
  116. formData.append('id', $('#sketch').attr('data-id'));
  117. }
  118. });
  119. }
  120. function search_sketch(){
  121. $('#sketchs').fill({action:'search_sketch'});
  122. }
  123. function create_sketch(){
  124. $.action($.getForm('#editSketch'),function(r){
  125. window.location = 'sketch.php?id='+r.id;
  126. });
  127. }
  128. function import_sketch(){
  129. var data = $.getForm('#importSketch');
  130. data.from = 'url';
  131. $.action(data,function(){
  132. search_sketch();
  133. });
  134. }
  135. function delete_sketch(element){
  136. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  137. var line = $(element).closest('tr');
  138. $.action({action : 'delete_sketch',id : line.attr('data-id')},function(r){
  139. line.remove();
  140. });
  141. }
  142. function save_sketch_title(element){
  143. $.action({action : 'save_sketch_title',label : $(element).val(),id:$('#sketch').attr('data-id')});
  144. }
  145. function toggle_share_sketch(element){
  146. var button = $(element).find('i');
  147. var nextState = !button.hasClass('fa-eye');
  148. $.action({action:'toggle_share_sketch',state:(nextState?1:0),id:$('#sketch').attr('data-id')},function(){
  149. button.removeClass('fa-eye').removeClass('fa-eye-slash');
  150. button.addClass((nextState?'fa-eye':'fa-eye-slash'));
  151. $(element).attr('title','Rendre '+(nextState?'Privé':'Public'));
  152. });
  153. }
  154. function toggle_embed_sketch(){
  155. $('#embedModal').modal('show');
  156. get_embeded_code();
  157. }
  158. function get_embeded_code(){
  159. var url = window.location.protocol+'//'+window.location.host+window.location.pathname+window.location.search;
  160. url+= '&embeded=1';
  161. if(!$('#enableSideBar').prop('checked'))
  162. url+= '&sidebar=0';
  163. url+= window.location.hash;
  164. var html = '<a href="'+window.location+'"><small>Voir en taille réelle<small></a><br/>';
  165. html += '<iframe frameborder="0" width="100%" align="center" height="400px" src="'+url+'"></iframe>';
  166. $('#embedModal textarea').val(html);
  167. $("#embedModal textarea").focus(function() {
  168. var $this = $(this);
  169. $this.select();
  170. $this.mouseup(function() {
  171. $this.unbind("mouseup");
  172. return false;
  173. });
  174. });
  175. }
  176. //RESOURCE
  177. function add_resource(selected){
  178. $('#resources a').removeClass('active');
  179. $('#editResourceMeta').modal('show');
  180. if(selected!=null) $('#editResourceMeta select').val(selected);
  181. $('#label').val('').focus();
  182. $('#resource').attr('data-id','');
  183. }
  184. function save_resource(){
  185. var data = $.getForm('#editResourceMeta');
  186. data.sketch = $('#sketch').attr('data-id');
  187. $.action(data,function(r){
  188. $('#editResourceMeta input').val('');
  189. $('#editResourceMeta').attr('data-id','');
  190. search_resources(select_resource(r.id));
  191. });
  192. }
  193. function search_resources(callback){
  194. var id = $('#sketch').attr('data-id');
  195. $('#resources').fill({id:id,action:'search_resources'},function(){
  196. if(callback!=null) callback();
  197. });
  198. }
  199. function edit_resource(element,event){
  200. event.stopPropagation();
  201. var line = $(element).closest('a');
  202. $.action({action:'edit_resource',id:line.attr('data-id')},function(r){
  203. $('#editResourceMeta').modal('show');
  204. $.setForm('#editResourceMeta',r);
  205. $('#editResourceMeta').attr('data-id',r.id);
  206. });
  207. }
  208. function delete_resource(element,event){
  209. event.stopPropagation();
  210. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  211. var line = $(element).closest('a');
  212. $.action({action : 'delete_resource',id : line.attr('data-id')},function(r){
  213. line.remove();
  214. });
  215. }
  216. function select_resource(id){
  217. $.hashData({resource:id});
  218. load_resource();
  219. }
  220. function load_resource(){
  221. var id = $.hashData('resource');
  222. var line = $('[data-id="'+id+'"]');
  223. if(line.attr('data-id')==null) return;
  224. $('.preloader').show();
  225. $('#resource p').html('');
  226. $.action({action:'edit_resource',id:line.attr('data-id')},function(r){
  227. $('.preloader').hide();
  228. $('#resources a').removeClass('active');
  229. line.addClass('active');
  230. $('#resource').attr('data-id',r.id);
  231. $('#resource h2').html(r.label);
  232. $('#resource p').html(r.content);
  233. $('#resource textarea:eq(0)').focus();
  234. if(r.upload !=null){
  235. var data = {};
  236. data.url = r.upload.url;
  237. data.success = function(useless,r){
  238. if(r.errors.length!=0){
  239. alert('Erreur : '+r.errors.join(','));
  240. }else{
  241. eval(r.upload.callback);
  242. }
  243. }
  244. data.sending = function(file, xhr, formData){
  245. formData.append('id', $('#resource').attr('data-id'));
  246. }
  247. data.createImageThumbnails = false;
  248. $(r.upload.element).dropzone(data);
  249. }
  250. if(r.code != null){
  251. var editor = CodeMirror.fromTextArea($('#resource p textarea').get(0),r.code);
  252. editor.on("change", function() {
  253. var data ={height:800};
  254. var wrap = editor.getWrapperElement();
  255. var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
  256. if (wrap.style.height != approp) {
  257. wrap.style.height = approp;
  258. editor.refresh();
  259. }
  260. });
  261. editor.on("blur", function(cm,obj){
  262. var data = {};
  263. data.content = cm.getValue();
  264. data.action='save_resource_content';
  265. data.id = $('#resource').attr('data-id');
  266. $.action(data,function(r){
  267. });
  268. });
  269. }
  270. if(r.callback !=null){
  271. eval(r.callback);
  272. }
  273. });
  274. }
  275. /*FILE*/
  276. function init_file(){
  277. search_file();
  278. };
  279. function search_file(){
  280. $('#files').fill({action:'search_file',id:$('#resource').attr('data-id')});
  281. }
  282. function delete_file(element){
  283. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  284. var line = $(element).closest('tr');
  285. $.action({action : 'delete_file',resource : $('#resource').attr('data-id'),id : line.attr('data-id')},function(r){
  286. line.remove();
  287. });
  288. }
  289. /*PART*/
  290. function init_part(){
  291. $('#label').autocomplete({
  292. source : 'action.php?action=autocomplete_part',
  293. minLength: 2,
  294. select: function( event, ui ) {
  295. save_part(ui.item.value);
  296. return false;
  297. }
  298. }).data("uiAutocomplete")._renderItem = function (ul, item) {
  299. return $("<li />")
  300. .data("item.autocomplete", item)
  301. .append("<a><img style='height:50px;width:auto; float:left;' src='" + item.value.image + "' /> " + item.label + " <br/></a><small>"+item.value.brand+"</small>")
  302. .appendTo(ul);
  303. };
  304. search_part();
  305. };
  306. function search_part(){
  307. $('#parts').fill({action:'search_part',id:$('#resource').attr('data-id')});
  308. }
  309. function save_part(model){
  310. var data = $.getForm('#partForm');
  311. if (model!=null) data.model = model.id;
  312. data.resource = $('#resource').attr('data-id');
  313. $.action(data,function(r){
  314. search_part();
  315. clear_part();
  316. });
  317. }
  318. function clear_part(){
  319. $('#partForm input').val('');
  320. $('#partForm').attr('data-id','');
  321. }
  322. /*
  323. function edit_part(element){
  324. var line = $(element).closest('tr');
  325. $.action({action:'edit_part',id:line.attr('data-id')},function(r){
  326. $.setForm('#partForm',r);
  327. $('#partForm').attr('data-id',r.id);
  328. });
  329. }
  330. */
  331. function delete_part(element){
  332. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  333. var line = $(element).closest('tr');
  334. $.action({action : 'delete_part',id : line.attr('data-id')},function(r){
  335. line.remove();
  336. });
  337. }