App.svelte 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <page actionBarHidden="true" class="page">
  2. <stackLayout padding="5">
  3. <label text="Farhang.im durchsuchen"></label>
  4. <textField bind:text="{term}" hint="Hier deutsch oder persisch eingeben" />
  5. <listView items="{lemmas}" style="height:100%" on:itemTap="{showLemma}">
  6. <Template let:item>
  7. {#if item.id === selectedItem.id}
  8. {#each translations as t}
  9. <flexboxLayout>
  10. <label alignSelf="flex-start" flexGrow="1" class="list-item active" text="{`${persian ? t.target : t.source}`}" textWrap="true"/>
  11. <label alignSelf="flex-end" flexGrow="1" class="list-item active" text="{`${persian ? t.source : t.target}`}" textWrap="true"/>
  12. </flexboxLayout>
  13. {/each}
  14. {:else}
  15. <label class="list-item" text="{item.lemma}" textWrap="true" />
  16. {/if}
  17. </Template>
  18. </listView>
  19. </stackLayout>
  20. </page>
  21. <script>
  22. import { Template } from 'svelte-native/components';
  23. import * as http from 'tns-core-modules/http'
  24. let term = ''
  25. let lemmas = []
  26. let translations
  27. let selectedItem = {}
  28. let persian
  29. $: if (term.length > 1) {
  30. persian = checkPersian(term)
  31. const normalized = persian ? term.replace(/([\u064B-\u0655])/g, '') : term
  32. const uri = encodeURI(`https://api.farhang.im/a/${normalized}`)
  33. http.getJSON(uri)
  34. .then(res => setLemmas(res))
  35. }
  36. $: if (term.length === 0) lemmas = []
  37. function setLemmas(res) {
  38. lemmas = res
  39. }
  40. function showLemma (lemma) {
  41. selectedItem = lemma.view.bindingContext;
  42. const uri = encodeURI(`https://api.farhang.im/g/${selectedItem.id}`)
  43. http.getJSON(uri)
  44. .then(res => {
  45. translations = res
  46. lemmas = Array.from(lemmas)
  47. })
  48. }
  49. function checkPersian (string) {
  50. const regex = /[\u0622\u0627\u0628\u067E\u062A-\u0632\u0686\u0698\u0633-\u063A\u0641\u0642\u06A9\u06AF\u0644-\u0648\u06CC]/
  51. return regex.test(string)
  52. }
  53. </script>
  54. <style>
  55. page {
  56. background-color: black;
  57. }
  58. textField {
  59. font-size: 20;
  60. color:white;
  61. placeholder-color: gray;
  62. margin: 5;
  63. }
  64. label {
  65. color: white;
  66. }
  67. .list-item {
  68. font-size: 20;
  69. color: white;
  70. /* margin-left: 20; */
  71. padding-top: 5;
  72. padding-bottom: 10;
  73. }
  74. .list-item.active {
  75. font-weight: bold;
  76. color: white;
  77. }
  78. </style>