home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / filF9CC53EA258B23D0E47AC2FC84B7FFBA < prev    next >
Text File  |  2010-07-12  |  10KB  |  315 lines

  1. var YaSpellcheckerUserDict = {
  2.   get personalDict() {
  3.     delete this.personalDict;
  4.     this.personalDict = Cc["@mozilla.org/spellchecker/personaldictionary;1"]
  5.                             .getService().QueryInterface(Ci.mozIPersonalDictionary);
  6.     
  7.     return this.personalDict;
  8.   },
  9.   
  10.   get tree() {
  11.     delete this.tree;
  12.     this.tree = document.getElementById("words-list");
  13.     return this.tree;
  14.   },
  15.   
  16.   get treechildren() {
  17.     delete this.treechildren;
  18.     this.treechildren = document.getElementById("words-list-treechildren");
  19.     return this.treechildren;
  20.   },
  21.   
  22.   init: function() {
  23.     this.buildDictList();
  24.     this.tree.addEventListener("yaStopEditing", this, false);
  25.     this.tree.addEventListener("yaStartEditing", this, false);
  26.     this.tree.addEventListener("select", this, false);
  27.   },
  28.   
  29.   saveDict: function() {
  30.     this.personalDict.save();
  31.     document.getElementById("dict-button-save").setAttribute("disabled", "true");
  32.   },
  33.   
  34.   onDialogAccept: function() {
  35.     this.saveDict();
  36.   },
  37.   
  38.   _canceledDialod: false,
  39.   
  40.   onDialogCancel: function() {
  41.     if (this._canceledDialod || document.getElementById("dict-button-save").hasAttribute("disabled"))
  42.       return true;
  43.     
  44.     this._canceledDialod = true;
  45.     
  46.     let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"].createInstance(Ci.nsIStringBundleService)
  47.                            .createBundle("chrome://yasearch/locale/spellchecker/spellchecker.properties");
  48.     
  49.     function getLocalizedString(aName) {
  50.       return stringBundle.GetStringFromName(aName);
  51.     }
  52.     
  53.     let prompter = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
  54.     
  55.     let flags = prompter.BUTTON_POS_2 * prompter.BUTTON_TITLE_IS_STRING +
  56.                 prompter.BUTTON_POS_1 * prompter.BUTTON_TITLE_IS_STRING +
  57.                 prompter.BUTTON_POS_0 * prompter.BUTTON_TITLE_IS_STRING +
  58.                 prompter.BUTTON_POS_2_DEFAULT;
  59.     
  60.     let result = prompter.confirmEx(window,
  61.                                     getLocalizedString("spellcheckerUserDictConfirmTitle"),
  62.                                     getLocalizedString("spellcheckerUserDictConfirmMessage"),
  63.                                     flags,
  64.                                     getLocalizedString("spellcheckerUserDictConfirmButtonCancel"),
  65.                                     getLocalizedString("spellcheckerUserDictConfirmButtonDontSave"),
  66.                                     getLocalizedString("spellcheckerUserDictConfirmButtonSave"),
  67.                                     null, {});
  68.     
  69.     switch (result) {
  70.       case 0:
  71.         this._canceledDialod = false;
  72.         break;
  73.       
  74.       case 2:
  75.         this.saveDict();
  76.         break;
  77.       
  78.       case 1:
  79.       default:
  80.         break;
  81.     }
  82.     
  83.     return this._canceledDialod;
  84.   },
  85.   
  86.   onDialogUnload: function() {
  87.     this.tree.removeEventListener("select", this, false);
  88.     this.tree.removeEventListener("yaStartEditing", this, false);
  89.     this.tree.removeEventListener("yaStopEditing", this, false);
  90.     
  91.     this.personalDict.load();
  92.     
  93.     let openerDocument = window.opener.document;
  94.     if (openerDocument) {
  95.       let tbSpellcheckerButton = openerDocument.getElementById("yasearch-spellchecker-button");
  96.       if (tbSpellcheckerButton && "updatePageDictionary" in tbSpellcheckerButton)
  97.         tbSpellcheckerButton.updatePageDictionary();
  98.     }
  99.   },
  100.   
  101.   onTreeSelect: function() {
  102.     let selections = this.getTreeSelections();
  103.     let disabled = !selections.length;
  104.     
  105.     document.getElementById("dict-button-remove").setAttribute("disabled", disabled);
  106.     document.getElementById("dict-button-edit").setAttribute("disabled", disabled || (this.tree.editingRow != -1));
  107.   },
  108.   
  109.   handleEvent: function(aEvent) {
  110.     switch (aEvent.type) {
  111.       case "select":
  112.         this.onTreeSelect();
  113.         break;
  114.       
  115.       case "yaStartEditing":
  116.         if (this.tree.editingRow != -1)
  117.           document.getElementById("info-box").setAttribute("visible", "true");
  118.         break;
  119.       
  120.       case "yaStopEditing":
  121.         document.getElementById("info-box").setAttribute("visible", "false");
  122.         
  123.         let changedData = aEvent.yaTextChangedData;
  124.         
  125.         if (changedData.shouldAccept && changedData.textBefore != changedData.textAfter) {
  126.           if (changedData.textBefore)
  127.             this.personalDict.removeWord(changedData.textBefore, null);
  128.           
  129.           if (changedData.textAfter) {
  130.             let rowIndex = changedData.row;
  131.             
  132.             let words = changedData.textAfter
  133.                             .replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ')
  134.                             .split(/\s/)
  135.                             .reverse()
  136.                             .filter(
  137.                               function(aWord) {
  138.                                 return aWord && !this.personalDict.check(aWord, null);
  139.                               }, this
  140.                             );
  141.             
  142.             if (words.length) {
  143.               let word = words.pop();
  144.               this.tree.view.setCellText(rowIndex, changedData.column, word);
  145.               if (!this.personalDict.check(word, null))
  146.                 this.personalDict.addWord(word, null);
  147.             } else {
  148.               this.removeRow(changedData.row);
  149.             }
  150.             
  151.             let addedWordsLength = words.length;
  152.             
  153.             words.forEach(function(aWord) {
  154.               if (!this.personalDict.check(aWord, null)) {
  155.                 this.personalDict.addWord(aWord, null);
  156.                 this.appendTreeItemForWord(aWord, rowIndex + 1);
  157.               } else {
  158.                 addedWordsLength--;
  159.               }
  160.             }, this);
  161.             
  162.             this.tree.view.selection.rangedSelect(rowIndex, rowIndex + addedWordsLength, false);
  163.           }
  164.         }
  165.         
  166.         if (!changedData.textAfter || (!changedData.shouldAccept && !changedData.textBefore))
  167.           this.removeRow(changedData.row);
  168.         else
  169.           this.onChange();
  170.         
  171.         this.onTreeSelect();
  172.         
  173.         setTimeout(function(me) {
  174.           if (me && "tree" in me) {
  175.             let focused = document.commandDispatcher.focusedElement;
  176.             if (!focused || (focused instanceof Ci.nsIDOMHTMLInputElement))
  177.               if (focused)
  178.                 focused.blur();
  179.               
  180.               me.tree.focus();
  181.           }
  182.         }, 5, this);
  183.         
  184.         break;
  185.     }
  186.   },
  187.   
  188.   onChange: function() {
  189.     let saveButton = document.getElementById("dict-button-save");
  190.     if (saveButton.hasAttribute("disabled"))
  191.       saveButton.removeAttribute("disabled");
  192.   },
  193.   
  194.   addRow: function() {
  195.     setTimeout(function(me) {
  196.       if (me && "_addRow" in me)
  197.         me._addRow();
  198.     }, 15, this);
  199.   },
  200.   
  201.   _addRow: function() {
  202.     if (this.tree.editingRow != -1 && !this.tree.inputField.value)
  203.       return;
  204.     
  205.     this.tree.stopEditing(true);
  206.     
  207.     this.appendTreeItemForWord("");
  208.     
  209.     let rowIndex = this.treechildren.childNodes.length - 1;
  210.     this.editRow(rowIndex);
  211.   },
  212.   
  213.   editRow: function(aRowIndex) {
  214.     let rowIndex = arguments.length ? aRowIndex : Math.min(this.treechildren.childNodes.length - 1, this.tree.currentIndex);
  215.     if (rowIndex < 0)
  216.       return;
  217.     
  218.     this.tree.view.selection.select(rowIndex);
  219.     this.tree.startEditing(rowIndex, this.tree.columns.getColumnAt(0));
  220.     
  221.     this.onChange();
  222.   },
  223.   
  224.   _removeRow: function(aRowIndex) {
  225.     let text = this.tree.view.getCellText(aRowIndex, this.tree.columns.getColumnAt(0));
  226.     if (text)
  227.       this.personalDict.removeWord(text, null);
  228.   },
  229.   
  230.   removeRow: function(aRowIndex) {
  231.     let rowIndex = arguments.length ? aRowIndex : this.tree.currentIndex;
  232.     if (rowIndex < 0 || rowIndex > this.treechildren.childNodes.length - 1)
  233.       return;
  234.     
  235.     this._removeRow(rowIndex);
  236.     this.treechildren.removeChild(this.treechildren.childNodes[rowIndex]);
  237.     this.tree.view.selection.select(rowIndex-1);
  238.     
  239.     this.onChange();
  240.   },
  241.   
  242.   removeRows: function() {
  243.     let selections = this.getTreeSelections();
  244.     let selectionsLen = selections.length;
  245.     
  246.     if (!selectionsLen)
  247.       return;
  248.     
  249.     let treechildren = this.treechildren;
  250.     let trChildNodes = treechildren.childNodes;
  251.     
  252.     selections.reverse().forEach(function(aSelection) {
  253.       this._removeRow(aSelection);
  254.       treechildren.removeChild(trChildNodes[aSelection]);
  255.     }, this);
  256.     
  257.     this.tree.view.selection.select(Math.min(treechildren.childNodes.length - 1, selections[selectionsLen-1]));
  258.     
  259.     this.onChange();
  260.   },
  261.   
  262.   buildDictList: function() {
  263.     let wordsArray = [];
  264.     let personalEnumerator = this.personalDict.wordList;
  265.     
  266.     while (personalEnumerator.hasMore())
  267.       wordsArray.push(personalEnumerator.getNext());
  268.     
  269.     wordsArray.sort().forEach(function(aWord) {
  270.       this.appendTreeItemForWord(aWord);
  271.     }, this);
  272.   },
  273.   
  274.   getTreeSelections: function() {
  275.     let selections = [];
  276.     let select = this.tree.view.selection;
  277.     
  278.     if (select) {
  279.       let childNodesLen = this.treechildren.childNodes.length;
  280.       if (childNodesLen) {
  281.         let count = select.getRangeCount();
  282.         let min = new Object();
  283.         let max = new Object();
  284.         
  285.         for (let i = 0; i < count; i++) {
  286.           select.getRangeAt(i, min, max);
  287.           
  288.           min = Math.max(0, min.value);
  289.           max = Math.min(childNodesLen-1, max.value);
  290.           
  291.           for (let k = min; k <= max; k++) {
  292.             selections[selections.length] = k;
  293.           }
  294.         }
  295.       }
  296.     }
  297.     
  298.     return selections;
  299.   },
  300.   
  301.   appendTreeItemForWord: function(aWord, aIndex) {
  302.     let treeItem = document.createElement("treeitem");
  303.     let treeRow = document.createElement("treerow");
  304.     let treeCell = document.createElement("treecell");
  305.     
  306.     treeCell.setAttribute("label", aWord);
  307.     treeRow.appendChild(treeCell);
  308.     treeItem.appendChild(treeRow);
  309.     
  310.     if (typeof aIndex == "undefined" || !this.treechildren.childNodes.length)
  311.       this.treechildren.appendChild(treeItem);
  312.     else
  313.       this.treechildren.insertBefore(treeItem, this.treechildren.childNodes[aIndex]);
  314.   }
  315. }