personnale notes front ajax is working, remains to improve the display, do it like liked materiaux

This commit is contained in:
Bachir Soussi Chiadmi 2017-11-28 17:02:45 +01:00
parent 268c8b619b
commit 7d5bedcb0c
8 changed files with 357 additions and 158 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

View File

@ -1,2 +1,10 @@
(function($){MaterioPersonalNotes=function(){function init(){trace('MaterioPersonalNotes :: init');};init();};$(document).ready(function(){var materioflag=new MaterioPersonalNotes();});})(jQuery); (function($){MaterioPersonalNotes=function(){function init(){trace('MaterioPersonalNotes :: init');$(document).bind('resultscompleted resultschanged previewloaded',onResultsUpdated);};function onResultsUpdated(event){trace('MaterioPersonalNotes :: onResultsUpdated',event);ajaxifyLinks(event.container);};function ajaxifyLinks(container){trace('MaterioPersonalNotes :: ajaxifyLinks',container);container=((container!=null)?container:'body');$('a.personal-note-link:not(.ajax-processed), ',container).bind('click',onClickLink).addClass('ajax-processed');};function onClickLink(event){event.preventDefault();var $link=$(event.currentTarget);var note_nid=$link.attr('note_nid');var action=note_nid?'edit':'create';var src_nid=$link.attr('src_nid');trace('MaterioPersonalNotes :: src_nid',src_nid);trace('MaterioPersonalNotes :: note_nid',note_nid);var url=Drupal.settings.basePath
+Drupal.settings.pathPrefix
+'materio_personalnotes/form/'+action
+'/'+src_nid;if(action=='edit'){url+='/'+note_nid;}
$.getJSON(url,function(json){trace('MaterioPersonalNotes :: '+action+' : json',json);if(json.new_note_nid){updateNoteLink($link,json.new_note_nid);}
showNoteForm(json);});return false;};function updateNoteLink($link,new_note_nid){trace('MaterioPersonalNotes :: updateNoteLink : new_note_nid',new_note_nid);$link.attr('note_nid',new_note_nid);$link.attr('href',Drupal.settings.pathPrefix+'node/'+new_note_nid+'/edit');};function showNoteForm(json){trace('MaterioPersonalNotes :: showNoteForm');$.event.trigger({type:"record-stat",categorie:"PersonalNotes",action:'show create form'});var $modal=$('<div id="modal" class="modal note-modal"/>').appendTo('body');$modal.css({position:'absolute',top:'40%',left:'50%',marginLeft:'-150px',width:'500px',zIndex:"99999"}).append($('<div class="close"></i><div>').bind('click',function(){trace('close note');$(this).parents('#modal').remove();}));$modal.append(json.rendered_form).find('textarea').focus().bind('change keyup paste',function(event){var note_nid=$(this).parents('form').find('input[name="note_nid"]').val();saveNote($(this).val(),note_nid);});$modal.find('input[type="submit"], .homepage-textfield').hide();};function saveNote(text,note_nid){trace(text,'MaterioPersonalNotes :: saveNote : text');var url=Drupal.settings.basePath
+Drupal.settings.pathPrefix
+'materio_personalnotes/save'
+'/'+note_nid;$.post(url,{'content':text},function(data,status){trace('MaterioPersonalNotes :: save : status',status);});};init();};$(document).ready(function(){var materioflag=new MaterioPersonalNotes();});})(jQuery);

View File

