Commit 24809d40 authored by Romain Courteaud's avatar Romain Courteaud Committed by Jérome Perrin

Share duplicated functions.

parent 4ca4b874
......@@ -6,6 +6,7 @@
<title>Create Document</title>
<script src="../<%= copy.rsvp.relative_dest %>" type="text/javascript"></script>
<script src="../<%= copy.renderjs.relative_dest %>" type="text/javascript"></script>
<script src="mixin_promise.js" type="text/javascript"></script>
<script src="create_document.js" type="text/javascript"></script>
</head>
<body>
......
/*global console, rJS, RSVP, FileReader */
(function (window, rJS, RSVP, FileReader) {
/*global console, rJS, RSVP, promiseEventListener, promiseReadAsText */
(function (window, rJS, RSVP, promiseEventListener, promiseReadAsText) {
"use strict";
function promiseEventListener(target, type, useCapture) {
//////////////////////////
// Resolve the promise as soon as the event is triggered
// eventListener is removed when promise is cancelled/resolved/rejected
//////////////////////////
var handle_event_callback;
function canceller() {
target.removeEventListener(type, handle_event_callback, useCapture);
}
function resolver(resolve) {
handle_event_callback = function (evt) {
canceller();
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(resolver, canceller);
}
function promiseReadAsText(file) {
return new RSVP.Promise(function (resolve, reject) {
var reader = new FileReader();
reader.onload = function (evt) {
resolve(evt.target.result);
};
reader.onerror = function (evt) {
reject(evt);
};
reader.readAsText(file);
});
}
rJS(window)
/////////////////////////////////////////////////////////////////
// Acquired methods
......@@ -121,4 +83,4 @@
});
});
}(window, rJS, RSVP, FileReader));
}(window, rJS, RSVP, promiseEventListener, promiseReadAsText));
......@@ -9,6 +9,7 @@
<script src="../<%= curl.jquery.relative_dest %>" type="text/javascript"></script>
<script src="../<%= curl.jquerymobilejs.relative_dest %>" type="text/javascript"></script>
<script src="mixin_promise.js" type="text/javascript"></script>
<script src="mixin_document_page.js" type="text/javascript"></script>
<script src="manage_document.js" type="text/javascript"></script>
</head>
......
/*global console, rJS, RSVP, initDocumentPageMixin, jQuery */
(function (window, rJS, RSVP, initDocumentPageMixin, $) {
/*global console, rJS, RSVP, initDocumentPageMixin, jQuery,
promiseEventListener */
(function (window, rJS, RSVP, initDocumentPageMixin, $, promiseEventListener) {
"use strict";
function datatouri(data, mime_type) {
......@@ -10,31 +11,6 @@
return result + ";base64," + window.btoa(data);
}
function promiseEventListener(target, type, useCapture) {
//////////////////////////
// Resolve the promise as soon as the event is triggered
// eventListener is removed when promise is cancelled/resolved/rejected
//////////////////////////
var handle_event_callback;
function canceller() {
target.removeEventListener(type, handle_event_callback, useCapture);
}
function resolver(resolve) {
handle_event_callback = function (evt) {
canceller();
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(resolver, canceller);
}
function disableAllButtons(gadget) {
// Prevent double click
var i,
......@@ -191,4 +167,4 @@
waitForKnowledgeExtraction(this)
]);
});
}(window, rJS, RSVP, initDocumentPageMixin, jQuery));
}(window, rJS, RSVP, initDocumentPageMixin, jQuery, promiseEventListener));
/*global console, RSVP, FileReader */
(function (window, RSVP, FileReader) {
"use strict";
window.promiseEventListener = function (target, type, useCapture) {
//////////////////////////
// Resolve the promise as soon as the event is triggered
// eventListener is removed when promise is cancelled/resolved/rejected
//////////////////////////
var handle_event_callback;
function canceller() {
target.removeEventListener(type, handle_event_callback, useCapture);
}
function resolver(resolve) {
handle_event_callback = function (evt) {
canceller();
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(resolver, canceller);
};
window.promiseReadAsText = function (file) {
return new RSVP.Promise(function (resolve, reject) {
var reader = new FileReader();
reader.onload = function (evt) {
resolve(evt.target.result);
};
reader.onerror = function (evt) {
reject(evt);
};
reader.readAsText(file);
});
};
}(window, RSVP, FileReader));
......@@ -14,6 +14,7 @@
<label>{{label}}</label>
</script>
<script src="mixin_promise.js" type="text/javascript"></script>
<script src="mixin_document_page.js" type="text/javascript"></script>
<script src="run_simulation.js" type="text/javascript"></script>
</head>
......
/*global rJS, RSVP, initDocumentPageMixin, jQuery, Handlebars */
/*jslint nomen: true, maxlen: 200 */
(function (window, rJS, RSVP, initDocumentPageMixin, $, Handlebars) {
/*global rJS, RSVP, initDocumentPageMixin, jQuery, Handlebars,
promiseEventListener */
/*jslint nomen: true */
(function (window, rJS, RSVP, initDocumentPageMixin, $, Handlebars,
promiseEventListener) {
"use strict";
function promiseEventListener(target, type, useCapture) {
//////////////////////////
// Resolve the promise as soon as the event is triggered
// eventListener is removed when promise is cancelled/resolved/rejected
//////////////////////////
var handle_event_callback;
function canceller() {
target.removeEventListener(type, handle_event_callback, useCapture);
}
function resolver(resolve) {
handle_event_callback = function (evt) {
canceller();
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(resolver, canceller);
}
/////////////////////////////////////////////////////////////////
// Handlebars
/////////////////////////////////////////////////////////////////
......@@ -189,4 +166,5 @@
return gadget.pleaseRedirectMyHash(url);
});
});
}(window, rJS, RSVP, initDocumentPageMixin, jQuery, Handlebars));
}(window, rJS, RSVP, initDocumentPageMixin, jQuery, Handlebars,
promiseEventListener));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment