This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checking in the text replacement feature
Now in generally ES6-accepted notation.
- Loading branch information
1 parent
d097f69
commit c0ff406
Showing
28 changed files
with
406 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Ember from 'ember'; | ||
import EmberValidations from 'ember-validations'; | ||
|
||
export default Ember.Controller.extend(EmberValidations, { | ||
hideCancelButton: true, | ||
updateCapability: 'update_config', | ||
|
||
createExpansion: function() { | ||
let newExpansion = this.get('store').createRecord('text-expansion'); | ||
this.set('newExpansion', newExpansion); | ||
}.on('init'), | ||
|
||
actions: { | ||
cancelExpansion() { | ||
this.createExpansion(); | ||
} | ||
}, | ||
|
||
validations: { | ||
'newExpansion.from': { | ||
presence: true | ||
}, | ||
'newExpansion.to': { | ||
presence: true | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import AbstractIndexRoute from 'hospitalrun/routes/abstract-index-route'; | ||
import { translationMacro as t } from 'ember-i18n'; | ||
|
||
export default AbstractIndexRoute.extend({ | ||
pageTitle: t('admin.text_replacements.page_title'), | ||
hideNewButton: true, | ||
|
||
model() { | ||
let store = this.get('store'); | ||
return store.findAll('text-expansion').then((result) => { | ||
return result.filter((model) => { | ||
let isNew = model.get('isNew'); | ||
console.log(`${model.get('from')} ${isNew}`); | ||
return !isNew; | ||
}); | ||
}); | ||
}, | ||
|
||
setupController(controller, model) { | ||
this._super(controller, model); | ||
controller.createExpansion(); | ||
}, | ||
|
||
actions: { | ||
addExpansion(newExpansion) { | ||
newExpansion.save() | ||
.then(() => { | ||
this.refresh(); | ||
}) | ||
.catch(() => { | ||
this.refresh(); | ||
}); | ||
}, | ||
|
||
deleteExpansion(expansion) { | ||
expansion.deleteRecord(); | ||
expansion.save() | ||
.then(() => { | ||
this.refresh(); | ||
}) | ||
.catch(() => { | ||
this.refresh(); | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<div class="panel panel-primary"> | ||
<h3>{{t 'admin.text_replacements.existing_repl'}}</h3> | ||
<p>{{t 'admin.text_replacements.repl_desc'}}</p> | ||
<table class="table"> | ||
<tr class="table-header"> | ||
<th>{{t 'labels.from'}}</th> | ||
<th>{{t 'labels.to'}}</th> | ||
<th/> | ||
</tr> | ||
<tbody> | ||
{{#each model as |expansion|}} | ||
<tr> | ||
<td>#{{expansion.from}}</td> | ||
<td>{{expansion.to}}</td> | ||
<td> | ||
<button class="pull-right btn button-default on-white" {{action 'deleteExpansion' expansion}}>{{t 'buttons.delete'}}</button> | ||
</td> | ||
</tr> | ||
{{/each}} | ||
</tbody> | ||
</table> | ||
<div class="panel"> | ||
<div class="panel-body"> | ||
<h3>{{t 'admin.text_replacements.create_new'}}</h3> | ||
{{#em-form model=newExpansion action="addExpansion" formLayout="horizontal" showErrorsOnFocusIn="true" submitButton=false}} | ||
{{em-input property="from" label=(t 'labels.from') placeholder=(t 'admin.text_replacements.to_replace')}} | ||
{{em-input property="to" label=(t 'labels.from') placeholder=(t 'admin.text_replacements.replace_with')}} | ||
{{/em-form}} | ||
</div> | ||
<div class="panel-footer"> | ||
<button class="btn button-primary on-white" disabled={{isInvalid}} {{action 'addExpansion' newExpansion}}>{{t 'buttons.add'}}</button> | ||
<button class="btn button-default on-white" {{action 'cancelExpansion'}}>{{t 'buttons.cancel'}}</button> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import Ember from 'ember'; | ||
import textExpansion from '../utils/text-expansion'; | ||
|
||
export default Ember.Component.extend({ | ||
i18n: Ember.inject.service(), | ||
store: Ember.inject.service(), | ||
|
||
userText: '', | ||
|
||
didInsertElement() { | ||
try { | ||
let feedbackDiv = document.createElement('div'); | ||
feedbackDiv.style.position = 'absolute'; | ||
let textarea = this.$()[0].getElementsByTagName('textarea')[0]; | ||
this.set('textarea', textarea); | ||
let textPos = textarea.getBoundingClientRect(); | ||
let fbStyle = feedbackDiv.style; | ||
fbStyle.top = `${textPos.bottom}px`; | ||
fbStyle.left = `${textPos.left}px`; | ||
fbStyle.width = `${textarea.offsetWidth}px`; | ||
fbStyle.backgroundColor = 'lightyellow'; | ||
fbStyle.borderStyle = 'solid'; | ||
fbStyle.borderWidth = '1px'; | ||
fbStyle.borderRadius = '3px'; | ||
fbStyle.paddingLeft = '5px'; | ||
fbStyle.visibility = 'hidden'; | ||
|
||
this.set('feedbackDiv', feedbackDiv); | ||
this.get('feedbackText'); | ||
this.get('activeExpansionSite'); | ||
|
||
this.get('store') | ||
.findAll('text-expansion') | ||
.then((expansions) => { | ||
return expansions.reduce((prev, curr) => { | ||
// console.log(`curr ${JSON.stringify(prev)}`); | ||
prev[curr.get('from')] = curr.get('to'); | ||
return prev; | ||
}, {}); | ||
}) | ||
.then((expansions) => { | ||
this.set('expansions', expansions); | ||
}); | ||
|
||
} catch(e) { | ||
// console.log(`didInsert {e}`); | ||
} | ||
}, | ||
|
||
keyUp(k) { | ||
let textArea = k.target; | ||
let text = textArea.value; | ||
this.set('userText', text); | ||
this.set('cursorLocation', textArea.selectionStart); | ||
}, | ||
|
||
keyDown(k) { | ||
if (k.keyCode === 13) { | ||
let possibleSwaps = this.get('possibleSwaps'); | ||
if (possibleSwaps && possibleSwaps.length === 1) { | ||
let swapTo = possibleSwaps[0].to; | ||
let activeSite = this.get('activeExpansionSite'); | ||
let sliceLength = activeSite.match.length; | ||
let currentText = k.target.value; | ||
let modifiedText = currentText.slice(0, activeSite.index) + swapTo + currentText.slice(activeSite.index + sliceLength); | ||
k.target.value = modifiedText; | ||
|
||
k.preventDefault(); | ||
k.returnValue = false; | ||
k.cancelBubble = true; | ||
return false; | ||
} | ||
} | ||
}, | ||
|
||
// Find an expandable word that has the cursor within it | ||
activeExpansionSite: Ember.computed('userText', 'cursorLocation', function() { | ||
|
||
let userText = this.get('userText'); | ||
let textarea = this.get('textarea'); | ||
if (!textarea) { | ||
return null; | ||
} | ||
let cursorLoc = textarea.selectionStart; | ||
let subjects = textExpansion.findExpansionSubjects(userText); | ||
let sites = textExpansion.findExpansionSites(userText, subjects); | ||
|
||
return sites.find((s) => { | ||
let endIndex = s.index + s.match.length; | ||
|
||
return cursorLoc >= s.index && cursorLoc <= endIndex; | ||
}); | ||
}), | ||
|
||
// If an expansion site is active, which possible swaps could occur there? | ||
possibleSwaps: Ember.computed('activeExpansionSite', 'expansions', function() { | ||
let activeSite = this.get('activeExpansionSite'); | ||
|
||
if (activeSite) { | ||
let expansions = this.get('expansions'); | ||
return Object.keys(expansions) | ||
.filter((ex) => { | ||
return ex.startsWith(activeSite.term); | ||
}) | ||
.sort() | ||
.map((from) => { | ||
return { | ||
from, | ||
to: expansions[from] | ||
}; | ||
}); | ||
} | ||
}), | ||
|
||
expansionText: Ember.computed('possibleSwaps', 'activeExpansionSite', 'userText', function() { | ||
let result = ''; | ||
|
||
let i18n = this.get('i18n'); | ||
let possibleSwaps = this.get('possibleSwaps'); | ||
if (possibleSwaps) { | ||
let activeSite = this.get('activeExpansionSite'); | ||
|
||
if (possibleSwaps.length === 1) { | ||
let swapTo = possibleSwaps[0].to; | ||
result = i18n.t('admin.text_replacements.perform_expand', { from: activeSite.term, to: swapTo }); | ||
} else if (possibleSwaps.length > 1) { | ||
let possible = possibleSwaps | ||
.map((swap) => { | ||
return swap.from; | ||
}).join(', '); | ||
result = i18n.t('admin.text_replacements.possible_expansions', { possible }); | ||
} else { | ||
result = i18n.t('admin.text_replacements.no_matches', { term: activeSite.term }); | ||
} | ||
} | ||
|
||
return result; | ||
}), | ||
|
||
expansionDivStyle: Ember.computed('expansionText', function() { | ||
let expansionText = this.get('expansionText'); | ||
let visiblility = expansionText ? 'visible' : 'hidden'; | ||
let textArea = this.get('textarea'); | ||
|
||
let styleString = `visibility: ${visiblility};`; | ||
|
||
if (textArea) { | ||
let textPos = textArea.getBoundingClientRect(); | ||
styleString += ` top: ${textPos.bottom}px; left: ${textPos.left}px; width: ${textArea.offsetWidth}px;`; | ||
} | ||
return new Ember.Handlebars.SafeString(styleString); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
These values should be camel cased; no underscores