-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebapi.js
More file actions
1002 lines (961 loc) · 43.8 KB
/
webapi.js
File metadata and controls
1002 lines (961 loc) · 43.8 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// webapi.js
/**
* @namespace WEBSPELLCHECKER
*/
(function() {
function init(Namespace) {
// Private
var OptionsManager = Namespace.OptionsManager,
logger = Namespace.logger,
optionTypes = OptionsManager.optionTypes,
optionsTemplate;
logger.isON(true);
optionsTemplate = {
lang: {
type: optionTypes.string,
defaultValue: 'auto'
},
autoLangPriorities: {
type: optionTypes.object,
defaultValue: {}
},
enforceAI: {
type: optionTypes.boolean,
defaultValue: false
},
serviceProtocol: {
type: optionTypes.urlProtocol,
defaultValue: 'http'
},
serviceHost: {
type: optionTypes.urlHost,
defaultValue: 'svc.webspellchecker.net'
},
servicePort: {
type: optionTypes.urlPort,
defaultValue: 80
},
servicePath: {
type: optionTypes.urlPath,
defaultValue: 'api'
},
serviceId: {
type: optionTypes.string,
defaultValue: '1:KpkvQ2-6KNUj-L1W3u2-C9j0K1-Zv2tY1-CfDOx-WfGRg2-qXtci-YyyE34-j09H42-b0aCt3-d9a'
},
userDictionaryName: {
type: optionTypes.string,
defaultValue: ''
},
customDictionaryIds: {
type: optionTypes.string,
defaultValue: ''
},
minWordLength: {
type: optionTypes.number,
defaultValue: 3
},
communicationFormat: {
type: optionTypes.string,
defaultValue: 'json'
},
customPunctuation: {
type: optionTypes.string,
defaultValue: ''
},
appType: {
type: optionTypes.string,
defaultValue: 'web_api'
},
withCredentials: {
type: optionTypes.boolean,
defaultValue: false
},
requestHeaders: {
type: optionTypes.function,
defaultValue: function() {}
}
};
OptionsManager.exportOptionsTemplate('WebApiTemplate', optionsTemplate);
/**
* @constructor
* @param {Object} clientOptions
* @private
*/
function WebApi(clientOptions) {
var self = this,
isErrorsCritical,
connection;
this._services = {};
// Create options based on clientOptions from user and with WebApiTemplate template.
this._options = OptionsManager.createOptions(clientOptions, 'WebApiTemplate', function errorHandler(errors) {
isErrorsCritical = errors.critical;
errors.reports.forEach(function(report) {
logger.log(report.message);
}, this);
});
// Instance based dependencies
this._dependencies = {
'TextProcessor': Namespace.TextProcessor,
'Connection': Namespace.Connection
};
for (var k in this._dependencies) {
this._services[k] = new this._dependencies[k](k, this);
}
connection = this._services['Connection'];
this._commands = connection.getCommands();
this._udActions = connection.getUdActions();
}
/**
* @namespace WebApiInstance
*/
WebApi.prototype = {
constructor: WebApi,
/**
* Return instance of dependencie.
* @private
*
* @param {String} name - Name of instancebased service.
*
* @returns {Object} - Instance of service.
*/
_getService: function(name) {
return this._services[name] || null;
},
/**
* Wrapper of request method.
* @private
*
* @param {Object} data - Object with request data.
* @param {function} success - Handler successful response from the server.
* @param {function} error - Handler unsuccessful response from the server.
*
* @returns {Object} - Transport object.
*/
_request: function(data, parameters) {
return this._getService('Connection').request(
data,
parameters.success || function() {},
parameters.error || function() {}
);
},
_makeUdAction: function(actionName, parameters) {
if (typeof parameters.name === 'undefined') {
parameters.name = this.getOption('userDictionaryName');
}
var requestparameters = {
command: this._commands.userDictionary,
UDAction: this._udActions[actionName],
UDName: parameters.name,
newUDName: parameters.newName,
wordList: parameters.wordList,
UDWord: parameters.word
};
return this._request(requestparameters, parameters);
},
_udMethodWrapper: function(actionName, parameters) {
var self = this,
success = parameters.success || function(){};
parameters.success = function(responseInfo) {
var ud = new UserDictionary({
wordlist: responseInfo.wordlist,
name: responseInfo.name,
modificationTime: responseInfo.modificationTime,
makeUdAction: self._makeUdAction.bind(self)
});
success(ud);
};
return this._makeUdAction(actionName, parameters);
},
getOption: function(name) {
return this._options[name];
},
setOption: function(name, value) {
var result = false,
template = {},
option = {};
if (optionsTemplate[name]) {
result = true;
template[name] = optionsTemplate[name];
option[name] = value;
this._options[name] = OptionsManager.createOptions(option, template)[name];
}
return result;
},
/**
* getInfo success Callback.
*
* @callback getInfoCallback
* @param {Object} data
* @param {Object} [data.langList={"ltr":{"en_US" : "American English","en_GB" : "British English","fr_FR" : "French","de_DE" : "German","it_IT" : "Italian","es_ES" : "Spanish"},"rtl":{}}]
* Object with list of available languages. Separeted on ltr(left-to-right) and rtl(right-to-left) directions.
* @param {Number} [data.verLang=9] Number of available languages.
* @param {Boolean} [data.banner=false] Banner parameter for integrations.
*/
/**
* getInfo API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {getInfoCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.getInfo({
* success: function(data) {
* console.log(data); // {"langList":{"ltr":{"en_US" : "American English","en_GB" : "British English","fr_FR" : "French","de_DE" : "German","it_IT" : "Italian","es_ES" : "Spanish"},"rtl":{}},"verLang":9}
* },
* error: function(error) {
* console.log(error);
* }
* })
*/
getInfo: function(parameters) {
return this._request({
command: this._commands.getInfo,
locale: parameters.locale || this.getOption('localization'),
version: parameters.version || 2,
containerType: this.getOption('containerType')
},
parameters
);
},
/**
* getLangList success Callback.
*
* @callback GetLangListCallback
* @param {Object} data
* @param {Object} [data.langList={"ltr":{"en_US" : "American English","en_GB" : "British English","fr_FR" : "French","de_DE" : "German","it_IT" : "Italian","es_ES" : "Spanish"},"rtl":{}}]
* Object with list of available languages. Separeted on ltr(left-to-right) and rtl(right-to-left) directions.
* @param {Number} [data.verLang=9] Number of available languages.
*/
/**
* getLangList API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {GetLangListCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.getLangList({
* success: function(data) {
* console.log(data); // {"langList":{"ltr":{"en_US" : "American English","en_GB" : "British English","fr_FR" : "French","de_DE" : "German","it_IT" : "Italian","es_ES" : "Spanish"},"rtl":{}},"verLang":9}
* },
* error: function(error) {
* console.log(error);
* }
* })
*/
getLangList: function(parameters) {
return this._request({
command: this._commands.getLangList
},
parameters
);
},
/**
* spellCheck success Callback.
*
* @callback SpellCheckCallback
* @param {Object[]} wordObjects
* @param {String} wordObjects.word - Misspelled word.
* @param {String} wordObjects.ud - Flag. Is this word in the user dictionary.
* @param {String[]} wordObjects.suggestions - Array with suggestions for current misspelled.
*/
/**
* spellCheck API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.text - Text to check spelling.
* @param {String} parameters.lang - Spellcheck language. If not provided then take from constructor.
* @param {SpellCheckCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.spellCheck({
* text: 'This is an exampl of a sentence with two mispelled words. Just type text with misspelling to see how it works',
* success: function(data) {
* console.log(data); // [{"word":"exampl","ud":"false","suggestions":["example","examples","exempla","exam","exemplar","exemplum","resample","exemplars","exemplary","exemplify","exempt","exams","xml","decamp","beanpole"]},{"word":"mispelled","ud":"false","suggestions":["misspelled","dispelled","impelled","misspell","miscalled","misspells","misplaced","misplayed","respelled","morseled","micelle","Giselle","misapplied","misspelt","installed"]}]
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
spellCheck: function(parameters) {
var _parameters = Object.assign({}, parameters),
words = this._getService('TextProcessor').getWordsFromString( _parameters.text ),
text = words.wordsCollection.join(',');
function addOffsetsToMisspelled(data, offsets) {
var misspelled;
return offsets.reduce(function(prev, offsetsObj) {
for (var i = 0; i < data.length; i += 1) {
misspelled = data[i];
if (misspelled.word === offsetsObj.word) {
prev.push( Object.assign(
{},
misspelled,
offsetsObj
) );
return prev;
}
}
return prev;
}, []);
}
_parameters.success = function(data) {
var misspelledsWithOffsets = addOffsetsToMisspelled(data, words.wordsOffsets);
parameters.success(misspelledsWithOffsets);
};
return this._request({
command: this._commands.spellCheck,
language: _parameters.lang || this.getOption('lang'),
userWordlist: parameters.userWordlist,
customDictionary: this.getOption('customDictionaryIds'),
userDictionary: this.getOption('userDictionaryName'),
text: text
},
{
success: _parameters.success,
error: _parameters.error
}
);
},
/**
* grammarCheck success Callback.
*
* @callback GrammarCheckCallback
* @param {Object[]} phraseObjects
* @param {String} phraseObjects.description - Description of grammar problem.
* @param {String} phraseObjects.phrase - Phrase with problem.
* @param {Number} phraseObjects.problem_id - Id of current problem.
* @param {String[]} phraseObjects.suggestions - Array with suggestions.
*/
/**
* grammarCheck API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.text - Text to grammar checking.
* @param {String} parameters.lang - Grammarcheck language. If not provided then take from constructor.
* @param {GrammarCheckCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.grammarCheck({
* text: 'These are an examples of a sentences with two misspelled words and gramar problems. Just type text with mispelling to see how it works.',
* success: function(data) {
* console.log(data); // [{"phrase":"type text","description":"Missing preposition.","problem_id":"436864176","suggestions":["type of text"]}]
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
grammarCheck: function(parameters) {
return this._request({
command: this._commands.grammarCheck,
language: parameters.lang || this.getOption('lang'),
sentences: parameters.sentences,
text: parameters.text
},
parameters
);
},
/**
* check API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.text - Text to checking.
* @param {String} parameters.lang - Check language. If not provided then take from constructor.
* @param {GrammarCheckCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.check({
* text: 'These are an examples of a sentences with two misspelled words and gramar problems. Just type text with mispelling to see how it works.',
* success: function(data) {
* console.log(data);
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
check: function(parameters) {
return this._request({
command: this._commands.check,
language: parameters.lang || this.getOption('lang'),
autoLangPriorities: parameters.autoLangPriorities,
enforceAI: parameters.enforceAI || this.getOption('enforceAI'),
shortAnswer: true,
userWordlist: parameters.userWordlist,
customDictionary: this.getOption('customDictionaryIds'),
userDictionary: this.getOption('userDictionaryName'),
customPunctuation: this.getOption('customPunctuation'),
minWordLength: this.getOption('minWordLength'),
disableGrammar: !this.getOption('enableGrammar') ? true : false,
disableStyleGuide: this.getOption('disableStyleGuide') ? true : false,
checkKit: this.getOption('checkKit'),
tokens: parameters.tokens,
text: parameters.text
},
parameters
);
},
/**
* Autocorrect API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.text - Text to autocorrect.
* @param {String} parameters.lang - Check language. If not provided then take from constructor.
* @param {GrammarCheckCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.autocorrect({
* text: 'teh',
* success: function(data) {
* console.log(data);
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
autocorrect: function(parameters) {
return this._request({
command: this._commands.autocorrect,
language: parameters.lang || this.getOption('lang'),
autoLangPriorities: parameters.autoLangPriorities || '',
detectedLang: parameters.detectedLang,
shortAnswer: true,
userWordlist: parameters.userWordlist,
customDictionary: this.getOption('customDictionaryIds'),
userDictionary: this.getOption('userDictionaryName'),
text: parameters.text
},
parameters
);
},
/**
* Autocomplete API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.text - Text to autocomplete.
* @param {String} parameters.lang - Check language. If not provided then take from constructor.
* @param {GrammarCheckCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.autocomplete({
* text: 'How are ',
* success: function(data) {
* console.log(data);
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
autocomplete: function(parameters) {
return this._request({
command: this._commands.autocomplete,
session: this.getOption('session'),
timestamp: new Date().getTime(),
language: parameters.lang || this.getOption('lang'),
autoLangPriorities: parameters.autoLangPriorities || '',
detectedLang: parameters.detectedLang,
shortAnswer: true,
text: parameters.text
},
parameters
);
},
/**
* getPrompts API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {getInfoCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.getPrompts({
* success: function(data) {
* console.log(data);
* },
* error: function(error) {
* console.log(error);
* }
* })
*/
getPrompts: function(parameters) {
return this._request({
command: this._commands.getPrompts
},
parameters
);
},
/**
* generate API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.text - Text to generate completion.
* @param {String} parameters.lang - Chosen language. If not provided then take from constructor.
* @param {GrammarCheckCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.generate({
* text: 'These are an examples of a sentences with two misspelled words and gramar problems. Just type text with mispelling to see how it works.',
* success: function(data) {
* console.log(data);
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
generate: function(parameters) {
return this._request({
command: this._commands.generate,
session: this.getOption('session'),
timestamp: new Date().getTime(),
prompt: parameters.prompt,
language: parameters.lang || this.getOption('lang'),
autoLangPriorities: parameters.autoLangPriorities || '',
detectedLang: parameters.detectedLang,
shortAnswer: true,
text: parameters.text
},
parameters
);
},
/**
* statistics API method.
* @private
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.action - Statistics action type.
* @param {String} parameters.text - Text related to the action.
* @param {String | Undefined} parameters.newText - New text related to the replace action only.
* @param {String} parameters.lang - Check language.
* @param {String} parameters.detectedLang - Auto detected language.
* @param {String} parameters.enforceAI - Enforce the use of enhanced text checking for American, British, Canadian and Australian English.
* @param {String} parameters.type - Type of the problem.
* @param {String | Undefined} parameters.context - Context of the problem.
* @param {Array} parameters.suggestions - Suggestions of the problem.
* @param {StatisticsCallback} parameters.success - Handler for successful response from the server.
* @param {RequestCallback} parameters.error - Handler for unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.statistics({
* action: 'replace',
* text: 'Hhello',
* newText: 'Hello',
* lang: 'en_US',
* type: 'spelling',
* context: 'Hhello man.',
* success: function(data) {
* console.log(data);
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
statistics: function(parameters) {
return this._request({
command: this._commands.statistics,
action: parameters.action,
session: this.getOption('session'),
timestamp: new Date().getTime(),
text: parameters.text,
newText: parameters.newText,
language: parameters.lang || this.getOption('lang'),
detectedLang: parameters.detectedLang,
enforceAI: parameters.enforceAI || this.getOption('enforceAI'),
checkKit: this.getOption('checkKit'),
type: parameters.type,
category: parameters.category,
rule: parameters.rule || '',
offset: parameters.offset,
context: parameters.context,
prompt: parameters.prompt,
suggestions: parameters.suggestions,
probability: parameters.probability,
responseTime: parameters.responseTime
},
parameters
);
},
/**
* userDictionary success Callback.
*
* @callback UserDictionaryCallback
* @param {UserDictionary} UDObject
*/
/**
* getUserDictionary API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.name - User dictionary name.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.getUserDictionary({
* name: 'test',
* success: function(data) {
* console.log(data); // {"name":"test","action":"getdict","wordlist":[]}
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
getUserDictionary: function(parameters) {
return this._udMethodWrapper('getDict', parameters);
},
/**
* createUserDictionary API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.name - User dictionary name.
* @param {String} parameters.wordList - Word list.
*
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.createUserDictionary({
* name: 'test',
* success: function(data) {
* console.log(data); // {"name":"test","action":"create","wordlist":[]}
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
createUserDictionary: function(parameters) {
return this._udMethodWrapper('create', parameters);
},
/**
* deleteUserDictionary API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.name - User dictionary name.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.deleteUserDictionary({
* name: 'test',
* success: function(data) {
* console.log(data); // {"name":"test","action":"delete","wordlist":[]}
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
deleteUserDictionary: function(parameters) {
return this._udMethodWrapper('delete', parameters);
},
/**
* renameUserDictionary API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.name - User dictionary name.
* @param {String} parameters.newName - New user dictionary name.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.renameUserDictionary({
* name: 'test',
* success: function(data) {
* console.log(data); // {"name":"test","action":"rename","wordlist":[]}
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
renameUserDictionary: function(parameters) {
return this._udMethodWrapper('rename' , parameters);
},
/**
* addWordToUserDictionary API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.name - User dictionary name.
* @param {String} parameters.word - Word what will be added to UD.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.addWordToUserDictionary({
* name: 'test',
* word: 'exaple',
* success: function(data) {
* console.log(data); // {"name":"test","action":"addword","wordlist":[]}
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
addWordToUserDictionary: function(parameters) {
return this._udMethodWrapper('addWord', parameters);
},
/**
* deleteWordFromUserDictionary API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.name - User dictionary name.
* @param {String} parameters.word - Word what will be deleted from UD.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.deleteWordFromUserDictionary({
* name: 'test',
* word: 'exaple',
* success: function(data) {
* console.log(data); // {"name":"test","action":"deleteword","wordlist":[]}
* },
* error: function(error) {
* console.log(error);
* }
* });
*/
deleteWordFromUserDictionary: function(parameters) {
return this._udMethodWrapper('deleteWord', parameters);
},
/**
* getDictionariesModifyTime API method.
* @public
* @memberof WebApiInstance#
*
* @param {Object} parameters
* @param {String} parameters.userDictionary - User dictionary name.
* @param {String} parameters.customDictionary - Custom dictionary name.
* @param {GetDictionariesModifyTime} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
* @returns {Object} - Transport object.
* @example
* wscWebApiInstance.getDictionariesModifyTime({
* userDictionary: 'udName',
* customDictionary: '1',
* success: function(data) {
* console.log(data); // {"customDicts":{},"userDicts":{"udName": 1513613189}}
* },
* error: function(error) {
* console.log(error);
* }
* })
*/
getDictionariesModifyTime: function(parameters) {
return this._request({
command: this._commands.getDictionariesModifyTime,
UDName: parameters.userDictionary,
customDictionary: parameters.customDictionary
},
parameters
);
},
};
/**
* UserDictionary constructor.
* Encapsulates the construction of a query for UD request.
*
* @typedef {(Object)} UserDictionary
* @namespace UserDictionary
* @property {String} parameters.name - List of available UD actions.
* @property {function} parameters.wordlist - List of words in current dictionary.
* @property {function} parameters.makeUdAction - Request-maker function.
*/
function UserDictionary(parameters) {
this.name = parameters.name;
this.wordlist = parameters.wordlist;
this.modificationTime = parameters.modificationTime;
this.makeUdAction = parameters.makeUdAction;
}
UserDictionary.prototype = {
constructor: UserDictionary,
_action: function(actionName, parameters) {
this.makeUdAction(actionName, Object.assign({
name: this.name
}, parameters) );
},
/**
* Add word to current user dictionary.
*
* @property {Object} parameters
* @memberof UserDictionary
*
* @property {String} parameters.word - Word what will be added to UD.
* @property {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @property {RequestCallback} parameters.error - Handler unsuccessful response from the server.
*/
addWord: function(parameters) {
var success = parameters.success,
self = this;
parameters.success = function(responseInfo) {
self.wordlist.push(parameters.word);
success(self);
};
this._action('addWord', parameters);
},
/**
* Delete word to current user dictionary.
*
* @memberof UserDictionary
* @param {Object} parameters
*
* @param {String} parameters.word - Word what will be deleted from UD.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
*/
deleteWord: function(parameters) {
var success = parameters.success,
word = parameters.word,
self = this;
parameters.success = function(responseInfo) {
var wordlist = [];
for (var i = 0; i < self.wordlist.length; i += 1) {
if (self.wordlist[i] !== word) {
wordlist.push(wordlist[i]);
}
}
self.wordlist = wordlist;
success(self);
};
this._action('deleteWord', parameters);
},
/**
* Delete current user dictionary.
*
* @memberof UserDictionary
* @param {Object} parameters
*
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
*/
'delete': function(parameters) {
var success = parameters.success,
self = this;
parameters.success = function(responseInfo) {
this.name = undefined;
this.wordlist = undefined;
success(self);
};
this._action('delete', parameters);
},
/**
* Rename current user dictionary.
*
* @memberof UserDictionary
* @param {Object} parameters
*
* @param {String} parameters.newName - New Ud name.
* @param {UserDictionaryCallback} parameters.success - Handler successful response from the server.
* @param {RequestCallback} parameters.error - Handler unsuccessful response from the server.
*/
rename: function(parameters) {
var success = parameters.success,
self = this;
parameters.success = function(responseInfo) {
self.name = parameters.newName;
success(self);
};
this._action('rename', parameters);
},
/**
* Return UD Word list.
* @memberof UserDictionary
*
* @returns {Array} UD word list.
*/
getWordList: function() {
return this.wordlist.slice();
}
};
/**
* Method check client options and return WebApi instance.
* @memberof WEBSPELLCHECKER
* @method WEBSPELLCHECKER.initWebApi
* @param {Object} options
* <start options doc>
* @property {string} [options.lang='en_US'] - The parameter sets the default spell checking language for WEBSPELLCHECKER. Possible values are:
* 'en_US', 'en_GB', 'pt_BR', 'da_DK',
* 'nl_NL', 'en_CA', 'fi_FI', 'fr_FR',
* 'fr_CA', 'de_DE', 'el_GR', 'it_IT',
* 'nb_NO', 'pt_PT', 'es_ES', 'sv_SE'.
*
* @property {string} [options.serviceProtocol='http'] - The parameter allows specifying a protocol for the WSC service (the entry point is ssrv.cgi) full path.
*
* @property {string} [options.serviceHost='svc.webspellchecker.net'] - The parameter allows specifying a host for the WSC service (the entry point is ssrv.cgi) full path.
*
* @property {number} [options.servicePort='80'] - The parameter allows specifying a default port for the WSC service (the entry point is ssrv.cgi) full path.
*
* @property {string} [options.servicePath='spellcheck31/script/ssrv.cgi'] - The parameter is used to specify a path to the WSC service (the entry point is ssrv.cgi) full path.
*
* @property {number} [options.minWordLength=3] - The parameter defines minimum length of the letters that will be collected from container's text for spell checking.
* Possible value is any positive number.
*
* @property {string} [options.customDictionaryIds=''] - The parameter links WEBSPELLCHECKER to custom dictionaries. Here is a string containing dictionary IDs separated by commas (',').
* Further details can be found at [link](@@BRANDING_CUSTOM_DICT_MANUAL_URL).
*
* @property {string} [options.userDictionaryName=''] - The parameter activates a User Dictionary in WEBSPELLCHECKER.
*
* @property {string} [options.serviceId=''] - The parameter sets the service ID for WEBSPELLCHECKER. It used for a migration from free,
* ad-supported version to paid, ad-free version.
*
* @property {String} [options.customPunctuation=''] - The parameter that receives a string with characters that will considered as separators.
*
* <end options doc>
* @returns {WebApiInstance} - WebApi Instance.
*/
Namespace.initWebApi = function(options) {
return new WebApi(options);
};
}
if (typeof window === 'undefined') {
module.exports = init;
}
if (typeof WEBSPELLCHECKER !== 'undefined' && !('initWebApi' in WEBSPELLCHECKER)) {
init(WEBSPELLCHECKER);