App.svelte 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <page actionBarHidden="true">
  2. <stackLayout padding="5">
  3. <label text="Farhang.im durchsuchen"></label>
  4. <textField bind:text="{term}" hint="Deutsch oder persisch" />
  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="iii{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. http.getJSON(`https://api.farhang.im/a/${normalized}`)
  33. .then(res => setLemmas(res))
  34. }
  35. $: if (term.length === 0) lemmas = []
  36. function setLemmas(res) {
  37. lemmas = res
  38. }
  39. function showLemma (lemma) {
  40. selectedItem = lemma.view.bindingContext;
  41. http.getJSON(`https://api.farhang.im/g/${selectedItem.id}`)
  42. .then(res => {
  43. translations = res
  44. lemmas = Array.from(lemmas)
  45. })
  46. }
  47. function checkPersian (string) {
  48. const regex = /[\u0622\u0627\u0628\u067E\u062A-\u0632\u0686\u0698\u0633-\u063A\u0641\u0642\u06A9\u06AF\u0644-\u0648\u06CC]/
  49. return regex.test(string)
  50. }
  51. </script>
  52. <style>
  53. textField {
  54. font-size: 20;
  55. color:black;
  56. margin: 5;
  57. }
  58. .list-item {
  59. font-size: 20;
  60. color: black;
  61. /* margin-left: 20; */
  62. padding-top: 5;
  63. padding-bottom: 10;
  64. }
  65. .list-item.active {
  66. font-weight: bold;
  67. color: black;
  68. }
  69. </style>