Commits

Alex Efros  committed d35c568

added widget SpellCheck

  • Participants
  • Parent commits 6cc54ad

Comments (0)

Files changed (2)

     <!-- Здесь будем подключать виджеты -->
     <script type="text/javascript" src="w.addphrase.js"></script>
     <script type="text/javascript" src="w.phrase.js"></script>
+    <script type="text/javascript" src="w.spellcheck.js"></script>
 </head>
 <body>
 <script type="text/javascript">
             if(data.phrase in w.phrase)
                 break;
             w.phrase[data.phrase] = new W.Phrase('tmpl_phrase', data.phrase);
+            w.phrase[data.phrase].add_handler(new W.SpellCheck('tmpl_spellcheck', data.phrase));
             $('#phrases').append( w.phrase[data.phrase].$ );
             break;
         case 'select phrase':
             delete w.phrase[data.phrase];
             $('#addphrase input').focus();
             break;
+        case 'spellcheck: started':
+            break;
+        case 'spellcheck: success':
+            break;
         default:
             alert('notice: unknown event "'+e+'"');
         }
         <span class="handlers"></span>
     </div>
     </script>
+    <script type="text/x-tmpl" id="tmpl_spellcheck">
+        <% if (status == 'started') { %>
+            (проверяю…)
+        <% } else { %>
+            (найдено <%= spellerrors %> ошибок)
+        <% } %>
+    </script>
 </div>
 </body>
 </html>

File w.spellcheck.js

+/*
+ * <script type="text/x-tmpl" id="tmpl_spellcheck">
+ *      <% if (status == 'started') { %>
+ *          Проверяю…
+ *      <% } else { %>
+ *          Найдено <%= spellerrors %> ошибок.
+ *      <% } %>
+ * </script>
+ *
+ * w = new W.SpellCheck('tmpl_spellcheck', 'some phrase');
+ *
+ * -> 'spellcheck: started', phrase
+ * -> 'spellcheck: success', phrase, spellerrors
+ *
+ */
+var W; W = W || {};
+
+W.SpellCheck = function(tmpl, phrase){
+    this.$      = $('<span>');
+    this._tmpl  = tmpl;
+    this._phrase= phrase;
+    this._load();
+};
+
+W.SpellCheck.prototype._load = function(){
+    $.ajax({
+        url:        'http://speller.yandex.net/services/spellservice.json/checkText',
+        data:       { text: this._phrase },
+        dataType:   'jsonp',
+        success:    $.proxy(this,'_load_success')
+    });
+    this.$.html( POWER.render(this._tmpl, {
+        status: 'started'
+    }) );
+    notice('spellcheck: started', { phrase: this._phrase });
+}
+
+W.SpellCheck.prototype._load_success = function(data){
+    this.$.html( POWER.render(this._tmpl, {
+        status: 'success',
+        spellerrors: data.length
+    }) );
+    notice('spellcheck: success', { phrase: this._phrase, spellerrors: data.length });
+};