소스 검색

order track list item done

Signed-off-by: bachy <git@g-u-i.net>
bachy 13 년 전
부모
커밋
b4b346b8a5
5개의 변경된 파일236개의 추가작업 그리고 55개의 파일을 삭제
  1. BIN
      images/bullet_move.png
  2. 31 0
      term_reference_tree.css
  3. 97 33
      term_reference_tree.js
  4. 2 1
      term_reference_tree.module
  5. 106 21
      term_reference_tree.widget.inc

BIN
images/bullet_move.png


+ 31 - 0
term_reference_tree.css

@@ -94,6 +94,37 @@
   background-position: middle left;
   background-position: middle left;
 }
 }
 
 
+.term-reference-tree-track-list.order-list li.track-item {
+	padding-left:0;
+}
+
+.term-reference-tree-track-list.order-list li.track-item:hover {
+	color:#000;
+	background-image: none;
+}
+
+.term-reference-tree-track-list.order-list .term-reference-tree-button-move, .term-reference-tree-track-list.order-list .term-reference-tree-button-delete{
+	display:inline-block; vertical-align:middle; zoom:1;
+	width:16px; height:16px;
+	background-repeat: no-repeat;
+	background-position: bottom center;
+	margin:0 5px;
+}
+
+
+.term-reference-tree-track-list.order-list li.track-item .term-reference-tree-button-move{
+	background-image: url("images/bullet_move.png");
+}
+.term-reference-tree-track-list.order-list li.track-item .term-reference-tree-button-move:hover{
+	cursor:move;
+}
+.term-reference-tree-track-list.order-list li.track-item .term-reference-tree-button-delete{
+	background-image: url("images/bullet_delete.png"); float:right;
+}
+.term-reference-tree-track-list.order-list li.track-item .term-reference-tree-button-:hover{
+	cursor:pointer;
+}
+
 .term-reference-tree-track-list li.term_ref_tree_nothing_message {
 .term-reference-tree-track-list li.term_ref_tree_nothing_message {
   list-style-type: none;
   list-style-type: none;
   list-style-image: none;
   list-style-image: none;

+ 97 - 33
term_reference_tree.js

@@ -28,7 +28,26 @@
         //currently selected items to it.
         //currently selected items to it.
         if($(this).hasClass('term-reference-tree-track-list-shown')) {
         if($(this).hasClass('term-reference-tree-track-list-shown')) {
           var track_list_container = $(this).find('.term-reference-tree-track-list');
           var track_list_container = $(this).find('.term-reference-tree-track-list');
-          
+          var tracklist_is_orderable = track_list_container.is('.order-list');
+					if(tracklist_is_orderable){
+						track_list_container.sortable({
+							update: function(event, ui) {
+								console.log('sort update : event', event);
+								console.log('sort update : ui', ui);
+
+								$.each(event.target.children, function(index, val) {
+									var $item = $(val),
+								// event.target = ul.list
+								// ui.item = li.track-item
+											control_id = $item.data('control_id'),
+											$hiddenInput = $('#'+control_id).parent('.form-item').next('input[type=hidden]');
+											// $hiddenInput.attr('value', $item.index());
+											$hiddenInput.val($item.index());
+								});
+							},
+						});
+					}
+					
           //Var to track whether using checkboxes or radio buttons.
           //Var to track whether using checkboxes or radio buttons.
           var input_type =
           var input_type =
             ( $(this).has('input[type=checkbox]').size() > 0 ) ? 'checkbox' : 'radio';
             ( $(this).has('input[type=checkbox]').size() > 0 ) ? 'checkbox' : 'radio';
@@ -40,14 +59,22 @@
           var labels = checked_controls.next();
           var labels = checked_controls.next();
           var label_element;
           var label_element;
           
           
+					//get delta
+					if(tracklist_is_orderable){
+						var weights = checked_controls.parent('.form-item').next('input[type=hidden]');
+					}
+
           //For each label of the checked boxes, add item to the track list.
           //For each label of the checked boxes, add item to the track list.
           labels.each(function(index) {
           labels.each(function(index) {
             label_element = $(labels[index]);
             label_element = $(labels[index]);
+						delta = tracklist_is_orderable ? $(weights[index]).val() : -1;
+						
             addItemToTrackList(
             addItemToTrackList(
               track_list_container,         //Where to add new item.
               track_list_container,         //Where to add new item.
               label_element.html(),         //Text of new item.
               label_element.html(),         //Text of new item.
               $(label_element).attr('for'), //Id of control new item is for.
               $(label_element).attr('for'), //Id of control new item is for.
-              input_type                    //checkbox or radio
+              input_type,                   //checkbox or radio
+							delta													//delta
             );
             );
           }); //End labels.each
           }); //End labels.each
           
           
@@ -62,10 +89,14 @@
             //Remove the "nothing selected" message if showing - add it later if needed.
             //Remove the "nothing selected" message if showing - add it later if needed.
             //removeNothingSelectedMessage(track_list_container);
             //removeNothingSelectedMessage(track_list_container);
             var event_target = $(event.target);
             var event_target = $(event.target);
-            var control_id = event_target.data('control_id');
-
-            if(control_id) {
-              event_target.remove();
+						var event_parent_list = event_target.parent('li');
+            var control_id = event_parent_list.data('control_id');
+						// console.log('event', event);
+						// console.log('event_target.parent("li")', event_target.parent('li'));
+						// console.log('control_id', control_id);
+						// console.log('event_target.is(term-reference-tree-delete)', event_target.is('term-reference-tree-delete'));
+            if(event_target.is('.term-reference-tree-button-delete') && control_id) {
+              event_parent_list.remove();
 
 
               var checkbox = $('#' + control_id);
               var checkbox = $('#' + control_id);
               checkbox.removeAttr('checked');
               checkbox.removeAttr('checked');
@@ -89,7 +120,8 @@
                 track_list_container,         //Where to add new item.
                 track_list_container,         //Where to add new item.
                 label_element.html(),         //Text of new item.
                 label_element.html(),         //Text of new item.
                 $(label_element).attr('for'), //Id of control new item is for.
                 $(label_element).attr('for'), //Id of control new item is for.
-                input_type                    //checkbox or radio
+                input_type,                   //checkbox or radio
+								-1													// delta
               );
               );
             }
             }
             else {
             else {
@@ -144,8 +176,9 @@
    * 
    * 
    * @param control_type Control type - 'checkbox' or 'radio'.
    * @param control_type Control type - 'checkbox' or 'radio'.
    */
    */
-  function addItemToTrackList(track_list_container, item_text, control_id, control_type) {
-    var new_item = $('<li class="track-item">' + item_text + '</li>');
+  function addItemToTrackList(track_list_container, item_text, control_id, control_type, delta) {
+		console.log('addItemToTrackList');
+    var new_item = $('<li class="track-item" delta="'+ delta +'"><div class="term-reference-tree-button-move"></div>' + item_text + '<div class="term-reference-tree-button-delete"></div></li>');
     new_item.data('control_id', control_id);
     new_item.data('control_id', control_id);
     
     
     //Add an id for easy finding of the item.
     //Add an id for easy finding of the item.
@@ -173,35 +206,66 @@
       }
       }
       return;
       return;
     }
     }
-    
-    //Using checkboxes, so there can be more than one selected item.
-    //Find the right place to put the new item, to match the order of the
-    //checkboxes.
+
+		//Using checkboxes, so there can be more than one selected item.
+    //Find the right place to put the new item, 
+		// to match the order of the checkboxes.
+		// OR order of delta
     var list_items = track_list_container.find('li');
     var list_items = track_list_container.find('li');
     var item_comparing_to;
     var item_comparing_to;
-    
+  
     //Flag to tell whether the item was inserted.
     //Flag to tell whether the item was inserted.
     var inserted_flag = false;
     var inserted_flag = false;
-    list_items.each(function(index){
-      item_comparing_to = $(list_items[index]);
+
+		if(!track_list_container.is('.order-list')){
+	    
+	    list_items.each(function(index){
+	      item_comparing_to = $(list_items[index]);
       
       
-      //If item is already on the track list, do nothing.
-      if ( control_id == item_comparing_to.data('control_id') ) {
-        inserted_flag = true;
-        return false; //Returning false stops the loop.
-      }
-      else if ( control_id < item_comparing_to.data('control_id') ) {
-        //Add it here.
-        item_comparing_to.before(new_item);
-        inserted_flag = true;
-        return false; //Returning false stops the loop.
-      }
-    });
+	      //If item is already on the track list, do nothing.
+	      if ( control_id == item_comparing_to.data('control_id') ) {
+	        inserted_flag = true;
+	        return false; //Returning false stops the loop.
+	      }
+	      else if ( control_id < item_comparing_to.data('control_id') ) {
+	        //Add it here.
+	        item_comparing_to.before(new_item);
+	        inserted_flag = true;
+	        return false; //Returning false stops the loop.
+	      }
+	    });
     
     
-    //If not inserted yet, add new item at the end of the track list.
-    if ( ! inserted_flag ) {
-      track_list_container.append(new_item);
-    }
+	    //If not inserted yet, add new item at the end of the track list.
+	    if ( ! inserted_flag ) {
+      	track_list_container.append(new_item);
+	    }
+		
+		}else{
+			if( ! track_list_container.find('#'+new_item.attr('id')).size() ){
+				
+				if(delta == -1){
+					track_list_container.append(new_item);				
+					inserted_flag = true;
+				}else{
+					list_items.each(function(index){
+			      item_comparing_to = $(this);
+						if ( delta < item_comparing_to.attr('delta') ) {
+			        //Add it here.
+			        item_comparing_to.before(new_item);
+			        inserted_flag = true;
+			        return false; //Returning false stops the loop.
+			      }
+			    });
+			    //If not inserted yet, add new item at the end of the track list.
+			    if ( ! inserted_flag )
+		      	track_list_container.append(new_item);
+		    
+				}
+
+				track_list_container.sortable('refresh');
+			}
+		}
+		
   }
   }
   
   
   /**
   /**
@@ -246,7 +310,7 @@
 
 
   // This helper function checks if the maximum number of choices is already selected.
   // This helper function checks if the maximum number of choices is already selected.
   // If so, it disables all the other options.  If not, it enables them.
   // If so, it disables all the other options.  If not, it enables them.
-  function checkMaxChoices(item, checkbox) {
+  function checkMaxChoices(item, checkbox, order_list) {
     var maxChoices = -1;
     var maxChoices = -1;
     try {
     try {
       maxChoices = parseInt(Drupal.settings.term_reference_tree.trees[item.attr('id')]['max_choices']);
       maxChoices = parseInt(Drupal.settings.term_reference_tree.trees[item.attr('id')]['max_choices']);

+ 2 - 1
term_reference_tree.module

@@ -33,7 +33,7 @@ function term_reference_tree_element_info() {
       '#input' => false,
       '#input' => false,
       '#theme' => array('checkbox_tree_track_list'),
       '#theme' => array('checkbox_tree_track_list'),
       '#pre_render' => array('form_pre_render_conditional_form_element'),
       '#pre_render' => array('form_pre_render_conditional_form_element'),
-    ),
+    )
   );
   );
   
   
   return $types;
   return $types;
@@ -183,6 +183,7 @@ function _term_reference_tree_flatten($element, &$form_state) {
   $children = element_children($element);
   $children = element_children($element);
   foreach($children as $c) {
   foreach($children as $c) {
     $child = $element[$c];
     $child = $element[$c];
+		// dsm($child, '$child');
     if (array_key_exists('#type', $child) && ($child['#type'] == 'radio' || $child['#type'] == 'checkbox')) {
     if (array_key_exists('#type', $child) && ($child['#type'] == 'radio' || $child['#type'] == 'checkbox')) {
       $output[] = $child;
       $output[] = $child;
     }
     }

+ 106 - 21
term_reference_tree.widget.inc

@@ -19,6 +19,7 @@ function term_reference_tree_field_widget_info() {
         'select_parents' => 0,
         'select_parents' => 0,
         'cascading_selection' => 0,
         'cascading_selection' => 0,
         'track_list' => 0,
         'track_list' => 0,
+        'track_list_order' => 0,
         'token_display' => '',
         'token_display' => '',
         'parent_term_id' => '',
         'parent_term_id' => '',
         'max_depth' => '',
         'max_depth' => '',
@@ -171,7 +172,18 @@ function term_reference_tree_field_widget_settings_form($field, $instance) {
       '#default_value' => $settings['track_list'],
       '#default_value' => $settings['track_list'],
       '#return_value' => 1,
       '#return_value' => 1,
     );
     );
+
+    $form['track_list_order'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Track list drag and drop order'),
+      '#description' => t(
+          'Allow drag and drop selected terms ordering on tracklist.'),
+      '#default_value' => $settings['track_list_order'],
+      '#return_value' => 1,
+      '#element_validate' => array('_term_reference_tree_track_list_order_validate'),
+    );
     
     
+
     $form['max_depth'] = array(
     $form['max_depth'] = array(
       '#type' => 'textfield',
       '#type' => 'textfield',
       '#title' => t('Maximum Depth'),
       '#title' => t('Maximum Depth'),
@@ -251,6 +263,18 @@ function _term_reference_tree_cascading_selection_validate($element, &$form_stat
   }
   }
 }
 }
 
 
+function _term_reference_tree_track_list_order_validate($element, &$form_state){
+  if ($form_state['values']['instance']['widget']['settings']['track_list'] == 0 && $form_state['values']['instance']['widget']['settings']['track_list_order'] == 1) {
+    // This is pretty wonky syntax for the field name in form_set_error, but it's
+    // correct.
+    form_set_error('field][track_list_order', t('You must enable Track List if Track List Order is enabled.'));
+  }
+	/*
+		TODO check if number of values is diffrent from 1
+	*/
+}
+
+
 /**
 /**
  * Process the checkbox_tree widget.
  * Process the checkbox_tree widget.
  * 
  * 
@@ -310,6 +334,7 @@ function term_reference_tree_process_checkbox_tree($element, $form_state) {
       $element[] = array(
       $element[] = array(
         '#type' => 'checkbox_tree_track_list',
         '#type' => 'checkbox_tree_track_list',
         '#max_choices' => $max_choices,
         '#max_choices' => $max_choices,
+				'#track_list_order' => $element['#track_list_order'],
       );
       );
     }
     }
   } 
   } 
@@ -443,7 +468,8 @@ function theme_checkbox_tree_label($variables) {
 function theme_checkbox_tree_track_list($variables) {
 function theme_checkbox_tree_track_list($variables) {
   //Should the label be singular or plural? Depends on cardinality of term field.
   //Should the label be singular or plural? Depends on cardinality of term field.
   static $nothingselected;
   static $nothingselected;
-  
+  // dsm($variables, 'theme_checkbox_tree_track_list : $variables');
+
   if(!$nothingselected) {
   if(!$nothingselected) {
     $nothingselected = t('[Nothing selected]');
     $nothingselected = t('[Nothing selected]');
     //Add the "Nothing selected" text. To style it, replace it with whatever you want.
     //Add the "Nothing selected" text. To style it, replace it with whatever you want.
@@ -459,17 +485,17 @@ function theme_checkbox_tree_track_list($variables) {
       'Selected item (click the item to uncheck it)',
       'Selected item (click the item to uncheck it)',
       'Selected items (click an item to uncheck it)'
       'Selected items (click an item to uncheck it)'
   );
   );
+	$order = $variables['element']['#track_list_order'] ? 'order-list' : '';
   $output = 
   $output = 
     '<div class="term-reference-track-list-container">
     '<div class="term-reference-track-list-container">
        <div class="term-reference-track-list-label">' . $label . '</div>
        <div class="term-reference-track-list-label">' . $label . '</div>
-       <ul class="term-reference-tree-track-list"><li class="term_ref_tree_nothing_message">'.$nothingselected.'</li></ul>
+       <ul class="term-reference-tree-track-list '.$order.'"><li class="term_ref_tree_nothing_message">'.$nothingselected.'</li></ul>
      </div>';
      </div>';
      
      
 
 
   return $output;
   return $output;
 }
 }
 
 
-
 /**
 /**
  * Implements hook_widget_field_form().
  * Implements hook_widget_field_form().
  */
  */
