This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-imperavi.coffee
145 lines (124 loc) · 4.2 KB
/
angular-imperavi.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
###*
# Created by delta54 on 31.05.15.
###
'use strict'
debug = false
log = (m) ->
if debug
console.log 'imeravi: ' + m
angular
.module('ui.imperavi', [])
.directive 'uiImperavi', ['$timeout','$window'
($timeout, $window) ->
{
restrict: 'A'
require: 'ngModel'
scope:
assetPath: '@'
lang: '@'
plugins: '='
options: '='
link: ($scope, element, attrs, ngModel) ->
log 'init'
$window.loaded_plugins = []
isLoaded = false
# set options
options = $.extend(true,
buttonSource: true
focus: true
linebreaks: false
autoload_plugins: false
plugins: $scope.plugins if $scope.plugins
, $scope.options || {})
options.changeCallback = (value) ->
$timeout ->
$scope.$apply ->
ngModel.$setViewValue value
getLang = (callback) ->
if typeof $scope.assetPath == 'undefined' || typeof $scope.lang == 'undefined'
callback()
file = $scope.assetPath + 'lang/' + $scope.lang + '.js'
log 'load lang file: ' + file
$.getScript file, (data, textStatus, jqxhr) ->
if jqxhr.status == 200
options.lang = $scope.lang
callback()
getPlugins = (callback) ->
if typeof $scope.assetPath == 'undefined'
callback()
plugins = [
{id: 'clips',css: 'clips.css',js: 'clips.js'}
{id: 'counter',js: 'counter.js'}
{id: 'definedlinks',js: 'definedlinks.js'}
{id: 'filemanager',js: 'filemanager.js'}
{id: 'fontcolor',js: 'fontcolor.js'}
{id: 'fontsize',js: 'fontsize.js'}
{id: 'fullscreen',js: 'fullscreen.js'}
{id: 'fontfamily',js: 'fontfamily.js'}
{id: 'imagemanager',js: 'imagemanager.js'}
{id: 'limiter',js: 'limiter.js'}
{id: 'textdirection',js: 'textdirection.js'}
{id: 'textexpander',js: 'textexpander.js'}
{id: 'video',js: 'video.js'}
{id: 'table',js: 'table.js'}
]
callback_count = 0
angular.forEach $scope.plugins, (plugin) ->
i = 0
while i < plugins.length
if plugins[i].id == plugin
#TODO need check if plugin is loaded
$window.loaded_plugins.push plugins[i].id
# load css file
if plugins[i]?.css
file = $scope.assetPath + 'plugins/' + plugins[i].id + '/' + plugins[i].css
log 'load css file: ' + file
$('<link/>',
rel: 'stylesheet'
type: 'text/css'
href: file).appendTo 'head'
# load js file
if plugins[i]?.js
file = $scope.assetPath + 'plugins/' + plugins[i].id + '/' + plugins[i].js
log 'load plugin: ' + file
$.getScript file, (data, textStatus, jqxhr) ->
++callback_count
if callback_count == $scope.plugins.length
callback()
i++
start = ->
# start redactor
callback = ->
if !isLoaded
log 'start'
$(element).redactor(options).redactor 'insert.set', ngModel.$viewValue or ''
isLoaded = true
if options.autoload_plugins
getLang ->
getPlugins ->
callback()
else
callback()
destroy = ->
$(element).redactor 'core.destroy'
log 'destroy'
restart = ->
destroy()
start()
$scope.$watch 'lang', 'plugins', 'options', (val, old_val) ->
if val == old_val
return
restart()
# Editor unloaded
$scope.$on '$destroy', (event, next, current) ->
destroy()
ngModel.$render = ->
$timeout ->
if isLoaded
$(element).redactor('insert.set', ngModel.$viewValue or '').redactor 'placeholder.toggle'
#
# init
#------------------------------
start()
}
]