main.js 11 KB

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