@@ -512,6 +538,7 @@ function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $in
       $element['#select_parents'] = $settings['select_parents'];
       $element['#select_parents'] = $settings['select_parents'];
       $element['#cascading_selection'] = $settings['cascading_selection'];
       $element['#cascading_selection'] = $settings['cascading_selection'];
       $element['#track_list'] = $settings['track_list'];
       $element['#track_list'] = $settings['track_list'];
+      $element['#track_list_order'] = $settings['track_list_order'];
       $element['#parent_tid'] = $settings['parent_term_id'] || $field['settings']['allowed_values'][0]['parent'];
       $element['#parent_tid'] = $settings['parent_term_id'] || $field['settings']['allowed_values'][0]['parent'];
       $element['#vocabulary'] = $voc->vid;
       $element['#vocabulary'] = $voc->vid;
       $element['#token_display'] = module_exists('token') ? $settings['token_display'] : '';
       $element['#token_display'] = module_exists('token') ? $settings['token_display'] : '';
@@ -523,7 +550,11 @@ function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $in
     '#element_validate' => array('_term_reference_tree_widget_validate'),
     '#element_validate' => array('_term_reference_tree_widget_validate'),
     '#properties' => $properties,
     '#properties' => $properties,
   );
   );
-
+	
+	if ($settings['track_list_order']) {
+		drupal_add_library('system', 'ui.sortable');
+	}
+	
   return $element;
   return $element;
 }
 }
 
 
