@iebh/tera-fy 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -77,7 +77,7 @@ export default class TeraFy {
77
77
  'handshake',
78
78
 
79
79
  // Session
80
- 'getUser',
80
+ 'getUser', 'requireUser',
81
81
 
82
82
  // Projects
83
83
  'bindProject', 'getProject', 'getProjects', 'setActiveProject', 'requireProject', 'selectProject',
@@ -91,6 +91,9 @@ export default class TeraFy {
91
91
 
92
92
  // Project Libraries
93
93
  'getProjectLibrary', 'setProjectLibrary',
94
+
95
+ // UI
96
+ 'uiAlert',
94
97
  ];
95
98
 
96
99
 
@@ -615,6 +618,25 @@ export default class TeraFy {
615
618
  */
616
619
 
617
620
 
621
+ /**
622
+ * Require a user login to TERA
623
+ * If there is no user OR they are not logged in a prompt is shown to go and do so
624
+ * This is an pre-requisite step for requireProject()
625
+ *
626
+ * @function requireUser
627
+ * @returns {Promise} A promise which will resolve if the there is a user and they are logged in
628
+ */
629
+
630
+
631
+ /**
632
+ * Require a user login to TERA
633
+ * If there is no user OR they are not logged in a prompt is shown to go and do so
634
+ * This is an pre-requisite step for requireProject()
635
+ *
636
+ * @returns {Promise} A promise which will resolve if the there is a user and they are logged in
637
+ */
638
+
639
+
618
640
  /**
619
641
  * Project entry within TERA
620
642
  *
@@ -817,5 +839,20 @@ export default class TeraFy {
817
839
  *
818
840
  * @returns {Promise} A promise which resolves when the save operation has completed
819
841
  */
842
+
843
+
844
+ /**
845
+ * Display simple text within TERA
846
+ *
847
+ * @function uiAlert
848
+ * @param {String} text The text to display
849
+ *
850
+ * @param {Object} [options] Additional options to mutate behaviour
851
+ * @param {String} [options.title='TERA'] The title of the alert box
852
+ * @param {Boolean} [options.isHtml=false] If falsy the text is rendered as plain-text otherwise it will be assumed as HTML content
853
+ *
854
+ * @returns {Promise} A promise which resolves when the alert has been dismissed
855
+ */
856
+
820
857
  // }}}
821
858
  }
@@ -33,6 +33,7 @@ export default class TeraFyServer {
33
33
  static SERVERMODE_NONE = 0;
34
34
  static SERVERMODE_EMBEDDED = 1;
35
35
  static SERVERMODE_WINDOW = 2;
36
+ static SERVERMODE_TERA = 3; // Terafy is running as the main TERA site
36
37
 
37
38
 
38
39
  // Contexts - createContext(), getClientContext(), messageEvent, senderRpc() {{{
@@ -105,7 +106,7 @@ export default class TeraFyServer {
105
106
  TERA: 1,
106
107
  ...cloneDeep(message), // Need to clone to resolve promise nasties
107
108
  };
108
- iFrame.postMessage(payload, this.settings.restrictOrigin);
109
+ iFrame.contentWindow.postMessage(payload, this.settings.restrictOrigin);
109
110
  } catch (e) {
110
111
  this.debug('ERROR', 'Attempted to dispatch payload server(top level window)->cient(iframe)', {payload, e});
111
112
  throw e;
@@ -290,9 +291,9 @@ export default class TeraFyServer {
290
291
  */
291
292
  requestFocus(cb) {
292
293
  return Promise.resolve()
293
- .then(()=> this.senderRpc('toggleFocus', true))
294
+ .then(()=> this.settings.serverMode != TeraFyServer.SERVERMODE_TERA && this.senderRpc('toggleFocus', true))
294
295
  .then(()=> cb.call(this))
295
- .finally(()=> this.senderRpc('toggleFocus', false))
296
+ .finally(()=> this.settings.serverMode != TeraFyServer.SERVERMODE_TERA && this.senderRpc('toggleFocus', false))
296
297
  }
297
298
 
298
299
 
@@ -314,7 +315,7 @@ export default class TeraFyServer {
314
315
  }
315
316
  // }}}
316
317
 
317
- // Session / User - getUser() {{{
318
+ // Session / User - getUser(), requireUser() {{{
318
319
 
319
320
  /**
320
321
  * User / active session within TERA
@@ -349,6 +350,22 @@ export default class TeraFyServer {
349
350
  } : null)
350
351
  }
351
352
 
353
+
354
+ /**
355
+ * Require a user login to TERA
356
+ * If there is no user OR they are not logged in a prompt is shown to go and do so
357
+ * This is an pre-requisite step for requireProject()
358
+ *
359
+ * @returns {Promise} A promise which will resolve if the there is a user and they are logged in
360
+ */
361
+ requireUser() {
362
+ return this.getUser()
363
+ .then(user => user || this.uiAlert('You must be logged in to <a href="https://tera-tools.com" target="_blank">TERA-tools.com</a> to use this tool', {
364
+ title: 'TERA-tools account needed',
365
+ isHtml: true,
366
+ }))
367
+ }
368
+
352
369
  // }}}
353
370
 
354
371
  // Projects - getProject(), getProjects(), requireProject(), selectProject() {{{
@@ -417,6 +434,7 @@ export default class TeraFyServer {
417
434
  * Note that this function will percist in asking the uesr even if they try to cancel
418
435
  *
419
436
  * @param {Object} [options] Additional options to mutate behaviour
437
+ * @param {Boolean} [options.autoRequireUser=true] Automatically call `requireUser()` before trying to fetch a list of projects
420
438
  * @param {Boolean} [options.autoSetActiveProject=true] After selecting a project set that project as active in TERA
421
439
  * @param {String} [options.title="Select a project to work with"] The title of the dialog to display
422
440
  * @param {String} [options.noSelectTitle='Select project'] Dialog title when warning the user they need to select something
@@ -426,6 +444,7 @@ export default class TeraFyServer {
426
444
  */
427
445
  requireProject(options) {
428
446
  let settings = {
447
+ autoRequireUser: true,
429
448
  autoSetActiveProject: true,
430
449
  title: 'Select a project to work with',
431
450
  noSelectTitle: 'Select project',
@@ -433,7 +452,9 @@ export default class TeraFyServer {
433
452
  ...options,
434
453
  };
435
454
 
436
- return this.getProject()
455
+ return Promise.resolve()
456
+ .then(()=> settings.autoRequireUser && this.requireUser())
457
+ .then(()=> this.getProject())
437
458
  .then(active => {
438
459
  if (active) return active; // Use active project
439
460
 
@@ -772,6 +793,37 @@ export default class TeraFyServer {
772
793
  }
773
794
  // }}}
774
795
 
796
+ // UI - uiAlert() {{{
797
+ /**
798
+ * Display simple text within TERA
799
+ *
800
+ * @param {String} text The text to display
801
+ *
802
+ * @param {Object} [options] Additional options to mutate behaviour
803
+ * @param {String} [options.title='TERA'] The title of the alert box
804
+ * @param {Boolean} [options.isHtml=false] If falsy the text is rendered as plain-text otherwise it will be assumed as HTML content
805
+ *
806
+ * @returns {Promise} A promise which resolves when the alert has been dismissed
807
+ */
808
+ uiAlert(text, options) {
809
+ let settings = {
810
+ title: 'TERA',
811
+ isHtml: false,
812
+ ...options,
813
+ };
814
+
815
+ return this.requestFocus(()=>
816
+ app.service('$prompt').dialog({
817
+ title: settings.title,
818
+ body: text,
819
+ buttons: ['ok'],
820
+ isHtml: settings.isHtml,
821
+ dialogClose: 'resolve',
822
+ })
823
+ );
824
+ }
825
+ // }}}
826
+
775
827
  // Utility - debug() {{{
776
828
 
777
829
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/tera-fy",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "TERA website worker",
5
5
  "scripts": {
6
6
  "dev": "esbuild --platform=browser --format=esm --bundle lib/terafy.client.js --outfile=dist/terafy.js --minify --sourcemap --serve --servedir=.",