2012 年 1 月的归档

Every Presentation Ever: Communication FAIL

article clipper remember Every Presentation Ever: Communication FAIL
 

Regular Expressions in JavaScript

1.定義

Javascript中,定義正則表達式的方法有兩個:

1) RegExp Literal

/* /pattern/flags; */
var re = /mac/i;

2) RegExp Object Constructor

/* new RegExp("pattern","flags"); */
var re = new RegExp(window.prompt("Please input a regex.","yes|yeah"),"g");

 

2. Flag

1) Global Search

g     The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.

2) Flags

i     The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as ?, ü, ?, ?.

3) Multiline Input

m     This flag makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively.

 

3. Pattern

參閱此處

 

4. 應用

1) RegExp.exec(string)

Applies the RegExp to the given string, and returns the match information.

var match = /s(amp)le/i.exec("Sample text")
//match then contains ["Sample","amp"] 

2) RegExp.test(string)

Tests if the given string matches the Regexp, and returns true if matching, false if not.   

var match = /sample/.test("Sample text")
//match then contains false 

3) String.match(pattern)

Matches given string with the RegExp. With g flag returns an array containing the matches, without g flag returns just the first match or if no match is found returns null.   

var str = "Watch out for the rock!".match(/r?or?/g)
//str then contains ["o","or","ro"] 

4) String.search(pattern)

Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not.   

var ndx = "Watch out for the rock!".search(/for/)
//ndx then contains 10 

5) String.replace(pattern,string)

Replaces matches with the given string, and returns the edited string.   

var str = "Liorean said: My name is Liorean!".replace(/Liorean/g,'Big Fat Dork')
//str then contains "Big Fat Dork said: My name is Big Fat Dork!" 

6) String.split(pattern)

Cuts a string into an array, making cuts at matches.   

var str = "I am confused".split(/\s/g)
//str then contains ["I","am","confused"]

 

Example:

var s='<div style="text-align:center;width:inherit;text-color:blue;">SOME TEXT</div>'
var rx=new RegExp("<div .*?>(.*?)</div>","i");
s=s.replace(rx,"$1");

// s="SOME TEXT"
// http://www.tek-tips.com/viewthread.cfm?qid=1231301

article clipper remember Regular Expressions in JavaScript
 

Hide right column in SharePoint Home page

SharePoint中Team Site的主頁, 默認會給創建為按7:3的比例左右分割. 這是一個煩人的問題, 因為很多時候我們並不想以這個比例劃分左右Column.

解決方法:

1. SharePoint Designer

  1. 用SharePoint Designer打開default.aspx
  2. 在Design View中, 點擊右邊webpart 區域, 選擇Menu – Delete Columns from the table. 并改變左邊column的寬度到100%.
  3. 保存,退出

Tip: You can just as easily insert new rows and columns and then add new web part zones from the Insert, SharePoint Controls menu

2. Javascript

<script>
//http://techtrainingnotes.blogspot.com/2008/11/sharepoint-how-to-hide-right-web-part.html
function HideWebPartZone()
{
  var x = document.getElementsByTagName("TD")
  var i=0;
  for (i=0;i<x.length;i++)
  {
    if (x[i].width=="70%")
    {
      // left column
      x[i].style.width="100%"; 

      // center (otherwise empty) column
      if (document.all) // is IE
        var x2=x[i].nextSibling;
      else
        var x2=x[i].nextSibling.nextSibling;

      x2.style.width="0";
      x2.style.display="none";
      x2.innerHTML=""; 

      // right column
      if (document.all) // is IE
        x2=x[i].nextSibling.nextSibling;
      else
        x2=x[i].nextSibling.nextSibling.nextSibling.nextSibling;

      x2.style.width="0";
      x2.style.display="none";
      x2.innerHTML=""; 

      // right margin column
      if (document.all) // is IE
        x2=x[i].nextSibling.nextSibling.nextSibling;
      else
        x2=x[i].nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling;

      x2.style.width="0";
      x2.style.display="none";
      x2.innerHTML="";

      //all done
      return;
    }
  }
}

_spBodyOnLoadFunctionNames.push("HideWebPartZone")
</script>

此方法,在IE7中流暢度欠佳.

用設置Div box的width似乎更理想

3. 直接覆蓋

當創建web part page 之後,需要更改page 的layout (如三欄變為兩欄),可以用同名創建欲改變的layout的web part page 以覆蓋之!

image Hide right column in SharePoint Home page

image 3 Hide right column in SharePoint Home page

image 4 Hide right column in SharePoint Home page

註意:覆蓋會令導致原來頁面中所有內容丟失,請先做好備份! 同時該方法不適用於home page, 因為homepage 異於普通web part page,詳情參見此處

article clipper remember Hide right column in SharePoint Home page
 

SharePoint Cheat sheet

Sharepoint在某些時候,部份的元素實屬多餘,例如Quick Launch Bar等. 要將其去除,最好當然是通過SharePoint Designer. 然而很多時候, 無法通過底層修改SharePoint的模板. 於是,只能求助于CSS.

  1. SharePoint 2007 Cheat Sheet
  2. Cheat Sheet Tool
  3. Style Under Cursor 下載此webpart,并加載到閣下頁面,就可以通過hover的方式,獲取鼠標所指元素的class或者id

下面是Hide Quick Launch Bar代碼

<style>
     .ms-navframe { display: none }
</style>

article clipper remember SharePoint Cheat sheet
 

Observer Pattern in Javascript

Observer Pattern 的重點是, publisher發佈消息之後,subscriber會自動對消息作出反應.

image Observer Pattern in Javascript

I. Publisher

1. Publisher構造函數

  1. 一個數組
  2. 方法:
    1. subscribe    : 添加func到array
    2. unsubscribe: 從array中remove function
    3. publish        : 當publisher publish 消息的時候, 所有subscriber的方法都會給觸發

2. makePublisher(o)

將publisher 的方法複製到object O中,為其添加publisher的方法

該方法中沒有observer

 

Example 1- Here

Example 2 – oreilly (Live)

  • game是window的訂閱者,當window中的onkeypress給觸發,其handlekeypress會立即給觸發
  • Player是game的訂閱者,當game.handlekeypress給觸發,Player的play會立即給觸發
  • game同是又是Player的訂閱者,當Player.play給觸發,game.handlePlay會立即給觸發
  • scoreboard是game的handleplay訂閱者,當game.handlePlay給觸發, scoreboard.update會立即給觸發

window.onkeypress –> game.handlekeypress –> Player:play –> game: handlePlay –> scoreboard.update

 

II. Publisher & Observer

1. Publisher

包含一個數組記錄所有subscribers和publish方法

2. Subscriber

包含subscribe 和 unsubscribe

Example from book 『Pro Javascript Design Patterns』

article clipper remember Observer Pattern in Javascript