@@ -544,25 +575,58 @@ function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $in
  *   The validated element.
  *   The validated element.
  */
  */
 function _term_reference_tree_widget_validate(&$element, &$form_state) {
 function _term_reference_tree_widget_validate(&$element, &$form_state) {
+	dsm($element, '_term_reference_tree_widget_validate | $element');
   $items = _term_reference_tree_flatten($element, $form_state);
   $items = _term_reference_tree_flatten($element, $form_state);
+	dsm($items, '$items');
   $value = array();
   $value = array();
 
 
   if ($element['#max_choices'] != 1) {
   if ($element['#max_choices'] != 1) {
-    foreach($items as $child) {
-      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
-        array_push($value, array($element['#value_key'] => $child['#value']));
-
-        // If the element is leaves only and select parents is on, then automatically
-        // add all the parents of each selected value.
-        if ($element['#select_parents'] && $element['#leaves_only']) {
-          foreach($child['#parent_values'] as $parent_tid) {
-            if (!in_array(array($element['#value_key'] => $parent_tid), $value)) {
-              array_push($value, array($element['#value_key'] => $parent_tid));
-            }
-          }
-        }
-      } 
-    }
+		if(!$element['#track_list_order']){
+	    foreach($items as $child) {
+	      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
+	        array_push($value, array( $element['#value_key'] => $child['#value']));
+
+	        // If the element is leaves only and select parents is on, then automatically
+	        // add all the parents of each selected value.
+	        if ($element['#select_parents'] && $element['#leaves_only']) {
+	          foreach($child['#parent_values'] as $parent_tid) {
+	            if (!in_array(array($element['#value_key'] => $parent_tid), $value)) {
+	              array_push($value, array($element['#value_key'] => $parent_tid));
+	            }
+	          }
+	        }
+
+	      } 
+	    }
+			
+		}else{
+			$selected_terms = array();			
+			foreach($items as $child) {
+	      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
+					$delta = $form_state['input'][$child['#value'].'-weight'];
+	        $selected_terms[$delta] = array($element['#value_key'] => $child['#value']);
+				
+	        // If the element is leaves only and select parents is on, then automatically
+	        // add all the parents of each selected value.
+	        if ($element['#select_parents'] && $element['#leaves_only']) {
+	          foreach($child['#parent_values'] as $parent_tid) {
+	            if (!in_array(array($element['#value_key'] => $parent_tid), $selected_terms)) {
+								$delta = $form_state['input'][$parent_tid.'-weight'];
+				        $selected_terms[$delta] = array($element['#value_key'] => $parent_tid);
+	            }
+	          }
+	        }
+
+	      } 
+	    }
+			// reorder items
+			ksort($selected_terms);
+			// record in value
+			foreach ($selected_terms as $delta => $term) {
+				$value[] = $term;
+			}
+
+		}
   }
   }
   else {
   else {
     // If it's a tree of radio buttons, they all have the same value, so we can just
     // If it's a tree of radio buttons, they all have the same value, so we can just
@@ -575,6 +639,8 @@ function _term_reference_tree_widget_validate(&$element, &$form_state) {
     }
     }
   }
   }
   
   
