Skip to content

Commit

Permalink
add storage service and use it to store form field value
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Mar 10, 2016
1 parent 4b2eeb8 commit e67e316
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var CustomStyle = displayDOMUtils.CustomStyle;
* @property {PageViewport} viewport
* @property {IPDFLinkService} linkService
* @property {DownloadManager} downloadManager
* @property {IPDFStorageService} storageService
*/

/**
Expand Down Expand Up @@ -114,6 +115,7 @@ var AnnotationElement = (function AnnotationElementClosure() {
this.viewport = parameters.viewport;
this.linkService = parameters.linkService;
this.downloadManager = parameters.downloadManager;
this.storageService = parameters.storageService;

if (isRenderable) {
this.container = this._createContainer();
Expand Down Expand Up @@ -462,7 +464,8 @@ var TextWidgetAnnotationElement =
}

content.disabled = isReadonly;
content.value = this.data.fieldValue;
content.value = this.storageService.get(this.data.fullName) ||
this.data.fieldValue;
var textAlignment = this.data.textAlignment;
content.style.textAlign = ['left', 'center', 'right'][textAlignment];
if (this.data.maxLen !== null) {
Expand All @@ -483,12 +486,18 @@ var TextWidgetAnnotationElement =
}
}

content.onchange = this._onchange.bind(this);

container.appendChild(content);

container.className = 'widgetAnnotation';
return container;
},

_onchange: function(evt) {
this.storageService.set(this.data.fullName, evt.target.value);
},

/**
* Apply text styles to the text in the element.
*
Expand Down Expand Up @@ -945,7 +954,8 @@ var AnnotationLayer = (function AnnotationLayerClosure() {
page: parameters.page,
viewport: parameters.viewport,
linkService: parameters.linkService,
downloadManager: parameters.downloadManager
downloadManager: parameters.downloadManager,
storageService: parameters.storageService
};
var element = annotationElementFactory.create(properties);
if (element.isRenderable) {
Expand Down
8 changes: 6 additions & 2 deletions web/annotation_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*globals PDFJS, mozL10n, SimpleLinkService */
/*globals PDFJS, mozL10n, SimpleLinkService, SimpleStorageService */

'use strict';

Expand All @@ -22,6 +22,7 @@
* @property {PDFPage} pdfPage
* @property {IPDFLinkService} linkService
* @property {DownloadManager} downloadManager
* @property {IPDFStorageService} storageService
*/

/**
Expand All @@ -37,6 +38,7 @@ var AnnotationLayerBuilder = (function AnnotationLayerBuilderClosure() {
this.pdfPage = options.pdfPage;
this.linkService = options.linkService;
this.downloadManager = options.downloadManager;
this.storageService = options.storageService;

this.div = null;
}
Expand All @@ -62,7 +64,8 @@ var AnnotationLayerBuilder = (function AnnotationLayerBuilderClosure() {
annotations: annotations,
page: self.pdfPage,
linkService: self.linkService,
downloadManager: self.downloadManager
downloadManager: self.downloadManager,
storageService: self.storageService
};

if (self.div) {
Expand Down Expand Up @@ -116,6 +119,7 @@ DefaultAnnotationLayerFactory.prototype = {
pageDiv: pageDiv,
pdfPage: pdfPage,
linkService: new SimpleLinkService(),
storageService: new SimpleStorageService(),
});
}
};
11 changes: 11 additions & 0 deletions web/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,14 @@ IPDFAnnotationLayerFactory.prototype = {
*/
createAnnotationLayerBuilder: function (pageDiv, pdfPage) {}
};

/**
* @interface
*/
function IPDFStorageService() {}
IPDFStorageService.prototype = {
get: function (key) {},
set: function (key, value) {},
remove: function (key) {},
removeAll: function () {},
};
28 changes: 27 additions & 1 deletion web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var DEFAULT_CACHE_SIZE = 10;
* @property {IPDFLinkService} linkService - The navigation/linking service.
* @property {DownloadManager} downloadManager - (optional) The download
* manager component.
* @property {IPDFStorageService} storageService - The storage service.
* @property {PDFRenderingQueue} renderingQueue - (optional) The rendering
* queue object.
* @property {boolean} removePageBorders - (optional) Removes the border shadow
Expand Down Expand Up @@ -95,6 +96,7 @@ var PDFViewer = (function pdfViewer() {
this.viewer = options.viewer || options.container.firstElementChild;
this.linkService = options.linkService || new SimpleLinkService();
this.downloadManager = options.downloadManager || null;
this.storageService = options.storageService || new SimpleStorageService();
this.removePageBorders = options.removePageBorders || false;

this.defaultRenderingQueue = !options.renderingQueue;
Expand Down Expand Up @@ -691,6 +693,7 @@ var PDFViewer = (function pdfViewer() {
this._pages[i].reset();
}
}
this.storageService.removeAll();
},

/**
Expand Down Expand Up @@ -761,7 +764,8 @@ var PDFViewer = (function pdfViewer() {
pageDiv: pageDiv,
pdfPage: pdfPage,
linkService: this.linkService,
downloadManager: this.downloadManager
downloadManager: this.downloadManager,
storageService: this.storageService
});
},

Expand Down Expand Up @@ -821,3 +825,25 @@ var SimpleLinkService = (function SimpleLinkServiceClosure() {
};
return SimpleLinkService;
})();

var SimpleStorageService = (function SimpleStorageServiceClosure() {
function SimpleStorageService() {
this._storage = Object.create(null);
}

SimpleStorageService.prototype = {
set: function(key, value) {
this._storage[key] = value;
},
get: function(key) {
return this._storage[name];
},
remove: function(key) {
delete this._storage[name];
},
removeAll: function(key) {
this._storage = Object.create(null);
}
};
return SimpleStorageService;
})();

0 comments on commit e67e316

Please sign in to comment.