appointments.js
1 year ago
attendees.js
1 year ago
colorManipulation.js
3 years ago
customer.js
1 year ago
date.js
1 year ago
defaultCustomize.js
1 year ago
employee.js
1 year ago
events.js
1 year ago
formFieldsTemplates.js
3 years ago
formatting.js
1 year ago
helper.js
1 year ago
image.js
1 year ago
integrationApple.js
1 year ago
integrationGoogle.js
1 year ago
integrationOutlook.js
1 year ago
integrationStripe.js
1 year ago
integrationZoom.js
1 year ago
licence.js
2 years ago
objectAndArrayManipulation.js
3 years ago
pricing.js
1 year ago
recurring.js
1 year ago
responsive.js
1 year ago
scrollElements.js
4 years ago
settings.js
1 year ago
translationsElementPlus.js
1 year ago
image.js
78 lines
| 1 | let usedColors = [] |
| 2 | let colors = [ |
| 3 | '1788FB', |
| 4 | '4BBEC6', |
| 5 | 'FBC22D', |
| 6 | 'FA3C52', |
| 7 | 'D696B8', |
| 8 | '689BCA', |
| 9 | '26CC2B', |
| 10 | 'FD7E35', |
| 11 | 'E38587', |
| 12 | '774DFB', |
| 13 | '31CDF3', |
| 14 | '6AB76C', |
| 15 | 'FD5FA1', |
| 16 | 'A697C5' |
| 17 | ] |
| 18 | |
| 19 | function usePictureLoad (baseUrls, entity, isPerson) { |
| 20 | if (entity !== null) { |
| 21 | let name = isPerson === true ? entity.firstName + ' ' + entity.lastName : entity.name |
| 22 | if (typeof name !== 'undefined') { |
| 23 | entity.pictureThumbPath = entity.pictureThumbPath || imageFromText(baseUrls, name) |
| 24 | return entity.pictureThumbPath |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | function getNameInitials (name) { |
| 30 | return name.split(' ').map((s) => s.charAt(0)).join('').toUpperCase().substring(0, 3).replace(/[^\w\s]/g, '') |
| 31 | } |
| 32 | |
| 33 | function imageFromText (baseUrls, name, entity = {}, error = false) { |
| 34 | let initials = getNameInitials(name) |
| 35 | let colorIndex = Math.floor(Math.random() * colors.length) |
| 36 | let colorHex = colors[colorIndex] |
| 37 | usedColors.push(colors[colorIndex]) |
| 38 | colors.splice(colorIndex, 1) |
| 39 | if (colors.length === 0) { |
| 40 | colors = usedColors |
| 41 | usedColors = [] |
| 42 | } |
| 43 | if (error) { |
| 44 | if (entity.firstName) { |
| 45 | return baseUrls.wpAmeliaPluginURL + 'public/img/default-employee.svg' |
| 46 | } |
| 47 | if (entity.latitude) { |
| 48 | return baseUrls.wpAmeliaPluginURL + 'public/img/default-location.svg' |
| 49 | } |
| 50 | return baseUrls.wpAmeliaPluginURL + 'public/img/default-service.svg' |
| 51 | } |
| 52 | |
| 53 | // Make canvas |
| 54 | const canvas = document.createElement('canvas'); |
| 55 | canvas.width = 100; |
| 56 | canvas.height = 100; |
| 57 | const ctx = canvas.getContext('2d'); |
| 58 | |
| 59 | // Draw background color |
| 60 | ctx.fillStyle = `#${colorHex}` // Background color |
| 61 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 62 | |
| 63 | // Draw initials text |
| 64 | ctx.font = '40px Arial'; |
| 65 | ctx.fillStyle = '#ffffff'; // Text color |
| 66 | ctx.textAlign = 'center'; |
| 67 | ctx.textBaseline = 'middle'; |
| 68 | ctx.fillText(initials, canvas.width / 2, canvas.height / 2); |
| 69 | |
| 70 | // Convert canvas to data URL (as image source) |
| 71 | return canvas.toDataURL('image/png'); |
| 72 | } |
| 73 | |
| 74 | export { |
| 75 | usePictureLoad, |
| 76 | getNameInitials |
| 77 | } |
| 78 |