+	dsm($value, '$value');
+
   if ($element['#required'] && empty($value)) {
   if ($element['#required'] && empty($value)) {
     // The title is already check_plained so it's appropriate to use !.
     // The title is already check_plained so it's appropriate to use !.
     form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
     form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
@@ -701,7 +767,23 @@ function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$valu
       $parents_for_id = array_merge($element['#parents'], array($term->tid));
       $parents_for_id = array_merge($element['#parents'], array($term->tid));
       $e['#id'] = drupal_html_id('edit-' . implode('-', $parents_for_id));
       $e['#id'] = drupal_html_id('edit-' . implode('-', $parents_for_id));
       $e['#parents'] = $element['#parents'];
       $e['#parents'] = $element['#parents'];
-    }
+    }else if($element['#track_list_order']){
+			$delta = 0;
+			$i = -1;
+			if(isset($value[$term->tid])){
+				foreach ($value as $tid) {
+					$i++;
+					if($term->tid == $tid)
+						break;
+				}
+			}
+			
+			$e_weight = array(
+			  '#type' => 'hidden',
+			  '#value' => $i,
+				'#name' => $term->tid.'-weight',
+			);
+		}
   }
   }
   else {
   else {
     $e = array(
     $e = array(
@@ -710,9 +792,12 @@ function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$valu
     );
     );
   }
   }
 
 
-
   $container[$term->tid] = $e;  
   $container[$term->tid] = $e;  
 
 
+	if(isset($e_weight)){
+		$container[$term->tid.'-weight'] = $e_weight;
+	}
+
   if (($depth + 1 <= $element['#max_depth'] || !$element['#max_depth']) && property_exists($term, 'children') && count($term->children) > 0) {
   if (($depth + 1 <= $element['#max_depth'] || !$element['#max_depth']) && property_exists($term, 'children') && count($term->children) > 0) {
     $parents = $parent_tids;
     $parents = $parent_tids;
     $parents[] = $term->tid;
     $parents[] = $term->tid;