Skip to content

Commit

Permalink
Merge pull request mozilla#5007 from timvandermeij/pdfview-tlb
Browse files Browse the repository at this point in the history
Converting PDFFindBar and PDFFindController to classes
  • Loading branch information
yurydelendik committed Aug 5, 2014
2 parents fa53fcb + dbe2247 commit df8d257
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 422 deletions.
10 changes: 5 additions & 5 deletions web/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals RenderingStates, PDFView, PDFHistory, PDFFindBar, PDFJS, mozL10n,
CustomStyle, PresentationMode, scrollIntoView, SCROLLBAR_PADDING,
CSS_UNITS, UNKNOWN_SCALE, DEFAULT_SCALE, getOutputScale,
TextLayerBuilder, cache, Stats */
/* globals RenderingStates, PDFView, PDFHistory, PDFJS, mozL10n, CustomStyle,
PresentationMode, scrollIntoView, SCROLLBAR_PADDING, CSS_UNITS,
UNKNOWN_SCALE, DEFAULT_SCALE, getOutputScale, TextLayerBuilder,
cache, Stats */

'use strict';

Expand Down Expand Up @@ -272,7 +272,7 @@ var PageView = function pageView(container, id, scale,

case 'Find':
if (!PDFView.supportsIntegratedFind) {
PDFFindBar.toggle();
PDFView.findBar.toggle();
}
break;

Expand Down
210 changes: 101 additions & 109 deletions web/pdf_find_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,36 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFFindController, FindStates, mozL10n */
/* globals FindStates, mozL10n */

'use strict';

/**
* Creates a "search bar" given set of DOM elements
* that act as controls for searching, or for setting
* search preferences in the UI. This object also sets
* up the appropriate events for the controls. Actual
* searching is done by PDFFindController
* Creates a "search bar" given a set of DOM elements that act as controls
* for searching or for setting search preferences in the UI. This object
* also sets up the appropriate events for the controls. Actual searching
* is done by PDFFindController.
*/
var PDFFindBar = {
opened: false,
bar: null,
toggleButton: null,
findField: null,
highlightAll: null,
caseSensitive: null,
findMsg: null,
findStatusIcon: null,
findPreviousButton: null,
findNextButton: null,

initialize: function(options) {
if(typeof PDFFindController === 'undefined' || PDFFindController === null) {
throw 'PDFFindBar cannot be initialized ' +
'without a PDFFindController instance.';
var PDFFindBar = (function PDFFindBarClosure() {
function PDFFindBar(options) {
this.opened = false;
this.bar = options.bar || null;
this.toggleButton = options.toggleButton || null;
this.findField = options.findField || null;
this.highlightAll = options.highlightAllCheckbox || null;
this.caseSensitive = options.caseSensitiveCheckbox || null;
this.findMsg = options.findMsg || null;
this.findStatusIcon = options.findStatusIcon || null;
this.findPreviousButton = options.findPreviousButton || null;
this.findNextButton = options.findNextButton || null;
this.findController = options.findController || null;

if (this.findController === null) {
throw new Error('PDFFindBar cannot be used without a ' +
'PDFFindController instance.');
}

this.bar = options.bar;
this.toggleButton = options.toggleButton;
this.findField = options.findField;
this.highlightAll = options.highlightAllCheckbox;
this.caseSensitive = options.caseSensitiveCheckbox;
this.findMsg = options.findMsg;
this.findStatusIcon = options.findStatusIcon;
this.findPreviousButton = options.findPreviousButton;
this.findNextButton = options.findNextButton;

// Add event listeners to the DOM elements.
var self = this;
this.toggleButton.addEventListener('click', function() {
self.toggle();
Expand All @@ -74,9 +65,9 @@ var PDFFindBar = {
}
});

this.findPreviousButton.addEventListener('click',
function() { self.dispatchEvent('again', true); }
);
this.findPreviousButton.addEventListener('click', function() {
self.dispatchEvent('again', true);
});

this.findNextButton.addEventListener('click', function() {
self.dispatchEvent('again', false);
Expand All @@ -89,86 +80,87 @@ var PDFFindBar = {
this.caseSensitive.addEventListener('click', function() {
self.dispatchEvent('casesensitivitychange');
});
},

dispatchEvent: function(aType, aFindPrevious) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('find' + aType, true, true, {
query: this.findField.value,
caseSensitive: this.caseSensitive.checked,
highlightAll: this.highlightAll.checked,
findPrevious: aFindPrevious
});
return window.dispatchEvent(event);
},

updateUIState: function(state, previous) {
var notFound = false;
var findMsg = '';
var status = '';

switch (state) {
case FindStates.FIND_FOUND:
break;

case FindStates.FIND_PENDING:
status = 'pending';
break;

case FindStates.FIND_NOTFOUND:
findMsg = mozL10n.get('find_not_found', null, 'Phrase not found');
notFound = true;
break;

case FindStates.FIND_WRAPPED:
if (previous) {
findMsg = mozL10n.get('find_reached_top', null,
'Reached top of document, continued from bottom');
} else {
findMsg = mozL10n.get('find_reached_bottom', null,
'Reached end of document, continued from top');
}
break;
}
}

if (notFound) {
this.findField.classList.add('notFound');
} else {
this.findField.classList.remove('notFound');
}
PDFFindBar.prototype = {
dispatchEvent: function PDFFindBar_dispatchEvent(type, findPrev) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('find' + type, true, true, {
query: this.findField.value,
caseSensitive: this.caseSensitive.checked,
highlightAll: this.highlightAll.checked,
findPrevious: findPrev
});
return window.dispatchEvent(event);
},

updateUIState: function PDFFindBar_updateUIState(state, previous) {
var notFound = false;
var findMsg = '';
var status = '';

switch (state) {
case FindStates.FIND_FOUND:
break;

this.findField.setAttribute('data-status', status);
this.findMsg.textContent = findMsg;
},
case FindStates.FIND_PENDING:
status = 'pending';
break;

open: function() {
if (!this.opened) {
this.opened = true;
this.toggleButton.classList.add('toggled');
this.bar.classList.remove('hidden');
}
case FindStates.FIND_NOTFOUND:
findMsg = mozL10n.get('find_not_found', null, 'Phrase not found');
notFound = true;
break;

this.findField.select();
this.findField.focus();
},
case FindStates.FIND_WRAPPED:
if (previous) {
findMsg = mozL10n.get('find_reached_top', null,
'Reached top of document, continued from bottom');
} else {
findMsg = mozL10n.get('find_reached_bottom', null,
'Reached end of document, continued from top');
}
break;
}

close: function() {
if (!this.opened) {
return;
}
this.opened = false;
this.toggleButton.classList.remove('toggled');
this.bar.classList.add('hidden');
if (notFound) {
this.findField.classList.add('notFound');
} else {
this.findField.classList.remove('notFound');
}

PDFFindController.active = false;
},
this.findField.setAttribute('data-status', status);
this.findMsg.textContent = findMsg;
},

toggle: function() {
if (this.opened) {
this.close();
} else {
this.open();
open: function PDFFindBar_open() {
if (!this.opened) {
this.opened = true;
this.toggleButton.classList.add('toggled');
this.bar.classList.remove('hidden');
}
this.findField.select();
this.findField.focus();
},

close: function PDFFindBar_close() {
if (!this.opened) {
return;
}
this.opened = false;
this.toggleButton.classList.remove('toggled');
this.bar.classList.add('hidden');
this.findController.active = false;
},

toggle: function PDFFindBar_toggle() {
if (this.opened) {
this.close();
} else {
this.open();
}
}
}
};
};
return PDFFindBar;
})();

Loading

0 comments on commit df8d257

Please sign in to comment.