Skip to content

Commit

Permalink
Merge pull request #34271 from dimagi/jls/web-apps-requirejs
Browse files Browse the repository at this point in the history
Migrate Web Apps to RequireJS
  • Loading branch information
orangejenny authored Apr 24, 2024
2 parents b88782e + f4ec1ce commit 2fac97b
Show file tree
Hide file tree
Showing 87 changed files with 1,488 additions and 923 deletions.
36 changes: 23 additions & 13 deletions corehq/apps/app_manager/static/app_manager/js/preview_app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
"use strict";
hqDefine('app_manager/js/preview_app', function () {
'use strict';
hqDefine('app_manager/js/preview_app', [
'jquery',
'analytix/js/google',
'analytix/js/kissmetrix',
'app_manager/js/app_manager_utils',
'hqwebapp/js/layout',
], function (
$,
googleAnalytics,
kissAnalytics,
appManagerUtils,
layoutController
) {
var module = {};
var _private = {};

Expand Down Expand Up @@ -38,8 +49,8 @@ hqDefine('app_manager/js/preview_app', function () {
$(module.SELECTORS.PREVIEW_ACTION_TEXT_HIDE).removeClass('hide');

if (triggerAnalytics) {
hqImport('analytix/js/kissmetrix').track.event("[app-preview] Clicked Show App Preview");
hqImport('analytix/js/google').track.event("App Preview", "Clicked Show App Preview");
kissAnalytics.track.event("[app-preview] Clicked Show App Preview");
googleAnalytics.track.event("App Preview", "Clicked Show App Preview");
}

var $offsetContainer = (_private.isFormdesigner) ? $(module.SELECTORS.FORMDESIGNER) : $(module.SELECTORS.APP_MANAGER_BODY);
Expand All @@ -60,8 +71,8 @@ hqDefine('app_manager/js/preview_app', function () {
}

if (triggerAnalytics) {
hqImport('analytix/js/kissmetrix').track.event("[app-preview] Clicked Hide App Preview");
hqImport('analytix/js/google').track.event("App Preview", "Clicked Hide App Preview");
kissAnalytics.track.event("[app-preview] Clicked Hide App Preview");
googleAnalytics.track.event("App Preview", "Clicked Hide App Preview");
}
};

Expand All @@ -72,7 +83,7 @@ hqDefine('app_manager/js/preview_app', function () {
_private.triggerPreviewEvent('tablet-view');

if (triggerAnalytics) {
hqImport('analytix/js/kissmetrix').track.event('[app-preview] User turned on tablet mode');
kissAnalytics.track.event('[app-preview] User turned on tablet mode');
}
};

Expand All @@ -83,7 +94,7 @@ hqDefine('app_manager/js/preview_app', function () {
_private.triggerPreviewEvent('phone-view');

if (triggerAnalytics) {
hqImport('analytix/js/kissmetrix').track.event('[app-preview] User turned off tablet mode');
kissAnalytics.track.event('[app-preview] User turned off tablet mode');
}
};

Expand Down Expand Up @@ -150,8 +161,7 @@ hqDefine('app_manager/js/preview_app', function () {

module.initPreviewWindow = function () {

var layoutController = hqImport("hqwebapp/js/layout"),
$appPreview = $(module.SELECTORS.PREVIEW_WINDOW),
var $appPreview = $(module.SELECTORS.PREVIEW_WINDOW),
$appBody = $(module.SELECTORS.APP_MANAGER_BODY),
$togglePreviewBtn = $(module.SELECTORS.BTN_TOGGLE_PREVIEW),
$iframe = $(module.SELECTORS.PREVIEW_WINDOW_IFRAME),
Expand Down Expand Up @@ -220,10 +230,10 @@ hqDefine('app_manager/js/preview_app', function () {
$('.js-preview-refresh').click(function () {
$(module.SELECTORS.BTN_REFRESH).removeClass('app-out-of-date');
_private.triggerPreviewEvent('refresh');
hqImport('analytix/js/kissmetrix').track.event("[app-preview] Clicked Refresh App Preview");
hqImport('analytix/js/google').track.event("App Preview", "Clicked Refresh App Preview");
kissAnalytics.track.event("[app-preview] Clicked Refresh App Preview");
googleAnalytics.track.event("App Preview", "Clicked Refresh App Preview");
});
hqImport("app_manager/js/app_manager_utils").handleAjaxAppChange(function () {
appManagerUtils.handleAjaxAppChange(function () {
$(module.SELECTORS.BTN_REFRESH).addClass('app-out-of-date');
});
var onload = function () {
Expand Down
26 changes: 21 additions & 5 deletions corehq/apps/cloudcare/static/cloudcare/js/debugger/debugger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
'use strict';
/* globals ace, Clipboard */
hqDefine('cloudcare/js/debugger/debugger', function () {
var kissmetrics = hqImport("analytix/js/kissmetrix"),
readableForm = hqImport("reports/js/readable_form");

hqDefine('cloudcare/js/debugger/debugger', [
'jquery',
'knockout',
'underscore',
'clipboard/dist/clipboard',
'ace-builds/src-min-noconflict/ace',
'analytix/js/kissmetrix',
'reports/js/readable_form',
'hqwebapp/js/atwho', // $.atwho
'ace-builds/src-min-noconflict/mode-json',
'ace-builds/src-min-noconflict/mode-xml',
'ace-builds/src-min-noconflict/ext-searchbox',
], function (
$,
ko,
_,
Clipboard,
ace,
kissmetrics,
readableForm
) {
/**
* These define tabs that are availabe in the debugger.
* {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
hqDefine("cloudcare/js/form_entry/const", function () {
hqDefine("cloudcare/js/form_entry/const", [], function () {
return {
GROUP_TYPE: 'sub-group',
REPEAT_TYPE: 'repeat-juncture',
Expand Down
44 changes: 34 additions & 10 deletions corehq/apps/cloudcare/static/cloudcare/js/form_entry/entries.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
'use strict';
/* globals moment, SignaturePad, DOMPurify */
hqDefine("cloudcare/js/form_entry/entries", function () {
var kissmetrics = hqImport("analytix/js/kissmetrix"),
cloudcareUtils = hqImport("cloudcare/js/utils"),
constants = hqImport("cloudcare/js/form_entry/const"),
formEntryUtils = hqImport("cloudcare/js/form_entry/utils"),
initialPageData = hqImport("hqwebapp/js/initial_page_data"),
toggles = hqImport("hqwebapp/js/toggles");

hqDefine("cloudcare/js/form_entry/entries", [
'jquery',
'knockout',
'underscore',
'DOMPurify/dist/purify.min',
'moment',
'fast-levenshtein/levenshtein',
'hqwebapp/js/initial_page_data',
'hqwebapp/js/toggles',
'analytix/js/kissmetrix',
'cloudcare/js/utils',
'cloudcare/js/form_entry/const',
'cloudcare/js/form_entry/utils',
'signature_pad/dist/signature_pad.umd.min',
'mapbox.js/dist/mapbox.uncompressed',
'cloudcare/js/formplayer/utils/calendar-picker-translations', // EthiopianDateEntry
'select2/dist/js/select2.full.min',
], function (
$,
ko,
_,
DOMPurify,
moment,
Levenshtein,
initialPageData,
toggles,
kissmetrics,
cloudcareUtils,
constants,
formEntryUtils,
SignaturePad,
L
) {
/**
* The base Object for all entries. Each entry takes a question object
* @param {Object} question - A question object
Expand Down Expand Up @@ -677,7 +701,7 @@ hqDefine("cloudcare/js/form_entry/entries", function () {
var isFuzzyMatch = function (haystack, query, distanceThreshold) {
return (
haystack === query ||
(query.length > 3 && window.Levenshtein.get(haystack, query) <= distanceThreshold)
(query.length > 3 && Levenshtein.get(haystack, query) <= distanceThreshold)
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
hqDefine("cloudcare/js/form_entry/errors", function () {
hqDefine("cloudcare/js/form_entry/errors", [], function () {
return {
GENERIC_ERROR: gettext("Something unexpected went wrong on that request. " +
"If you have problems filling in the rest of your form please submit an issue. " +
Expand Down
32 changes: 25 additions & 7 deletions corehq/apps/cloudcare/static/cloudcare/js/form_entry/form_ui.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
'use strict';
/* global DOMPurify */
hqDefine("cloudcare/js/form_entry/form_ui", function () {
var markdown = hqImport("cloudcare/js/markdown"),
constants = hqImport("cloudcare/js/form_entry/const"),
entries = hqImport("cloudcare/js/form_entry/entries"),
formEntryUtils = hqImport("cloudcare/js/form_entry/utils");
hqDefine("cloudcare/js/form_entry/form_ui", [
'jquery',
'knockout',
'underscore',
'DOMPurify/dist/purify.min',
'hqwebapp/js/toggles',
'cloudcare/js/markdown',
'cloudcare/js/utils',
'cloudcare/js/form_entry/const',
'cloudcare/js/form_entry/entries',
'cloudcare/js/form_entry/utils',
'jquery-tiny-pubsub/dist/ba-tiny-pubsub', // $.pubsub
], function (
$,
ko,
_,
DOMPurify,
toggles,
markdown,
cloudcareUtils,
constants,
entries,
formEntryUtils
) {
var groupNum = 0;

_.delay(function () {
Expand Down Expand Up @@ -391,7 +409,7 @@ hqDefine("cloudcare/js/form_entry/form_ui", function () {
self.blockSubmit = ko.observable(false);
self.hasSubmitAttempted = ko.observable(false);
self.isSubmitting = ko.observable(false);
self.isAnchoredSubmitStyle = hqImport('hqwebapp/js/toggles').toggleEnabled('WEB_APPS_ANCHORED_SUBMIT');
self.isAnchoredSubmitStyle = toggles.toggleEnabled('WEB_APPS_ANCHORED_SUBMIT');
self.submitClass = constants.FULL_WIDTH + ' text-center' +
(self.isAnchoredSubmitStyle ? ' anchored-submit' : ' nonanchored-submit');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
'use strict';
/* eslint-env mocha */
/* globals moment */
hqDefine("cloudcare/js/form_entry/spec/entries_spec", function () {
hqDefine("cloudcare/js/form_entry/spec/entries_spec", [
"underscore",
"sinon/pkg/sinon",
"moment",
"hqwebapp/js/initial_page_data",
"cloudcare/js/form_entry/const",
"cloudcare/js/form_entry/entries",
"cloudcare/js/form_entry/form_ui",
"cloudcare/js/utils",
], function (
_,
sinon,
moment,
initialPageData,
constants,
entries,
formUI,
utils
) {
describe('Entries', function () {
var constants = hqImport("cloudcare/js/form_entry/const"),
entries = hqImport("cloudcare/js/form_entry/entries"),
formUI = hqImport("cloudcare/js/form_entry/form_ui"),
utils = hqImport("cloudcare/js/utils"),
questionJSON,
var questionJSON,
spy;

before(function () {
hqImport("hqwebapp/js/initial_page_data").register(
initialPageData.register(
"has_geocoder_privs",
true
);
hqImport("hqwebapp/js/initial_page_data").register(
initialPageData.register(
"toggles_dict",
{
WEB_APPS_UPLOAD_QUESTIONS: true,
Expand All @@ -25,7 +38,7 @@ hqDefine("cloudcare/js/form_entry/spec/entries_spec", function () {
});

after(function () {
hqImport("hqwebapp/js/initial_page_data").unregister("toggles_dict");
initialPageData.unregister("toggles_dict");
});

beforeEach(function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';
hqDefine("cloudcare/js/form_entry/spec/fixtures", function () {
hqDefine("cloudcare/js/form_entry/spec/fixtures", [
"underscore",
], function (
_
) {
return {
textJSON: (options = {}) => (_.defaults(options, {
"caption_audio": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
'use strict';
/* eslint-env mocha */
hqDefine("cloudcare/js/form_entry/spec/form_ui_spec", function () {
hqDefine("cloudcare/js/form_entry/spec/form_ui_spec", [
"underscore",
"sinon/pkg/sinon",
"hqwebapp/js/initial_page_data",
"cloudcare/js/form_entry/const",
"cloudcare/js/form_entry/form_ui",
"cloudcare/js/form_entry/spec/fixtures",
], function (
_,
sinon,
initialPageData,
constants,
formUI,
fixtures
) {
describe('Fullform formUI', function () {
var constants = hqImport("cloudcare/js/form_entry/const"),
formUI = hqImport("cloudcare/js/form_entry/form_ui"),
fixtures = hqImport("cloudcare/js/form_entry/spec/fixtures"),
questionJSON,
var questionJSON,
formJSON,
groupJSON,
noQuestionGroupJSON,
Expand All @@ -15,7 +26,7 @@ hqDefine("cloudcare/js/form_entry/spec/form_ui_spec", function () {
repeatNestJSON;

before(function () {
hqImport("hqwebapp/js/initial_page_data").register(
initialPageData.register(
"toggles_dict",
{
WEB_APPS_UPLOAD_QUESTIONS: true,
Expand All @@ -25,7 +36,7 @@ hqDefine("cloudcare/js/form_entry/spec/form_ui_spec", function () {
});

after(function () {
hqImport("hqwebapp/js/initial_page_data").unregister("toggles_dict");
initialPageData.unregister("toggles_dict");
});

beforeEach(function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
'use strict';
/* eslint-env mocha */
hqDefine("cloudcare/js/form_entry/spec/integration_spec", function () {
hqDefine("cloudcare/js/form_entry/spec/integration_spec", [
"underscore",
"hqwebapp/js/initial_page_data",
"sinon/pkg/sinon",
"cloudcare/js/form_entry/const",
"cloudcare/js/form_entry/form_ui",
], function (
_,
initialPageData,
sinon,
constants,
formUI
) {
describe('Integration', function () {
var constants = hqImport("cloudcare/js/form_entry/const"),
formUI = hqImport("cloudcare/js/form_entry/form_ui"),
formJSON,
var formJSON,
questionJSONMulti,
questionJSONString;

before(function () {
hqImport("hqwebapp/js/initial_page_data").register("toggles_dict", { WEB_APPS_ANCHORED_SUBMIT: false });
initialPageData.register("toggles_dict", { WEB_APPS_ANCHORED_SUBMIT: false });
});

after(function () {
hqImport("hqwebapp/js/initial_page_data").unregister("toggles_dict");
initialPageData.unregister("toggles_dict");
});

beforeEach(function () {
Expand Down Expand Up @@ -74,7 +84,6 @@ hqDefine("cloudcare/js/form_entry/spec/integration_spec", function () {
this.clock.restore();
});


it('Should reconcile questions answered at the same time for strings', function () {
var questionJSONString2 = {};
$.extend(questionJSONString2, questionJSONString);
Expand Down
Loading

0 comments on commit 2fac97b

Please sign in to comment.