Index: www/sites/all/themes/scan/scan.js =================================================================== --- www/sites/all/themes/scan/scan.js (revision 34283) +++ www/sites/all/themes/scan/scan.js (working copy) @@ -179,76 +179,26 @@ Drupal.behaviors.npScanDestinations = function(context) { $('#edit-destinations-textarea-wrapper:not(.wordSelector-processed)', context).each(function() { - var type = 'regular', keywordsSelector, widgetSnippet; + var type = 'regular'; + var destinationsSelector, widgetSnippet, prepareWidgetSnippet; - // Prepares the widget snippet code. - var prepareWidgetSnippet = function () { - // Widget embed codes are only for the Examiner Promotional group. - // @todo #2391 Add a checkbox to group node type that enables embed code on scans. - if (Drupal.settings.og.group_context.nid != '17867') { - return; - } - - var template, pageId, input, embed, eventHandler; - var $online = $(':radio:checked[value=1]', keywordsSelector.elements); - - // Check for online Page Ids - if ($online.length) { - // Gather the Page Ids. - pageId = $online.map(function () { - return this.name; - }).get(0); - - // Apply the template. - var path = Drupal.settings.NP.apiPath.replace(/\/+$/, ''); - template = Drupal.theme('widgetSnippet', pageId, Drupal.settings.NP.gid, path); - - // Add to the DOM. - if (widgetSnippet && widgetSnippet.length) { - // Replace snippet code. - widgetSnippet.replaceWith(template); - } - else { - // Add snippet code. - keywordsSelector.widget.children('.span-36').append(template); - } - - // Access the DOM elements. - widgetSnippet = $('#widget-snippet', keywordsSelector.widget); - input = $('input', widgetSnippet); - embed = input.val(); - - // Handle events that cause the embed code to be selected. - eventHandler = function() { - // Also restore the embed code incase it got broken - input.val(embed); - input.select(); - // Stop the event from bubbling. - return false; - }; - - // Bind to events. - input.focus(eventHandler); - input.change(eventHandler); - input.click(eventHandler); - } - else if (widgetSnippet && widgetSnippet.length) { - // Remove the snippet, as there are no online Page Ids. - widgetSnippet.remove(); - widgetSnippet = null; - } - }; - - keywordsSelector = new Drupal.wordSelector({ + var params = { textarea: $('.form-textarea' ,this), content_type: 'content', selector_type: type, widgetTheme: 'destinationSelectorWidget', itemTheme: 'destinationSelectorItem', + itemCallback: 'checkDestinationUnused', title: 'Destination', label: 'Add a page ID or a Page Name to put the scan online', description: 'This scan is shown on these pages:(at least one is required to make the scan online)', defaults: ['0', '0', '0'], + addHandler: function() { + prepareWidgetSnippet(); + }, + removeHandler: function() { + prepareWidgetSnippet(); + }, radioChangedHandler: function(value, item) { prepareWidgetSnippet(); // change online/offline state @@ -259,8 +209,77 @@ $('.regularSelector-title', item).removeClass('color-13'); } } - }); + } + // Examiner2 topics + if (Drupal.settings.og.group_context.nid == '17867') { + // Default to online. + params.defaults = ['', '0', '0', '1']; + // Simplified widget theme. + params.widgetTheme = 'destinationSelectorWidgetEx2Topics'; + // Hey, cool, there was already code for this. + params.limit = 1; + // Different label. + params.label = 'Add a page ID (required to generate the widget code, no spaces, keep it simple)'; + // Embed codes. + prepareWidgetSnippet = function () { + var template, pageId, input, embed, eventHandler; + var $online = $(':radio:checked[value=1]', destinationsSelector.elements); + + // Check for online Page Ids + if ($online.length) { + // Gather the Page Ids. + pageId = $online.map(function () { + return this.name; + }).get(0); + + // Apply the template. + var path = Drupal.settings.NP.apiPath.replace(/\/+$/, ''); + template = Drupal.theme('widgetSnippet', pageId, Drupal.settings.NP.gid, path); + + // Add to the DOM. + if (widgetSnippet && widgetSnippet.length) { + // Replace snippet code. + widgetSnippet.replaceWith(template); + } + else { + // Add snippet code. + destinationsSelector.widget.children('.span-36').append(template); + } + + // Access the DOM elements. + widgetSnippet = $('#widget-snippet', destinationsSelector.widget); + input = $('input', widgetSnippet); + embed = input.val(); + + // Handle events that cause the embed code to be selected. + eventHandler = function() { + // Also restore the embed code incase it got broken + input.val(embed); + input.select(); + // Stop the event from bubbling. + return false; + }; + + // Bind to events. + input.focus(eventHandler); + input.change(eventHandler); + input.click(eventHandler); + } + else if (widgetSnippet && widgetSnippet.length) { + // Remove the snippet, as there are no online Page Ids. + widgetSnippet.remove(); + widgetSnippet = null; + } + }; + } + else { + // No customization for other groups. + prepareWidgetSnippet = function () {}; + } + + destinationsSelector = new Drupal.wordSelector(params); + prepareWidgetSnippet(); $('.label', this).remove(); $(this).addClass('word-selector').addClass(type + '-selector'); Index: www/sites/all/modules/custom/np_potpourri/np_potpourri.module =================================================================== --- www/sites/all/modules/custom/np_potpourri/np_potpourri.module (revision 34283) +++ www/sites/all/modules/custom/np_potpourri/np_potpourri.module (working copy) @@ -366,3 +366,18 @@ function np_potpourri_filter_hashtags($item) { return substr($item, 0, 1) == '#'; } + +/** + * Get the channel list for the examiner2 channel selector. + */ +function np_potpourri_ex2_channels() { + static $channels = array(); + if (!empty($channels)) { + return $channels; + } + $result = db_query('SELECT n.nid, n.title FROM {node} n INNER JOIN {og_ancestry} a ON n.nid = a.nid WHERE a.group_nid = %d', 40169); + while ($row = db_fetch_object($result)) { + $channels[$row->nid] = preg_replace('/^Examiner /', '', $row->title); + } + return $channels; +} \ No newline at end of file Index: www/sites/all/modules/custom/np_potpourri/wordSelector.js =================================================================== --- www/sites/all/modules/custom/np_potpourri/wordSelector.js (revision 34283) +++ www/sites/all/modules/custom/np_potpourri/wordSelector.js (working copy) @@ -92,6 +92,8 @@ locked: false, radioChangedHandler: function() {}, + addHandler: function() {}, + removeHandler: function() {}, /** * Adds an element to the list of already selected items. @@ -106,6 +108,11 @@ if ((this.limit == 0 || this.limit > this.data.length) && this.isContained(data) === false) { var that = this; + // Disable stuff if we hit the limit. + if (this.limit == (this.data.length + 1)) { + $('.hide-at-limit', this.widget).hide(); + } + this.addElementToView(data); // Display elements container if necessary @@ -125,6 +132,7 @@ } this.updateElements(this.currentIndex); } + this.addHandler(); } // clean default value from text field if it's still there @@ -244,12 +252,17 @@ // Remove the item from the list of added items. $(item).remove(); + // Enable any disabled widgets + // @@@ Just do this when going from = limit to < limit? + $('.hide-at-limit', this.widget).show(); + // If we remove first elements we need to readd first class $(this.elements).children(':first-child').addClass('first'); // Put the focus on the textfield again. this.textfield.focus(); + this.removeHandler(); this.updatePreview(); $(this.textarea).trigger('removedElement', [ data ]); @@ -391,6 +404,8 @@ * */ keyupTextfield: function() { + // Clear status + $('div.status', this.widget).empty(); // Hide/Display Add button if (this.textfield.val() && this.textfield.val().length) { @@ -407,36 +422,47 @@ */ keydownTextfield: function(e) { if (e.keyCode == 13) { // enter key - var that = this; - var data = []; - $.each(this.textfield, function (i, val) { - if ($('option:first', that.textfield).length > 0) { - data[i] = []; - data[i][0] = $('option:selected', val).val(); - data[i][1] = $('option:selected', val).text(); - } - else if ($(val).val().length || $(val).hasClass('required') == false) { - var inputData = $.map($(val).val().split(","), $.trim); - $.each(inputData, function(j, ival) { - data[j] = data[j] || []; - data[j][i] = ival; - }); - // Reset the textfield and execute the default action. - $(val).val(''); - } - }); - $.each(data, function () { - // if we have defaults merge it with data from textfield and add - if (that.defaults) { - var element = this; - element = element.concat(that.defaults); - that.addElement(element, false); - } - else { - that.addElement(this, false); - } - }); - this.keyupTextfield(); + var _callback = function (e) { + var that = this; + var data = []; + $.each(this.textfield, function (i, val) { + if ($('option:first', that.textfield).length > 0) { + data[i] = []; + data[i][0] = $('option:selected', val).val(); + data[i][1] = $('option:selected', val).text(); + } + else if ($(val).val().length || $(val).hasClass('required') == false) { + var inputData = $.map($(val).val().split(","), $.trim); + $.each(inputData, function(j, ival) { + data[j] = data[j] || []; + data[j][i] = ival; + }); + // Reset the textfield and execute the default action. + $(val).val(''); + } + }); + $.each(data, function () { + // if we have defaults merge it with data from textfield and add + if (that.defaults) { + var element = this; + element = element.concat(that.defaults); + that.addElement(element, false); + } + else { + that.addElement(this, false); + } + }); + this.keyupTextfield(); + }; + + if (this.itemCallback) { + // itemCallback is name of function in NP namespace. (Yeah, cheating a bit here) + NP[this.itemCallback].call(this, e, _callback); + } + else { + _callback.call(this, e); + } + // Return false so that the actual key event does not bubble further. return false; } @@ -704,17 +730,18 @@ '' + '' + '' + - '
' + + '
' + '' + - '' + + '' + '
' + - '
' + + '
' + '' + '' + '
' + '
' + 'Add' + '
' + + '
' + // @@@ Bevan help me fix this. '
' + '
' ); @@ -729,6 +756,46 @@ }; /** + * Theme function for destinations. + */ +Drupal.theme.prototype.destinationSelectorWidgetEx2Topics = function(class_prefix, title, label, description, selector) { + var class_elements = class_prefix + "Selector-elements"; + var ul_class_index = class_prefix + "Selector-indexes"; + + var widget = $( + '
' + + '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
'+ description +'OnlineOffline
' + + '
' + + '
' + + '' + + '' + + '
' + + '
' + + 'Add' + + '
' + + '
' + // @@@ Bevan help me fix this. + '
' + + '
' + ); + return { + widget: widget, + indexWrapper: $('.' + class_prefix + 'Selector-index-wrapper', widget), + elementsWrapper: $('.' + class_prefix + 'Selector-elements-wrapper', widget), + elements: $('.' + class_prefix + 'Selector-elements', widget), + textfield: $('.' + class_prefix + 'Selector-textfield', widget), + addButton: $('.' + class_prefix + 'Selector-add-btn', widget) + }; +}; + +/** * Theme function for an autocomplete item. * * @param data @@ -1340,3 +1407,23 @@ } } }; + +NP.checkDestinationUnused = function(e, callback) { + var that = this; + var page_id = $('#page-id', that.widget).val(); + $.getJSON(Drupal.settings.basePath +'np_scan/prevalidate/'+ Drupal.settings.NP.gid +'/'+ page_id, function(response) { + var status = $('div.status', that.widget); + if (response.error) { + status.html(''+ Drupal.theme('icon', '02', 'warning', 'icon-va-middle') + response.error +''); + return; + } + if (response.found == true) { + status.html(''+ Drupal.theme('icon', '02', 'warning', 'icon-va-middle') + 'That page ID is already in use, please choose another'); + return; + } + else { + callback.call(that, e); + } + }); +} +