Permalink
Browse files

#866 Abstract jQM grid out of Views to support the theme() layer as w…

…ell.
  • Loading branch information...
1 parent 937bc85 commit c3cbf112564e92463c17d4f704d4377a515a72ab @signalpoint committed Oct 19, 2016
Showing with 76 additions and 30 deletions.
  1. +1 −0 makefile
  2. +70 −0 src/includes/widget-grid.inc.js
  3. +5 −30 src/modules/views/views.js
View
@@ -16,6 +16,7 @@ files = src/dg.js\
src/includes/render.inc.js\
src/includes/theme.inc.js\
src/includes/title.inc.js\
+ src/includes/widget-grid.inc.js\
src/modules/comment/comment.js\
src/modules/contact/contact.js\
src/modules/entity/entity.js\
@@ -0,0 +1,70 @@
+function theme_jqm_grid(variables) {
+ if (!variables.items || !variables.items.length) { return ''; }
+
+ var html = '';
+
+ // Determine columns, and jqm grid type and css class..
+ var columns = jqm_grid_verify_columns(variables);
+ var grid = jqm_grid_get_type(columns);
+ variables.attributes.class += ' ui-grid-' + grid + ' ';
+
+ var open = '<div ' + drupalgap_attributes(variables.attributes) + '>';
+ var close = '</div>';
+ var open_row = '<div class="ui-block">'; // This class will be replaced dynamically.
+ var close_row = '</div>';
+
+ html += open;
+ for (var i = 0; i < variables.items.length; i++) {
+ var className = jqm_grid_get_item_class(i, columns);
+ var openRow = jqm_grid_set_item_row_class(open_row, className);
+ html += openRow + variables.items[i] + close_row;
+ }
+ html += close;
+
+ return html;
+}
+
+function jqm_grid_verify_columns(variables) {
+ var columns = variables.columns ? variables.columns : 2;
+ var msg = null;
+ if (columns < 2) {
+ msg = columns + ' columns is not enough, a minimum of 2 is needed';
+ columns = 2;
+ }
+ if (columns > 5) {
+ msg = columns + ' columns is too many enough, a maximum of 5 is allowed';
+ columns = 5;
+ }
+ variables.columns = columns;
+ if (msg) { console.log('jqm_grid_verify_columns - ' + msg); }
+ return variables.columns;
+}
+
+function jqm_grid_get_type(columns) {
+ var grid = null;
+ switch (columns) {
+ case 2: grid = 'a'; break;
+ case 3: grid = 'b'; break;
+ case 4: grid = 'c'; break;
+ case 5: grid = 'd'; break;
+ }
+ return grid;
+}
+
+function jqm_grid_get_item_class(index, columns) {
+ var className = null;
+ switch (index % columns) {
+ case 0: className = 'ui-block-a'; break;
+ case 1: className = 'ui-block-b'; break;
+ case 2: className = 'ui-block-c'; break;
+ case 3: className = 'ui-block-d'; break;
+ case 4: className = 'ui-block-e'; break;
+ }
+ return className
+}
+
+function jqm_grid_set_item_row_class(open_row, className) {
+ var openRow = JSON.parse(JSON.stringify(open_row)); // Make a copy of the string.
+ if (className) { openRow = openRow.replace('ui-block', className); }
+ return openRow;
+}
@@ -830,26 +830,9 @@ function drupalgap_views_get_result_formats(variables) {
switch (variables.format) {
case 'grid':
- // Determine how many columns to use, warn if there are any problems.
- var columns = variables.columns ? variables.columns : 2;
- var msg = null;
- if (columns < 2) {
- msg = columns + ' columns is not enough, a minimum of 2 is needed';
- columns = 2;
- }
- if (columns > 5) {
- msg = columns + ' columns is too many enough, a maximum of 5 is allowed';
- columns = 5;
- }
- variables.columns = columns;
- if (msg) { console.log('drupalgap_views_get_result_formats - ' + msg ); }
- var grid = null;
- switch (columns) {
- case 2: grid = 'a'; break;
- case 3: grid = 'b'; break;
- case 4: grid = 'c'; break;
- case 5: grid = 'd'; break;
- }
+ // Determine columns and jqm grid type.
+ var columns = jqm_grid_verify_columns(variables);
+ var grid = jqm_grid_get_type(columns);
// Prep the container class name.
if (!format_attributes.class) { format_attributes.class = ''; }
@@ -956,16 +939,8 @@ function drupalgap_views_render_rows(variables, results, root, child, open_row,
// If we're rendering a grid, replace the class name for the current column. Otherwise
// just render the row as usual.
if (variables.format == 'grid') {
- var className = null;
- switch ((row._position) % variables.columns) {
- case 0: className = 'ui-block-a'; break;
- case 1: className = 'ui-block-b'; break;
- case 2: className = 'ui-block-c'; break;
- case 3: className = 'ui-block-d'; break;
- case 4: className = 'ui-block-e'; break;
- }
- var openRow = JSON.parse(JSON.stringify(open_row)); // Make a copy of the string.
- if (className) { openRow = openRow.replace('ui-block', className); }
+ var className = jqm_grid_get_item_class(row._position, variables.columns);
+ var openRow = jqm_grid_set_item_row_class(open_row, className);
html += openRow + row_content + close_row;
}
else { html += open_row + row_content + close_row; }

0 comments on commit c3cbf11

Please sign in to comment.