@ -12,14 +12,132 @@ MaterioPersonalNotes = function(){
function init(){ function init(){
trace('MaterioPersonalNotes :: init'); trace('MaterioPersonalNotes :: init');
$(document)
.bind('resultscompleted resultschanged previewloaded', onResultsUpdated);
// .bind('init-scroller-pager', onInitScrollerPager)
// .bind('load-scroller-pager', onLoadScrollerPager)
// .bind('view-mode-changed', onViewModeChanged)
// .bind('history-state-change', onHistoryStateChange);
}; };
function onResultsUpdated(event){
trace('MaterioPersonalNotes :: onResultsUpdated', event);
ajaxifyLinks(event.container);
};
function ajaxifyLinks(container){
trace('MaterioPersonalNotes :: ajaxifyLinks', container);
container = ((container != null) ? container : 'body');
// trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink);
// if (typeof Drupal.flagLink != 'undefined')
// Drupal.flagLink(container);
$('a.personal-note-link:not(.ajax-processed), ', container)
.bind('click', onClickLink)
.addClass('ajax-processed');
// $('a.personal-note-edit:not(.ajax-processed)', container)
// .bind('click', {'action':'edit'}, onClickLink)
// .addClass('ajax-processed');
};
/**
* onClickCreatLink(event)
*/
function onClickLink(event){
// trace('MaterioPersonalNotes :: onClickLink | '+action+' | event', event);
event.preventDefault();
var $link = $(event.currentTarget);
var note_nid = $link.attr('note_nid');
var action = note_nid ? 'edit' : 'create';
var src_nid = $link.attr('src_nid');
trace('MaterioPersonalNotes :: src_nid', src_nid);
trace('MaterioPersonalNotes :: note_nid', note_nid);
var url = Drupal.settings.basePath
+Drupal.settings.pathPrefix
+'materio_personalnotes/form/'+action
+'/'+src_nid;
if(action == 'edit'){
url += '/'+note_nid;
}
$.getJSON(url, function(json){
trace('MaterioPersonalNotes :: '+action+' : json', json);
if(json.new_note_nid){
updateNoteLink($link, json.new_note_nid);
}
showNoteForm(json);
});
return false;
};
function updateNoteLink($link, new_note_nid){
trace('MaterioPersonalNotes :: updateNoteLink : new_note_nid', new_note_nid);
$link.attr('note_nid', new_note_nid);
$link.attr('href', Drupal.settings.pathPrefix+'node/'+new_note_nid+'/edit');
};
function showNoteForm(json){
trace('MaterioPersonalNotes :: showNoteForm');
// google analytics
$.event.trigger({
type:"record-stat",
categorie:"PersonalNotes",
action: 'show create form'
});
var $modal = $('<div id="modal" class="modal note-modal"/>').appendTo('body');
$modal
.css({
position:'absolute',
top:'40%', left:'50%',
marginLeft:'-150px', width:'500px',
zIndex:"99999"
})
.append($('<div class="close"></i><div>')
.bind('click', function(){
trace('close note');
$(this).parents('#modal').remove();
})
);
$modal
.append(json.rendered_form)
.find('textarea')
.focus()
.bind('change keyup paste', function(event){
// trace('text changed', $(this).val());
var note_nid = $(this).parents('form').find('input[name="note_nid"]').val();
saveNote($(this).val(), note_nid);
});
$modal
.find('input[type="submit"], .homepage-textfield').hide();
// TODO: esc keypressed close the form
};
function saveNote(text, note_nid){
trace(text, 'MaterioPersonalNotes :: saveNote : text');
var url = Drupal.settings.basePath
+Drupal.settings.pathPrefix
+'materio_personalnotes/save'
+'/'+note_nid;
$.post(url, {'content':text}, function(data, status){
trace('MaterioPersonalNotes :: save : status', status);
});
};
init(); init();
}; };
$(document).ready(function() { $(document).ready(function() {

View File

@ -23,23 +23,32 @@ function materio_personalnotes_permission() {
return $perms; return $perms;
} }
// function materio_personalnotes_menu(){ function materio_personalnotes_menu(){
// $items = array(); $items = array();
//
// $base = array( $base = array(
// 'type' => MENU_CALLBACK, 'type' => MENU_CALLBACK,
// 'file' => 'materio_personalnotes.pages.inc', 'file' => 'materio_personalnotes.pages.inc',
// ); );
//
// $items['materio_personalnotes/registerblock'] = $base+array( $items['materio_personalnotes/form/%/%'] = $base+array(
// 'title' => 'Materio base user ajax', 'access arguments' => array('create own personal notes'),
// 'page callback' => 'materio_personalnotes_registerblock', 'access callback' => 'user_access',
// // 'page arguments' => array(), 'title' => 'Materio base create/edit note ajax',
// 'access callback' => TRUE, 'page callback' => 'materio_personalnotes_createeditnote',
// ); 'page arguments' => array(2,3),
// );
// return $items;
// } $items['materio_personalnotes/save/%'] = $base+array(
'access arguments' => array('create own personal notes'),
'access callback' => 'user_access',
'title' => 'Materio base save note ajax',
'page callback' => 'materio_personalnotes_savenote',
'page arguments' => array(2),
);
return $items;
}
/** /**
* Implements hook_menu_alter(). * Implements hook_menu_alter().
@ -55,9 +64,9 @@ function materio_personalnotes_permission() {
* Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120 * Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
*/ */
function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode) { function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode) {
if($type == 'node'){ // dsm($entity, 'entity');
if($type == 'node' && $entity->type == "materiau"){
if(user_access('create own personal notes') && $view_mode != 'print'){ if(user_access('create own personal notes') && $view_mode != 'print'){
$entity->content['personalnotelink'] = materio_personalnotes_get_note_link($entity); $entity->content['personalnotelink'] = materio_personalnotes_get_note_link($entity);
// drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css'); // drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
@ -68,28 +77,51 @@ function materio_personalnotes_entity_view($entity, $type, $view_mode, $langcode
} }
function materio_personalnotes_get_note_link($entity){ function materio_personalnotes_get_note_link($entity){
global $user;
// dsm($user);
// get note already created for this entity by current user
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'note')
// ->propertyCondition('status', NODE_PUBLISHED);
->propertyCondition('uid', $user->uid)
->fieldCondition('field_target_content_nid', 'value', $entity->nid)
->addMetaData('account', user_load(1));
$result = $query->execute();
// dsm($result, 'result');
// if note alredy exists link to it if (isset($result['node'])) {
// if note alredy exists link to it
// else create one $note = array_shift($result['node']);
#create new list // dsm($note, 'note');
$link = array( $link = array(
'#path' => '/node/add/note', '#path' => '/node/'.$note->nid.'/edit',
'#query' => array( '#attributes' => array(
// 'edit[field_target_content][und][0][target_id]' => $entity->title.'('.$entity->nid.')', 'class' => array('personal-note-link', 'personal-note-edit'),
'target_id'=>$entity->nid, 'title' => t('Edit your @title\'s notes.', array('@title'=>$entity->title)),
), 'src_nid' => $entity->nid,
// get the content type from settings OR create the content type with module install 'note_nid' => $note->nid,
// TODO: add data (node nid) for pre-filled reference field of new note ),
'#attributes' => array( );
'class' => array('personal-note-link', 'personal-note-create'), }else{
'title' => t('create a note for @title.', array('@title'=>$entity->title)), // else create one
'nid' => $entity->nid, $link = array(
), // get the content type from settings OR create the content type with module install
'#theme'=>'materio_personalnotes_note_link', '#path' => '/node/add/note',
); '#query' => array(
'target_id'=>$entity->nid,
),
'#attributes' => array(
'class' => array('personal-note-link', 'personal-note-create'),
'title' => t('create a note for @title.', array('@title'=>$entity->title)),
'src_nid' => $entity->nid,
),
);
}
if(isset($link)){ if(isset($link)){
$link['#theme'] = 'materio_personalnotes_note_link';
// dsm($link, 'link'); // dsm($link, 'link');
drupal_add_js(drupal_get_path('module', 'materio_personalnotes').'/js/dist/materio_personalnotes.min.js'); drupal_add_js(drupal_get_path('module', 'materio_personalnotes').'/js/dist/materio_personalnotes.min.js');
return $link; return $link;
@ -103,69 +135,17 @@ function materio_personalnotes_get_note_link($entity){
*/ */
function materio_personalnotes_form_alter(&$form, &$form_state, $form_id) { function materio_personalnotes_form_alter(&$form, &$form_state, $form_id) {
// dsm($form_id); // dsm($form_id);
/* if($form_id == "note_node_form" && isset($_GET['target_id']) && $target_id = $_GET['target_id']){
*if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) { // dsm($_GET, 'get');
* $form['workflow']['upload_' . $form['type']['#value']] = array( // dsm($target_id, 'target_id');
* '#type' => 'radios', // dsm($form, 'form');
* '#title' => t('Attachments'), // dsm($form_state, 'form_state');
* '#default_value' => variable_get('upload_' . $form['type']['#value'], 1),
* '#options' => array(t('Disabled'), t('Enabled')),
* );
*}
*/
if($form_id == "note_node_form" && $target_id = $_GET['target_id']){
dsm($_GET, 'get');
dsm($target_id, 'target_id');
dsm($form, 'form');
dsm($form_state, 'form_state');
// $form['#after_build'][] = 'materio_personalnotes_noteform_after_build';
// $form_state['field']['field_target_content']['und']['instance']['default_value'] = $target_id;
$form['field_target_content_nid']['und'][0]['value']['#default_value'] = $target_id; $form['field_target_content_nid']['und'][0]['value']['#default_value'] = $target_id;
$form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden'; $form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden';
// $form_state['field']['field_target_content']['und']['items_count'] = 1;
} }
} }
/**
* Implements hook_node_presave().
*/
function materio_personalnotes_node_presave($node) {
/*
*if ($node->nid && $node->moderate) {
* // Reset votes when node is updated:
* $node->score = 0;
* $node->users = '';
* $node->votes = 0;
*}
*/
/* Your code here */
}
// function materio_personalnotes_noteform_after_build($form, &$form_state) {
// dsm($_GET, 'get');
// dsm($form, 'form');
// dsm($form_state, 'form_state');
//
// $form_state['field_target_content']['und'][0]['target_id']['#default_value'] = $target_id;
//
// // switch ($form['form_id']['#value']) {
// //
// // case 'your_form':
// //
// // $form['field_yournoderef']['nid']['nid']['#value'] = arg(3);
// // $form['field_yournoderef']['#access'] = false;
// // break;
// //
// // }
//
// return $form;
//
// }
/** /**
* Implements hook_theme(). * Implements hook_theme().
*/ */
@ -188,3 +168,18 @@ function theme_materio_personalnotes_note_link($vars){
) )
); );
} }
/**
* Implements hook_block_info().
*/
// function materio_personalnotes_block_info() {
//
// TODO: add my notes block
// $blocks[''] = array(
// 'info' => t(''),
// 'cache' => DRUPAL_NO_CACHE
// );
//
// return $blocks;
// }

View File

@ -1,62 +1,112 @@
<?php <?php
// function materio_personalnotes_createeditnote($action = 'create', $src_nid = null, $note_nid = null){
// function materio_personalnotes_registerblock(){ $debug = false;
// $return = array();
// $block = block_load('materio_personalnotes','user_createaccount'); global $user;
// $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
// drupal_json_output($return); $rep = array();
// }
// if(!$src_nid)
// function materio_personalnotes_loginandregisterblock(){ drupal_json_output(array('error'=>"src_nid arguments needed"));
// $return = array();
// $block = block_load('materio_personalnotes','user_register'); $contenttype = 'note';
// $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
// drupal_json_output($return); // check if request is ajax, if not rediret
// } if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
// switch ($action) {
// function materio_personalnotes_register_submit(){ case 'edit':
// $return = array(); drupal_goto('node/'.$src_nid.'/edit');
// $return['POST'] = $_POST; break;
// default:
// $form_state = array( "values"=>$_POST); drupal_goto('node/add/' . $contenttype . '?target_id='.$src_nid);
// drupal_form_submit($_POST['form_id'], $form_state); break;
// }
// // $return['form_state'] = $form_state; exit ;
// }
// $return['errors'] = form_get_errors();
// if($return['errors']){
// unset ($_SESSION['messages']['error']); module_load_include('inc', 'node', 'node.pages');
// }else{ switch ($action) {
// $messages = drupal_get_messages('status'); case 'edit':
// // $return['messages'] = $messages; if(!$note_nid){
// // drupal_set_message(t("Congratulations, you juste created your free materiO' account, welcome !"), 'status'); return "For edit action, note_nid is needed";
// foreach ($messages['status'] as $msg) { }else{
// drupal_set_message($msg, 'status'); $note = node_load($note_nid);
// } $form = drupal_get_form('note_node_form', $note);
// } }
// // after registration user is automaticly logged in, thank's to login tobogan module break;
// default: // create
// drupal_json_output($return); $new_note = (object) array(
// } 'uid' => $user->uid,
// 'name' => (isset($user->name) ? $user->name : ''),
// function materio_personalnotes_login_submit(){ 'type' => 'note',
// $return = array(); 'language' => LANGUAGE_NONE,
// $return['POST'] = $_POST; 'field_target_content_nid' => array(
// 'und'=>array(
// $form_state = array("values"=>$_POST); array(
// drupal_form_submit($_POST['form_id'], $form_state); 'value'=>$src_nid
// )
// $return['errors'] = form_get_errors(); )
// if($return['errors']) ),
// unset ($_SESSION['messages']['error']); 'field_personal_note' => array(
// 'und'=>array(
// // if user-login form succed we retreive the user uid on $form_state, then we can effectively loggin the user array(
// if($uid = $form_state['uid']) 'value'=>$src_nid
// user_login_submit(array(), $form_state); )
// )
// drupal_json_output($return); )
// } );
// node_object_prepare($new_note);
// node_save($new_note);
$note_nid = $new_note->nid;
$rep['new_note_nid'] = $note_nid;
$form = drupal_get_form('note_node_form', $new_note);
// pre-fill the field_target_content_nid with src_nid argument
// $form['field_target_content_nid']['und'][0]['value']['#default_value'] = $src_id;
// $form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden';
break;
}
$form['note_nid'] = array(
'#type'=>'hidden',
'#value'=> $note_nid,
'#name' => 'note_nid',
);
$rendered_form = drupal_render($form);
$rep += array(
// 'form'=>$form,
'rendered_form'=>$rendered_form
);
if ($debug) {
dsm($rep, 'rep');
return "debug display";
}else{
drupal_json_output($rep);
}
}
function materio_personalnotes_savenote($note_nid = null){
$debug = false;
if(!$note_nid)
drupal_json_output(array('error'=>"note_nid arguments needed"));
$node = node_load($note_nid);
// $note->field_target_content_nid
$node->field_personal_note[$node->language][0]['value'] = $_POST['content'];
node_save($node);
if($debug){
dsm($node, "note node");
return 'debug display';
}else{
drupal_json_output(array('status'=>'note node saved'));
}
}

View File

@ -8327,6 +8327,18 @@ body.node-type-publication article.node-publication .field figure, body.node-typ
margin-left: 1em; margin-left: 1em;
width: 70%; } } width: 70%; } }
.note-modal .close {
position: absolute;
top: 0;
right: 0;
width: 8px;
height: 8px;
border-radius: 4px;
background-color: #fff !important;
background-image: url(../img/close2.png);
background-size: contain;
background-position: center; }
/* /*
__ ___ __ ___
/ |/ /__ ______________ _____ ____ _____ / |/ /__ ______________ _____ ____ _____

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

View File

@ -3836,8 +3836,24 @@ body.node-type-publication{
} }
} }
// _ __ __
// / | / /___ / /____ _____
// / |/ / __ \/ __/ _ \/ ___/
// / /| / /_/ / /_/ __(__ )
// /_/ |_/\____/\__/\___/____/
.note-modal{
.close{
position: absolute;
top:0; right:0;
width:8px; height:8px;
border-radius: 4px;
background-color: #fff!important;
background-image: url(../img/close2.png);
background-size: contain;
background-position: center;
}
}
@import "misc.scss"; @import "misc.scss";