Skip to content

Commit

Permalink
#160. Changed drupalgap_jqm_page_event_fire() to take in page argumen…
Browse files Browse the repository at this point in the history
…ts and to send them along to the page event handler. Added drupalgap_taxonomy_vocabularies_extract(). Changed drupalgap.services.drupalgap_system.connect success to extract taxonomy vocabularies from the result data. Changed theme_taxonomy_term_reference() to load the vocabulary from its machine name, pass arguments along to the pageshow handler, and set an onchange event listener on the select list. Changed _theme_taxonomy_term_reference_load_items() to accept options and make a call to the drupal server for the terms, it then populates the select list upon success. Added _theme_taxonomy_term_reference_onchange(). Implemented taxonomy_vocabulary_machine_name_load().
  • Loading branch information
signalpoint committed Sep 5, 2013
1 parent 6211115 commit 7ed07b3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
16 changes: 11 additions & 5 deletions drupalgap.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var drupalgap = {
'router_path':'', /* The current menu router path. */
'services':{},
'sessid':null,
taxonomy_vocabularies:null, /* holds the services index of vocabularies */
taxonomy_vocabularies:false, /* holds the services index of vocabularies */
'theme_path':'',
'themes':[],
'theme_registry':{},
Expand Down Expand Up @@ -683,12 +683,18 @@ function drupalgap_item_list_populate(list_css_selector, items) {
* menu_execute_active_handler() to prevent jQM from firing inline page event
* handlers more than once.
*/
function drupalgap_jqm_page_event_fire(event, callback) {
function drupalgap_jqm_page_event_fire(event, callback, page_arguments) {
try {
if ($.inArray(event, drupalgap.page.jqm_events) == -1 && drupalgap_function_exists(callback)) {
drupalgap.page.jqm_events.push(event);
// Concatenate the event name and the callback name together into a unique
// key so multiple callbacks can handle the same event.
var key = event + '-' + callback;
if ($.inArray(key, drupalgap.page.jqm_events) == -1 && drupalgap_function_exists(callback)) {
drupalgap.page.jqm_events.push(key);
var fn = window[callback];
fn();
if (page_arguments) {
fn.call(null, page_arguments);
}
else { fn(); }
}
}
catch (error) {
Expand Down
1 change: 1 addition & 0 deletions modules/services/drupalgap_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ drupalgap.services.drupalgap_system = {
drupalgap.entity_info = data.entity_info;
drupalgap.field_info_instances = data.field_info_instances;
drupalgap.field_info_fields = data.field_info_fields;
drupalgap.taxonomy_vocabularies = drupalgap_taxonomy_vocabularies_extract(data.taxonomy_vocabularies);
drupalgap_service_resource_extract_results({
'service':'drupalgap_system',
'resource':'connect',
Expand Down
75 changes: 59 additions & 16 deletions modules/taxonomy/taxonomy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
*
*/
function drupalgap_taxonomy_vocabularies_extract(taxonomy_vocabularies) {
try {
var results = false;
if (taxonomy_vocabularies.length > 0) {
results = {};
$.each(taxonomy_vocabularies, function(index, vocabulary){
eval('results.' + vocabulary.machine_name + ' = vocabulary;');
});
}
return results;
}
catch (error) { drupalgap_error(error); }
}

/**
* Implements hook_field_formatter_view().
*/
Expand Down Expand Up @@ -409,10 +426,10 @@ function taxonomy_vocabulary_load(vid) {
*/
function taxonomy_vocabulary_machine_name_load(name) {
try {
var options = null;
if (arguments[1]) { options = arguments[1]; }

return entity_load('taxonomy_vocabulary', vid, options);
if (drupalgap.taxonomy_vocabularies && drupalgap.taxonomy_vocabularies[name]) {
return drupalgap.taxonomy_vocabularies[name];
}
return false;
}
catch (error) { drupalgap_error(error); }
}
Expand All @@ -422,40 +439,45 @@ function taxonomy_vocabulary_machine_name_load(name) {
*/
function theme_taxonomy_term_reference(variables) {
try {
dpm(variables);
var html = '';

// Make this a hidden field since the widget will just populate a value.
variables.attributes.type = 'hidden';
html += '<input ' + drupalgap_attributes(variables.attributes) + '/>';

// What vocabulary are we using?
var vocabulary = variables.field_info_field.settings.allowed_values[0].vocabulary;
var machine_name = variables.field_info_field.settings.allowed_values[0].vocabulary;
var taxonomy_vocabulary = taxonomy_vocabulary_machine_name_load(machine_name);

// Prepare the variables for the widget and render it based on its type.
var widget_type = variables.field_info_instance.widget.type;
if (widget_type == 'options_select') { widget_type = 'select'; }
var widget_function = 'theme_' + widget_type;
var id = variables.attributes.id + '-' + widget_type;
var widget_id = variables.attributes.id + '-' + widget_type;
if (drupalgap_function_exists(widget_function)) {
var fn = window[widget_function];
html += fn.call(null, {'id':id});
// Attach a pageshow handler to the current page (if it hasn't been
// already) that will load the terms into the widget.
//drupalgap.menu_links[drupalgap_router_path_get()]
html += fn.call(null, {
attributes:{
id:widget_id,
onchange:"_theme_taxonomy_term_reference_onchange(this, '" + variables.attributes.id + "');"
}
});
// Attach a pageshow handler to the current page that will load the terms
// into the widget.
var options = {
'page_id':drupalgap_get_page_id(drupalgap_path_get()),
'jqm_page_event':'pageshow',
'jqm_page_event_callback':'_theme_taxonomy_term_reference_load_items',
'jqm_page_event_args':null
'jqm_page_event_args':JSON.stringify({
'taxonomy_vocabulary':taxonomy_vocabulary,
'widget_id':widget_id
})
};
html += drupalgap_jqm_page_event_script_code(options);
}
else {
console.log('WARNING: theme_taxonomy_term_reference() - unsupported widget type! (' + widget_type + ')');
}

console.log(html);
return html;
}
catch (error) { drupalgap_error(error); }
Expand All @@ -464,9 +486,30 @@ function theme_taxonomy_term_reference(variables) {
/**
*
*/
function _theme_taxonomy_term_reference_load_items() {
function _theme_taxonomy_term_reference_load_items(options) {
try {
alert('_theme_taxonomy_term_reference_load_items');
drupalgap.services.drupalgap_taxonomy.get_terms.call({
vid:options.taxonomy_vocabulary.vid,
success:function(terms){
if (terms.length > 0) {
$.each(terms, function(index, term){
var option = '<option value="' + term.tid + '">' + term.name + '</option>';
$('#' + options.widget_id).append(option);
});
}
}
});
}
catch (error) { drupalgap_error(error); }
}

/**
*
*/
function _theme_taxonomy_term_reference_onchange(input, id) {
try {
$('#' + id).val($(input).val());
}
catch (error) { drupalgap_error(error); }
}

0 comments on commit 7ed07b3

Please sign in to comment.