materio_personalnotes.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // @codekit-prepend "gui.js"
  2. // @koala-prepend "gui.js"
  3. (function($) {
  4. MaterioPersonalNotes = function(){
  5. /**
  6. * init()
  7. */
  8. function init(){
  9. trace('MaterioPersonalNotes :: init');
  10. $(document)
  11. .bind('resultscompleted resultschanged previewloaded', onResultsUpdated);
  12. // .bind('init-scroller-pager', onInitScrollerPager)
  13. // .bind('load-scroller-pager', onLoadScrollerPager)
  14. // .bind('view-mode-changed', onViewModeChanged)
  15. // .bind('history-state-change', onHistoryStateChange);
  16. };
  17. function onResultsUpdated(event){
  18. trace('MaterioPersonalNotes :: onResultsUpdated', event);
  19. ajaxifyLinks(event.container);
  20. };
  21. function ajaxifyLinks(container){
  22. trace('MaterioPersonalNotes :: ajaxifyLinks', container);
  23. container = ((container != null) ? container : 'body');
  24. // trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink);
  25. // if (typeof Drupal.flagLink != 'undefined')
  26. // Drupal.flagLink(container);
  27. $('a.personal-note-link:not(.ajax-processed), ', container)
  28. .bind('click', onClickLink)
  29. .addClass('ajax-processed');
  30. // $('a.personal-note-edit:not(.ajax-processed)', container)
  31. // .bind('click', {'action':'edit'}, onClickLink)
  32. // .addClass('ajax-processed');
  33. };
  34. /**
  35. * onClickCreatLink(event)
  36. */
  37. function onClickLink(event){
  38. // trace('MaterioPersonalNotes :: onClickLink | '+action+' | event', event);
  39. event.preventDefault();
  40. var $link = $(event.currentTarget);
  41. var note_nid = $link.attr('note_nid');
  42. var action = note_nid ? 'edit' : 'create';
  43. var src_nid = $link.attr('src_nid');
  44. trace('MaterioPersonalNotes :: src_nid', src_nid);
  45. trace('MaterioPersonalNotes :: note_nid', note_nid);
  46. var url = Drupal.settings.basePath
  47. +Drupal.settings.pathPrefix
  48. +'materio_personalnotes/form/'+action
  49. +'/'+src_nid;
  50. if(action == 'edit'){
  51. url += '/'+note_nid;
  52. }
  53. $.getJSON(url, function(json){
  54. trace('MaterioPersonalNotes :: '+action+' : json', json);
  55. if(json.new_note_nid){
  56. updateNoteLink($link, json.new_note_nid);
  57. }
  58. showNoteForm($link, json);
  59. });
  60. return false;
  61. };
  62. function updateNoteLink($link, new_note_nid){
  63. trace('MaterioPersonalNotes :: updateNoteLink : new_note_nid', new_note_nid);
  64. $link.attr('note_nid', new_note_nid);
  65. $link.attr('href', Drupal.settings.pathPrefix+'node/'+new_note_nid+'/edit');
  66. };
  67. function showNoteForm($link, json){
  68. trace('MaterioPersonalNotes :: showNoteForm');
  69. // google analytics
  70. $.event.trigger({
  71. type:"record-stat",
  72. categorie:"PersonalNotes",
  73. action: 'show create form'
  74. });
  75. var $card = $link.parents('.node-materiau');
  76. if($card.is('.vm-cardmedium') || $card.is('.vm-cardbig') || $card.is('.vm-cardfull')){
  77. var $note = $('<div class="note-wrapper"/>')
  78. .append($('<div class="close"></i><div>')
  79. .bind('click', function(){
  80. trace('close note');
  81. $(this).parents('.node-materiau').removeClass('note-opened');
  82. setTimeout((function(that){
  83. $(that).parents('.note-wrapper').remove();
  84. })(this), 300);
  85. })
  86. );
  87. $note
  88. .append(json.rendered_form)
  89. .find('textarea')
  90. .focus()
  91. .bind('change keyup paste', function(event){
  92. // trace('text changed', $(this).val());
  93. var note_nid = $(this).parents('form').find('input[name="note_nid"]').val();
  94. saveNote($(this).val(), note_nid);
  95. });
  96. $note
  97. .find('input[type="submit"], .homepage-textfield').hide();
  98. $note
  99. .appendTo($card.addClass('note-opened'));
  100. $note
  101. .find('textarea')
  102. .focus();
  103. }else{
  104. // var $note = $('<div id="modal" class="modal note-modal"/>').appendTo('body');
  105. // $note
  106. // .css({
  107. // position:'absolute',
  108. // top:'40%', left:'50%',
  109. // marginLeft:'-150px', width:'500px',
  110. // zIndex:"99999"
  111. // })
  112. // .append($('<div class="close"></i><div>')
  113. // .bind('click', function(){
  114. // trace('close note');
  115. // $(this).parents('#modal').remove();
  116. // })
  117. // );
  118. // $note
  119. // .append(json.rendered_form)
  120. // .find('textarea')
  121. // .focus()
  122. // .bind('change keyup paste', function(event){
  123. // // trace('text changed', $(this).val());
  124. // var note_nid = $(this).parents('form').find('input[name="note_nid"]').val();
  125. // saveNote($(this).val(), note_nid);
  126. // });
  127. // $note
  128. // .find('input[type="submit"], .homepage-textfield').hide();
  129. // // TODO: esc keypressed close the form
  130. }
  131. };
  132. function saveNote(text, note_nid){
  133. trace(text, 'MaterioPersonalNotes :: saveNote : text');
  134. var url = Drupal.settings.basePath
  135. +Drupal.settings.pathPrefix
  136. +'materio_personalnotes/save'
  137. +'/'+note_nid;
  138. $.post(url, {'content':text}, function(data, status){
  139. trace('MaterioPersonalNotes :: save : status', status);
  140. });
  141. };
  142. init();
  143. };
  144. $(document).ready(function() {
  145. var materioflag = new MaterioPersonalNotes();
  146. });
  147. })(jQuery);